printf function


printf is used to O/P data to STDOUT (usually the screen). It has many formatting options which we shall look at in a moment.


printf syntax

This is an example of printf in its simplest form.


  #include <stdio.h>

  main()
  {
    printf("This text will appear on the screen\n");  
  }

printf is passed one formatting argument. The unusual thing about the example (in my mind) is \n, this is actually an escape sequence that signals a new line. Without it, any printf's that follow would O/P to the same line. printf also takes extra arguments which are inserted into the format string at locations marked with a %.


  #include <stdio.h>

  main()
  {
    int number=42;
    printf("The answer is %i\n", number);  
  }

What happens here is the %i is seen as a formatting identifer for the next argument (number). In this case an integer is expected.


See also

  1. puts Much easier to use - but not as powerfull.
  2. sprintf Same as 'printf' but O/P to a string array.
  3. Strings.
  4. A dead handy printf idiom..


Top Master Index Keywords Functions


Martin Leslie