0


Formatted Input/Output functions

scanf( ) function – scanf( ) function is used to read/input values of variables using the standard input device (keyboard). It has the following form:-

scanf(“format string”, &V1 &V2……. &Vn);

Where V1, V2 …… Vn are variables whose values are to be read from the keyboard.
            “format string” is the control string which represents the format specification(Table1).
            The symbol & (ampersand) represents the memory address where the variable value is to be stored.
Ex:-
·         scanf(“%d  %d”, &a, &b);                  to read value of int variable a and b.
·         scanf(“%f %d”, &x, &n);                   to read float value of x and int value of n.
·         scanf(“%c”, &sex);                             to read char value for variable sex.
·         scanf(“%s”, sname);                            to read string of char variable sname.

When these functions are executed, the computer will wait for the values of the variable listed in scanf which are to be entered using the keyboard.

Format Specifier (Table 1)

Data Type
Format
Meaning

int
%d
%u
%o
%
Represents a decimal integer value
Represents an unsigned integer value
Represents an unsigned octal value
Represents an unsigned hexadecimal value

float/double
%f
%e
Represents a floating point value
Represents a floating point value in decimal or exponential form
char
%c
%s
Represents a single character value
Represents a sting of value of characters

Note - %hd, %ld, %lf are used for shot int, long int and long double respectively.


printf( ) function – printf( ) function is used to print/display value of variables using the standard output device(monitor). It has following form:

printf( “format string”, V1, V2, ……………Vn);

Where V1, V2, ………. Vn are variable whose values are to be displayed in the monitor.
            “format string” is the control string which represents the format specification(Table 1).

Ex:-
·         printf(“%f:, s);
·         printf(“\n sum = %6.3f”, s);
·         printf(“\n %d factorial is %d”, k, kfact);

When these functions are executed, the computer prints/displays the values of the variables listed in printf( ).
Note –
(i)                 \n used in printf is an escape sequence to print the results in the new line.
(ii)               %6.3f represents the formatted print with total width 6 and number of decimal digits 2.

→  Use a formatted print with required number of decimal digits for float type variable to display the results in decimal form.


Escape sequences – Escape sequences are control character used to move the cursor and print characters such as ?, “. \ and so on.

Character Constant
Meaning
\a
Audible bell (Alert)
\b
Backspace
\f
Form feed
\n
Move to new line
\r
Carriage return (Enter)
\t
Horizontal Tab
\v
Vertical Tab
\\
Print back slash
\?
Print question mark
\’
Print single quote
\”
Print double quote
\0
Null character

Assignment Statement – An assignment statement is used to assign value to a variable. It has following form:

Variable = value or expression;

Ex: -
            m = 25;
            y = a*a+4*a-5;
            s = s + a;


Multiple assignments Statement – A multiple assignment statement is used to assign a value to more than one variable. It has the following form:
           
            V1 = V2 ………….. = Vn = value or expression;

            Where V1, V2, ……………. Vn are variables.

            Ex:-
                        m = n = 3;
                        np = nm = nz = 0;
                        x = y = (a*a+b*b)/2;

More about formatted output functions – Output values can be printed with specific width using commands for formatted output.
Ex:-
            int n = 28;
            printf(“%5d”, n);
It will print n value right justified with three preceding spaces:




2
8

            printf(“% -5d”, n);
It will print n value left justified.
2
8




printf(“% +5d”, n);
It will print n value right justified with + sign.


+
2
8

float x = 28.358416;
printf(“%6.2f”, x);
It will print x value right justified with a space preceding it.

2
8
.
3
6

            printf(“%0.2f”, x);
It will print x value left justified without any space preceding it.     
2
8
.
3
6

It should be noted that even if the total width may be assigned wrong (e.g. zero), the computer prints the result with the required number of decimal digits.

            printf(“%0.3e”, x);
It will print x value left justified in exponential form.
2
.
8
4
e
+
0
1

            char st [10] = “NEWYORK”;
            printf(“%s”, st);
It will print st value left justified.
N
E
W
Y
O
R
K

printf(“%10s”, st);
It will print st value right justified



