
The functions printf and scanf 'C'
The printf() function
Return value
Display formatting
An example
The fprintf() function
The sprintf() and snprintf() functions
The scanf() function
Formatting reading
The fscanf() and sscanf() functions
The printf() function
Return value
Display formatting
An example
The fprintf() function
The sprintf() and snprintf() functions
The scanf() function
Formatting reading
The fscanf() and sscanf() functions
The functions printf and scanf 'C'
The printf() function:
Printf is part of the standard input-output library (stdio.h). Its role is to produce a display on standard output (stdout).Return value:
The printf function returns the number of characters written and on error (very rare), the function returns a negative number.#include <stdio.h> int main(int argc,char* argv[]) { int n=printf("%s",argv[0]); printf("\nThe string written with the printf above is %d characters long\n",n); return 0; }
Display formatting:
A small non-exhaustive list of conversion formats:
%% | the char '%' |
%s | string |
%c | a char |
%d | signed integer |
%u | unsigned integer |
%o | unsigned integer in octal |
%x | unsigned integer in hexa (lowercase) |
%X | unsigned integer in hexa (uppercase) |
%f | float (with trailing zeroes) |
%g | float (without trailing zeroes) |
%e | float exponential notation (lowercase) |
%E | float exponential notation (uppercase) |
%p | memory address in hex |
%n | stores the number of characters written |
Le gabarit:
It is possible to change the display by adding information between the '%' and the letter of format.+ | forces the display of the sign |
(space) | displays a sign if negative a space if positive |
(number) | uses at least number characters, complete with ' ' |
*int | same effect as (number) |
0(number) | uses at least number characters, complete with 0 |
.(number) | rounding the float to number decimals |
An example:
#include <stdio.h> int main(int argc,char* argv[]) { int i=-43,j=43,n; double f=345.5756; char* the_string="Test string."; /* integers */ printf("%d\n",i); printf("%+d\n",j); printf("% d\n",j); printf("%u\n",i); printf("%o\n",j); printf("%x\n",j); printf("%X\n",j); /* floats */ printf("%f\n",f); printf("%.2f\n",f); printf("%g\n",f); printf("%e\n",f); printf("%E\n",f); /* string */ printf("%s\n",the_string); /* measure */ printf("%g%n mesures:",f,&n); printf("%d chars\n",n); printf("&n=%p\n",&n); return 0; } /* hanoo@hp_laptop % ./a.out -43 +43 43 4294967253 53 2b 2B 345.575600 345.58 345.576 3.455756e+02 3.455756E+02 Test string. 345.576 mesures:7 chars &n=0x7ffffef668a4 */
The fprintf() function:
Function fprintf() works just like printf() except it expect as the first parameter the file in which it is written.#include <stdio.h> int main(int argc,char* argv[]) { /* printf and fprintf print here the same thing */ printf("%s\n",argv[0]); fprintf(stdout,"%s\n",argv[0]); return 0; }
The sprintf() and snprintf() functions:
These two functions work the same way but they store the result in a string buffer. WARNING, many security problems come from the incorrect use of sprintf(). The reason is that the size of the string which is inserted in the result is not tested. There may therefore be 'buffer overflow' if the result is greater than the buffer and not always segfaulting. It is strongly recommended to use snprintf() that takes the maximum size possible for the destination buffer, in order to avoid buffer overflow.#include <stdio.h> #define BUFFER_SIZE 256 int main(int argc,char* argv[]) { char* thing="THING"; char* stuff="STUFF"; char* another="ANOTHER"; char buffer[BUFFER_SIZE]; /* here there is a risk of BOF if the buffer is smaller than "THING;STUFF;ANOTHER\n" */ sprintf(buffer, "%s;%s;%s\n",thing,stuff,another); printf("%s",chaîne); /* do it safe */ snprintf(buffer, BUFFER_SIZE, "%s;%s;%s\n",thing,stuff,another); printf("%s",buffer); return 0; } /* hanoo@hp_laptop % ./a.out THING;STUFF;ANOTHER THING;STUFF;ANOTHER */
The scanf() function:
The scanf function is to reading what printf is to writing. It can read data from standard input (stdin). Their operation is very close, but as scanf will change the value of variables associated with it, it must pass through a reference (address).Formatting reading:
A small non-exhaustive list of conversion formats:
%% | the char '%' |
%s | string (ignores initial blanks read until blank) |
%[] | string (only characters in [string], OR excludes chars if '^') |
%d | signed integer (ignoring the initial spaces) |
%u | unsigned integer (ignore spaces) |
%o | unsigned integer in octal |
%i | integer (hex if begins with "0x", octal if begins with "0", decimal else) |
%c | char (space included) |
%f | float (ignore spaces) |
%g | float (without trailing zeroes) |
%n | the number of writen chars |
%p | hex memory address |