2 Friends in C: printf and scanf functions
Hi,
The left over topics from previous post were:
The left over topics from previous post were:
- printf()
- scanf()
- escape sequences
- format specifiers
In this post I will give you introduction to printf() and scanf() functions.
There is a separate post for escape sequences and format specifier list.
Line 1 shows the generic syntax of printf() function where "<formatted string>" should be replaced by actual string to be printed on console along with the values of variables in format specifier form.
Line 2,3 and 4 have format specifiers for integer, float and character variables.
We can have combination of format specifiers and string. It is important to keep in mind the sequencing of variables should be in sequence in which desired values are required.
Let's understand this by an example:
On executing, the output would look like:
Line 11 of program is shown in Line 1 in Output screen.
Here, you can observe, the values of integer and char are printed as it is and in float there is loss of precision. Later on you will understand why float comparison is not used in programs.
Line 11 of program shown how more that one format specifiers can be written in single printf() statement.
In Line 12, the sequence of format specifiers and variables is same, but, here format specifier is mixed with text content which is to be printed as it is on the screen. Thus, you can observe the output value of iVar, fVar and cVar is same as it was in Line1 of output, only difference is in formatting.
In Line13 of program, we have scrambled the sequence. We have supplied, cVar for "%d", iVar for "%f" and fVar for "%c", and you look at the output. It is also improper.
Thus the takeaway is, while using multiple format specifiers in single printf statement, its compulsory to keep the sequence of format specifier and variable same.
Try Yourself:
Make a simple interest program with following specs:
-parameter(data type, variable name)
a. SimpleInterest (float, si)
b. Principal(integer, p)
c. RateOfInterest(float, r)
d. Time(int, t)
Use the formula si=(p*r*t)/100 to calculate simple interest and print following:
Line 1. printf("format_specifiers", p, n, r, si); //Enter format specifier sequence
Line 2. print value of simple interest only.
Line 3. print "Simple Interest=Rs<value of si>"
Line 4. print "Prin=<principal value> , Rate=<rate value>"
Line 5 print simple interest formula.
What this program do:
A sample run of program produces following output:
Look at Line 6 and Line 7, Guess how addition of 2 numbers gave two different answers.
The brief and much useful intro to 2 friend of yours in C, i.e. printf() and scanf() gets over here.
We will keep coming back to them, on need basis.
For your reference a complete list of "C" format specifiers is created and published as Quick Reference List.
There is a separate post for escape sequences and format specifier list.
The printf( ) function:
The printf() function is used to print formatted string on the standard console. The general syntax of printf() function is as follows:1 2 3 4 | printf ("<formatted string>", <list of variables>); printf("%d", <integer_variable>); printf("%f", <float_variable>); printf("%c", <char_variable>); |
Line 1 shows the generic syntax of printf() function where "<formatted string>" should be replaced by actual string to be printed on console along with the values of variables in format specifier form.
Line 2,3 and 4 have format specifiers for integer, float and character variables.
We can have combination of format specifiers and string. It is important to keep in mind the sequencing of variables should be in sequence in which desired values are required.
Let's understand this by an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include<stdio.h> #include<conio.h> void main() { int iVar; float fVar; char cVar; iVar=100; fVar=200.53; cVar='A'; printf("\n%d %f %c", iVar, fVar, cVar); printf("\n\nMixed\nInteger: %d \nFloat: %f \nChar: %c", iVar, fVar, cVar); printf("\n\nValues after sequence mismatch: \n Integer: %d \nFloat: %f \nChar:%c", cVar, iVar, fVar); getch(); } |
On executing, the output would look like:
1 2 3 4 5 6 7 8 9 | 100 200.529999 A Mixed Integer: 100 Float: 200.529999 Char: A Values after sequence mismatch: Integer: 65 Float: 0.000000 Char: |
Line 11 of program is shown in Line 1 in Output screen.
Here, you can observe, the values of integer and char are printed as it is and in float there is loss of precision. Later on you will understand why float comparison is not used in programs.
Line 11 of program shown how more that one format specifiers can be written in single printf() statement.
In Line 12, the sequence of format specifiers and variables is same, but, here format specifier is mixed with text content which is to be printed as it is on the screen. Thus, you can observe the output value of iVar, fVar and cVar is same as it was in Line1 of output, only difference is in formatting.
In Line13 of program, we have scrambled the sequence. We have supplied, cVar for "%d", iVar for "%f" and fVar for "%c", and you look at the output. It is also improper.
Thus the takeaway is, while using multiple format specifiers in single printf statement, its compulsory to keep the sequence of format specifier and variable same.
Try Yourself:
Make a simple interest program with following specs:
-parameter(data type, variable name)
a. SimpleInterest (float, si)
b. Principal(integer, p)
c. RateOfInterest(float, r)
d. Time(int, t)
Use the formula si=(p*r*t)/100 to calculate simple interest and print following:
Line 1. printf("format_specifiers", p, n, r, si); //Enter format specifier sequence
Line 2. print value of simple interest only.
Line 3. print "Simple Interest=Rs<value of si>"
Line 4. print "Prin=<principal value> , Rate=<rate value>"
Line 5 print simple interest formula.
The scanf() function:
scanf() function is used as to take standard input, which generally is the keyboard. scanf() is most effective when used in its most simplest form. The standard syntax of scanf() is:
1 2 3 4 | scanf("<format_specifier>", &<variable_name>); scanf("%d", &iVar); scanf("%d", &fVar); scanf("%d", &cVar); |
Just like in printf(), scanf() also have format specifiers associated with each variable.
Mentioning format specifier tells the compiler, about the kind of input it should expect on execution of statement.
Ampersand (&) is compulsory in scanf() statements. Its the address of operator. It tells the program to store the input value at the address location specified by the ampersand operator.
Lets understand it by example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /*Program to add a integer and float number*/ #include<stdio.h> #include<conio.h> void main() { int integer=0; float floating=0; int int_result=0; float float_result=0; printf("\nProgram to add a Integer and Float number"); printf("\nEnter Integer Value:"); scanf("%d", &integer); //notice ampersand for storing value at the location of integer printf("\nEnter Float Value:"); scanf("%f", &floating); //notice ampersand for storing value at the location of floating //Adding two numbers and storing result in an integer variable i.e int_result. int_result=integer+floating; //print output of int_result printf("\nInteger Result is: %d", int_result); //Adding two numbers and storing result in a float variable i.e float_result. float_result=integer+floating; //print output of int_result printf("\nFloat Result is: %5.2f", float_result); getch(); } |
What this program do:
The program takes two values viz 1 integer and other float as an input, stores it in integer and floating variables.
Then, first time it adds the two numbers and stores the result in integer variable and displays result on the console.
Next time it adds same two numbers and stores the result in float variable and displays result on the console.
1 2 3 4 5 6 7 | Program to add a Integer and Float number Enter Integer Value:300 Enter Float Value:200.5 Integer Result is: 500 Float Result is: 500.50 |
Look at Line 6 and Line 7, Guess how addition of 2 numbers gave two different answers.
This was a simple scanf() implementation, where we take only one variable as input in each statement.
For taking more than one input in scanf() function. The syntax goes like this:
1 2 | scanf ( "<format_specifier_01> <format_specifier_02> <format_specifier_03>", &<variable_01>, &&<variable_02>, &&<variable_03> ) ; scanf ( "%d %d %f", &p, &n, &r ) ; |
Only difference is, at run time, you have to enter blank or tab separated list as input, in specified order.
In above case, at time of input you have to enter space/tab separated 2 integers followed by a float value.
Note: A blank is creating using a Spacebar, tab using the Tab key and new line using the Enter key.
The brief and much useful intro to 2 friend of yours in C, i.e. printf() and scanf() gets over here.
We will keep coming back to them, on need basis.
For your reference a complete list of "C" format specifiers is created and published as Quick Reference List.
Thanks,
Prateek
Comments
Post a Comment