N
E
W
Y
O
R
K

printf(“%10.3s”, st);
It will print st value right justified and only the first 3 letters are printed.







N
E
W

            printf(“% -10.3”, st);
It will print st value left justified and only the first 3 letters are printed.
N
E
W








            printf(“% .3”, st);
It will print only the first 3 letters.
N
E
W

Character Input/Output Functions

getchar ( ) function – getchar ( ) function is used to read one character at a time from the standard input device (Keyboard). It has following form:
            ch = getchar ( );
where ch is a char variable.
Ex:-
            char c;
            c = getchar ( );
When this function is executed, the computer will wait for a key to be pressed and assigns the value to the variable when the enter key is pressed.

putchar ( ) function – This function is used to display one character at a time on the monitor screen . It has the following form:-
            putchar (ch);
Where ch is a char variable.
Ex:-
char c = ‘M’;
            putchar (c);
When this function is executed, the computer will display the value of the char variable (i.e. letter M) on the monitor screen.

getch ( ) function – getch ( ) function is used to read a character from the keyboard and it does not expect the neter key press. It has following form:
            ch = getch ( );
Where ch is char variable.
Ex:-
            char c;
            c = getch ( );
When this function is executed, the computer waits for a key to be pressed from the keyboard. As soon as a key is pressed, the control is transferred to the next line of the program and the value is assigned to the char variable. It is to be noted that the key ( letter) pressed will not be displayed on the monitor screen.

putch ( ) function – putch ( ) function is used to display a character on the monitor screen. It has following form:
            putch (ch);
Where ch is a char variable.
Ex:-
            char c = ‘s’;
            putch (c);
When this function is executed, the computer will display the value of the variable ‘s’ on the monitor screen.

getche ( ) function – getche ( ) function is used to read a character from the keyboard without expecting the enter key press. However any key pressed by the user will be displayed on the monitor. It has the following form:
            ch = getche ( );
Where ch is a char variable.
Ex: -
            char c;
            c = getche ( );
When this function is executed, the computer waits for a key to be pressed from the keyboard. The value is assigned to the char variable.
                        getche ( ) is similar to getch ( ) except that getche ( ) displays the key pressed from the keyboard on the monitor screen. The ‘e’ at the end of getche ( ) stands for echo. The header file <conio.h> (console input output) is include to use these functions in a program.

gets(  ) function – gets ( ) function is used to read a string of characters including white spaces. Note that a string containing white spaces cannot read using scanf( ) with “%s” format specifier. It has the following form:
            gets (st);
Where st is a character string variable.
Ex:-
            char st [20];
            gets (st);
When this function is executed, the computer waits for the string value. Enter string of characters (e.g. “UNITED STATES”) and press the enter key. It is to be noted that C language will add a null character “\0” to the end of any character string.
0      1      2     3      4        5     6      7       8     9      10   11     12    13   14    15    16    17    18    19
U
N
I
T
E
D

S
T
A
T
E
S
\0






↑                                              ↑                                                       ↑
St                                     white space                                     null character              
└→ char variable can hold maximum 20 characters as per the declaration.

puts ( ) function – put ( ) function is used to display a character string on the monitor screen. It has the following form:
            puts (str);
Where str is a string (array of characters)
Ex:-
            char st [20] = “COMPUTER WORLD”;
            puts (st);
When this function is executed, the computer will display the string.

clrscr ( ) function – clrscr ( ) function is used to clear the monitor screen. It has following form:
            clrscr ( );

When this function is executed, the previously displayed text/error messages will be cleared. This is used when a new program is run. It is to be noted that the header file <conio.h> is included to use this function in a program.

A Program with their Details 

You can Find out the Flowchart and Algorithm Click Here





Like it ? Share it.

Post a Comment

Comment Rules :
1. Do not post Adult/illegal Links.
2. Try to comment in only English Language.
3. Do not post other website's links which are useless.
4. Your Comment should be based on the Topic for other queries Kindly Visit our Contact Us Page.
5. Do not use Abusive Language.
6. Respect each other.
Thank You for following the rules. Please Comment....

 
Top