Printf and scanf functions
Printf and scanf functions
printf and scanf are the two standard C programming language functions for console input and output. A variation of these commands (fprintf and fscanf) also allows I/O to files. Another (sprintf and sscanf) allows I/O to strings. (sscanf is especially useful.) All these as well as many other I/O functions are declared in the standard header file <stdio.h>.
Using scanf is harder than printf. This is because of promotion, so that any type smaller than an int is promoted to an int when passed to printf, and floats are promoted to double. This means there is a single code for most integers and one for most floating point types. There is no such promotion with scanf arguments, which are actually pointers. So all types must be exactly specified. In addition there are security concerns with input that don't apply to output. (These will be discussed below. In particular, never use the gets() function in production-quality C code!)
Printf() :-
A call to printf looks like this:
Example:-
printf and scanf are the two standard C programming language functions for console input and output. A variation of these commands (fprintf and fscanf) also allows I/O to files. Another (sprintf and sscanf) allows I/O to strings. (sscanf is especially useful.) All these as well as many other I/O functions are declared in the standard header file <stdio.h>.
Using scanf is harder than printf. This is because of promotion, so that any type smaller than an int is promoted to an int when passed to printf, and floats are promoted to double. This means there is a single code for most integers and one for most floating point types. There is no such promotion with scanf arguments, which are actually pointers. So all types must be exactly specified. In addition there are security concerns with input that don't apply to output. (These will be discussed below. In particular, never use the gets() function in production-quality C code!)
Printf() :-
A call to printf looks like this:
printf( "format-string", expression, ... );The format-string can contain regular characters which are simply printed out, and format specifications or place-holders. For each place-holder in the format-string there must be one matching expression. The expressions are converted to strings according to the instructions in the corresponding place-holder and are mixed with the regular text in the format-string. Then the whole string is output. Here's an example:
printf( "%i + %i = %i\n", 2, 3, (2+3) );Output:-
2 + 3 = 5
Example:-
#include<conio.h> #include<stdio.h> void main() {int k,a,b,c; float i; printf( "hello" );//without variable.we can say that's only a message printf( "%d%f",k,i);//with variable. c=a+b; printf( "sum of %d and %d is %d",a,b,c );//without variable.we can say that's only a message getch(); }
hello
5
2.200000
sum of 3 and 3 is 6
Letter | Type of Matching Argument | Example | Output |
---|---|---|---|
% | none | printf( "%%" ); | % |
d, i | int | printf( "%i", 17 ); | 17 |
u | unsigned int (Converts to decimal) | printf( "%u", 17u ); | 17 |
o | unsigned int (Converts to octal) | printf( "%o", 17 ); | 21 |
x | unsigned int (Converts to lower-case hex) | printf( "%x", 26 ); | 1a |
X | unsigned int (Converts to upper-case hex) | printf( "%X", 26 ); | 1A |
f, F | double | printf( "%f", 3.14 ); | 3.140000 |
e, E | double | printf( "%e", 31.4 ); | 3.140000e+01 |
g, G | double | printf( "%g, %g", 3.14, 0.0000314 ); | 3.14, 3.14e-05 |
a, A | double | printf( "%a", 31.0 ); | 0x1.fp+0 |
c | int | printf( "%c", 65 ); | A |
s | string | printf( "%s", "Hello" ); | Hello |
p | void* | int a = 1; printf( "%p", &a ); | 0064FE00 |
n | int* | int a; printf( "ABC%n", &a ); | ABC (a==3) |
Scanf() :-
A call to scanf looks like this:
scanf( "conversion-string", &variable, ... );
The conversion-string can contain three types of directives:
Regular characters | This is text that must be matched character by character with the input. Such entries are rarely used for interactive programs, but can be handy when working with formatted input files. |
white-space characters | A blank, tab, or other white-space character will match any amount (including none) of any white-space. (So a single space will match any string of white-space, including newlines.) Note that it is legal for this to match no input at all (if there isn't a blank or tab, it is ok). |
Conversion Specifiers | Similar to printf conversion specifiers but just different enough to
cause many errors.
They all begin with a percent and end with a letter indicating the type of conversion.
In between can be some special conversion controls, including the length.
Unlike printf, failing to use the exact type and length for the conversion will
result in unpredictable errors.
Since few compilers will check the conversion-string for argument mis-matches, the result
is a runtime (logic) error that can be hard to find.
These conversion specifiers match a string of characters in the input, convert to the specified type (and
length), and store the result in the RAM address provided by the corresponding argument.
(The most common error with scanf is not using the address-of
operator in front of a variable name for the argument.) |
Example:-
#include<conio.h> #include<stdio.h> void main() {int a,b,c; printf("enter two values");\\message scanf("%d%d",&a,&b);\\input of values and you will noticed also '&'sign .This sign is used as address of a,b //in my case i enter 2 and 2 c=a+b; printf( "sum of %d and %d is %d",a,b,c );//without variable getch(); }Output:-
sum of 2 and 2 is 4
Letter | Type of Matching Argument | Auto-skip Leading White-Space |
Example | Sample Matching Input |
---|---|---|---|---|
% | % (a literal, matched but not converted or assigned) | no | int anInt; scanf("%i%%", &anInt); |
23% |
d | int (See note) | yes | int anInt; long l; scanf("%d %ld", &anInt, &l); |
-23 200 |
i | int | yes | int anInt; scanf("%i", &anInt); | 0x23 |
o | unsigned int | yes | unsigned int aUInt; scanf("%o", &aUInt); | 023 |
u | unsigned int | yes | unsigned int aUInt; scanf("%u", &aUInt); | 23 |
x | unsigned int | yes | unsigned int aUInt; scanf("%d", &aUInt); | 1A |
a, e, f, g | float or double | yes | float f; double d; scanf("%f %lf", &f, &d); |
1.2 3.4 |
c | char | no | char ch; scanf(" %c", &ch); | Q |
s | array of char | yes | char s[30]; scanf("%29s", s); | hello |
p | void | yes | int* pi; void* ptr; scanf("%p", &ptr); pi = (int*) ptr; |
0064FE00 |
n | int | no | int x, cnt; scanf("X: %d%n", &x, &cnt); |
X: 123 (cnt==6) |
[ | array of char | no | char s1[64], s2[64]; scanf(" %[^\n]", s1); scanf("%[^\t] %[^\t]", s1, s2); |
Hello World field1 field2 |
Comments
Post a Comment