logo
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 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 '%'
%sstring
%ca char
%dsigned integer
%uunsigned integer
%ounsigned integer in octal
%xunsigned integer in hexa (lowercase)
%Xunsigned integer in hexa (uppercase)
%ffloat (with trailing zeroes)
%gfloat (without trailing zeroes)
%efloat exponential notation (lowercase)
%Efloat exponential notation (uppercase)
%pmemory address in hex
%nstores 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 ' '
*intsame 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 '%'
%sstring (ignores initial blanks read until blank)
%[]string (only characters in [string], OR excludes chars if '^')
%dsigned integer (ignoring the initial spaces)
%uunsigned integer (ignore spaces)
%ounsigned integer in octal
%iinteger (hex if begins with "0x", octal if begins with "0", decimal else)
%cchar (space included)
%ffloat (ignore spaces)
%gfloat (without trailing zeroes)
%nthe number of writen chars
%phex memory address
Put a '*' after the '%' to skip reading. In addition it is possible to read only 3 characters noting '%3s' for example. CAUTION receiving memory absolutely must be large enough to hold the variable read.

The fscanf() and sscanf() functions:

The function fscanf() works just like scanf() except it expects as first parameter the file in which it is read. For sscanf() the first parameter is the string in which it is read.
Be The First To Comment.

Add a comment:





You can use these HTML tags: <br><a><i><b>