Intermediate Programming
|
|
3. Conversion specifiers - Conversion specifiers tell printf() where variables should be printed and how they should be printed. The following is a list of common conversion specifiers.
Take the following for example:
int x = 1;
printf("x contains %d \n", x);
|
The first statement simply initializes the variable "x." The second statement prints the message on the screen. Printf() will print the literal string "X contains" then it will print the contents of x, and then move to the next line.
puts() Sometimes you need to print text without printing any variables. In these instances you should use "puts()" for output. Puts() allows literal strings and escape sequences but not conversion specifiers. The following is an example of the puts() statement. puts("Hello World \n"); This statement will print the words "Hello World" on the screen and then move to the next line. putchar()
There are times when a program only needs to output a single character. Instead of using printf() or scanf() the program can use a third output function putchar(). putchar() takes the ASCII code of the character you want to print and then prints the character to the screen.
/*Output the letter A to the screen*/ putchar(65);
Input
Scanf()
The scanf() function reads data according to the specified format, and assigns it to the passed variables.