-
Notifications
You must be signed in to change notification settings - Fork 0
Home
bebrown edited this page Oct 13, 2014
·
1 revision
printf - prints to stdout ##Synopsis
#include <stdio.h>
int printf(char *format);##Description
printf prints some formatted output to stdout (your computer terminal). You specify the format with a % followed by a c for a character, d for a digit, and s for a string. There are a number of other identifiers; the aformentioned, however, are the most used.
You can also further format output to stdout. After the %, including a number, such as 2, specifies the minimum number of characters to be printed. Using a decimal point . followed by some number, such as .2 indicates how precise you want your answer. A negative number, such as -2, will left-justify your output.
| Format | Meaning |
|---|---|
| %d | print out digit |
| %i | print out integer |
| %f | print out float |
| %c | print out character |
| %s | print out string |
| %2d | print out digit using at least 2 characters |
| %.2d | specifies precision |
printf returns the total number of characters written. If there is a problem, it will return a negative number.
printf("%c%c%d %s\n", 'C', 'S', 50, "rocks!");
printf("%3d\n", 42);
printf("%5d\n", 42);
printf("%.2f\n", 42.0);Output:
CS50 rocks!
42
42
42.00