We have already illustrated one form of I/O using the printf function, the all-in-one output method for C. In the first program we used printf to output the static string "hello world":

produces the output:

Hello world

Printf"s first parameter is a format string that can contain any number of format specifiers (or "conversion characters") to control how values are printed. In the Hello World example above, the format string contained no specifiers and therefore was printed verbatim. In this example, the format string contains a "%s" specifier to indicate that the value of "myName" should be printed as a string:

This produces the output:

my name is: john

printf is versatile and through the conversion characters it can output data of many different built-in types. The common conversion characters are:

d, i , u    --integer
x	    --hexadecimal
f	    --floating point number
s 	    --string
c	    --character
Another printf example:

The \n is treated as whatever character(s) form a newline on system being compiled for. A few other useful characters are:

  • \n -- newline
  • \t -- tab
  • \b -- backspace
  • \\\\ -- produces a single backslash "\"
 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement