printf

printf is a function that writes to stdout.  It is declared in the stdio.h header file.

#include <stdio.h>

int printf(const char *format, ...);

for example:


printf("Hello World! \n");

this will cause “Hello World!” to be displayed on stdout followed by a newline.

printf returns an integer representing the actual number of characters.  In the “Hello World” example above printf would return 15.

Some common sequences used for formatting in printf are:

 

Code \ Escape Sequence  Purpose
 \n  Newline
 \t  tab
 %d  Signed Decimal Integer
 %f  Floating point decimal
 %s  String of Characters
Sample Code:
#include <stdio.h>


int main(int argc, char** argv) {
    
    int i=10;

    float j=3.5;

    char Iamusing[]="a Computer!";

    int rc;

    rc=printf("i = %d\n",i);
    printf("the first printf call returned %d\n",rc);
    
    rc=printf("j = %f\n",j);
    printf("the second printf call returned %d\n",rc);
    
    rc=printf("Iamusing = %s\n",Iamusing);
    printf("the third printf call returned %d\n",rc);
            
            

    return 0;
}