Ads 468x60px

Labels

Thursday, 6 September 2012

Input and Output functions in C


printf

This offers more structured output than the putchar. Its arguments are, in order; a control string, which controls what get printed, followed by a list of values to be substituted for entries in the control string.

The prototype for the printf() is:
 printf(const char *format, ...);

Scanf
To grab things from input,scanf() is used. Beware though, scanf isn't greatest function that C has to offer. Some people brush off the scanf as a broken function that shouldn't be used often. 
The prototype for scanf is:
Last example will create a rectangle with rounded corner:
 scanf( const char *format, ...);
To read of data from the keyboard,scanf allows formatted . Like printf it has a control string,followed by the list of items to be read. However scanf wants to know the address of items to be read, since it is a function which will change that value. Therefore the names of variables are preceeded by the & sign. Character strings are an exception to this. Since a string is already a character pointer, we give the names of the string variables unmodified by a leading &. Control string entries which match values to be read are preceeded by the percentage sign in a similar way to their printf equivalent. Looks similar to printf, but doesn't completely behave like the printf does. Take the example:
scanf("%d", x);
For grabbing things from input. Beware though, scanf isn't the greatest function that C has to offer,scanf() is used.