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:
  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();
}
Output:-
hello 5 2.200000
sum of 3 and 3 is 6

Printf  Conversion Letters and Matching Types
LetterType of Matching ArgumentExampleOutput
%none printf( "%%" );%
d, iint printf( "%i", 17 );17
uunsigned int (Converts to decimal) printf( "%u", 17u );17
ounsigned int (Converts to octal) printf( "%o", 17 );21
xunsigned int (Converts to lower-case hex) printf( "%x", 26 );1a
Xunsigned int (Converts to upper-case hex) printf( "%X", 26 );1A
f, Fdouble printf( "%f", 3.14 );3.140000
e, Edouble printf( "%e", 31.4 );3.140000e+01
g, Gdouble printf( "%g, %g", 3.14, 0.0000314 );3.14, 3.14e-05
a, Adouble printf( "%a", 31.0 );0x1.fp+0
cint printf( "%c", 65 );A
sstring printf( "%s", "Hello" );Hello
pvoid* int a = 1; printf( "%p", &a );0064FE00
nint* 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 charactersA 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 SpecifiersSimilar 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
scanf Conversion Letters and Matching Types
LetterType of Matching ArgumentAuto-skip
Leading
White-Space
ExampleSample Matching Input
%% (a literal, matched but not converted or assigned) noint anInt;
scanf("%i%%", &anInt);
23%
dint (See note) yesint anInt; long l;
scanf("%d %ld", &anInt, &l);
-23 200
iint yesint anInt;
scanf("%i", &anInt);
0x23
ounsigned int yesunsigned int aUInt;
scanf("%o", &aUInt);
023
uunsigned int yesunsigned int aUInt;
scanf("%u", &aUInt);
23
xunsigned int yesunsigned int aUInt;
scanf("%d", &aUInt);
1A
a, e, f, gfloat or double yesfloat f; double d;
scanf("%f %lf", &f, &d);
1.2 3.4
cchar nochar ch;
scanf(" %c", &ch);
Q
sarray of char yeschar s[30];
scanf("%29s", s);
hello
pvoid yesint* pi; void* ptr;
scanf("%p", &ptr);
pi = (int*) ptr;
0064FE00
nint noint x, cnt;
scanf("X: %d%n", &x, &cnt);
X: 123  (cnt==6)
[array of char  nochar s1[64], s2[64];
scanf(" %[^\n]", s1);
scanf("%[^\t] %[^\t]", s1, s2);

Hello World
field1   field2

Comments

Popular posts from this blog