#include <stdio.h> // for a C compiler #include <cstdio> // for a C++ compiler
printf (format_string, list_of_expressions);where:
printf("Hello, world!\n"); printf("Greetings, Earthling\n\n");
Here are some commonly used conversion specifiers (not a comprehensive list):
%d int (signed decimal integer) %u unsigned decimal integer %f floating point values (fixed notation) - float, double %e floating point values (exponential notation) %s string %c character
int numStudents = 35123; printf("FSU has %d students", numStudents); // Output: // FSU has 35123 students
printf("FSU has %10d students", numStudents); // Output: // FSU has 35123 students
printf("FSU has %-10d students", numStudents); // Output: // FSU has 35123 students
printf("FSU has %2d students", numStudents); // Output: // FSU has 35123 students
double cost = 123.45; printf("Your total is $%f today\n", cost); // Output: // Your total is $123.450000 today
printf("Your total is $%e today\n", cost); // Output: // Your total is $1.234500e+02 todayNote that the e+02 means "times 10 to the 2nd power"
printf("Your total is $%.2f today\n", cost); // Output: // Your total is $123.45 today
printf("Your total is $%9.2f today\n", cost); // Output: // Your total is $ 123.45 todayIn the conversion specifier, the number before the decimal is field width, and the number after is the precision. (In this example, 9 and 2).
char letter = 'Q'; printf("%c%c%c\n", '*', letter, '*'); // Output is: *Q*
printf("%s%10s%-10sEND\n", "Hello", "Alice", "Bob"); // Output: // Hello AliceBob END
output.c -- contains all of
the above sample outputs. Try running it yourself
scanf(format_string, list_of_variable_addresses);
int month, day; printf("Please enter your birth month, followed by the day: "); scanf("%d %d", &month, &day);
#include <stdio.h> int main() { int i; float f; char c; printf("Enter an integer and a float, then Y or N\n> "); scanf("%d%f%c", &i, &f, &c); printf("You entered:\n"); printf("i = %d, f = %f, c = %c\n", i, f, c); }
Enter an integer and a float, then Y or N > 34 45.6Y You entered: i = 34, f = 45.600, c = Y
Enter an integer and a float, then Y or N > 12 34.5678 N You entered: i = 12, f = 34.568, c =Note that in this sample run, the character that was read was NOT the letter 'N'. It was the space. (Remember, white space not skipped on character reads).
This can be accounted for. Consider if the scanf line looked like this:
scanf("%d%f %c", &i, &f, &c);There's a space betwen the %f and the %c in the format string. This allows the user to type a space. Suppose this is the typed input:
12 34.5678 NThen the character variable c will now contain the 'N'.
input2.c -- a version of the
example with this change is linked here
int age; double gpa; char answer; printf("Please enter your age: "); scanf("%d", &age); printf("Please enter your gpa: "); scanf("%lf", %gpa); printf("Do you like pie (Y/N)? "); scanf("%c", %answer);
A good way to learn more about scanf is to try various inputs in various combinations, and type in test cases -- see what happens!
An entire C-style string can be easily printed, by using the %s formatting symbol, along with the name of the char array storing the string (as the argument filling in that position):
char greeting[] = "Hello"; printf("%s", greeting); // prints the word "Hello"Be careful to only use this on char arrays that are being used as C-style strings. (This means, only if the null character is present as a terminator).
Similarly, you can read a string into a char array with scanf. The following call allows the entry of a word (up to 19 characters and a terminating null character) from the keyboard, which is stored in the array word1:
char word1[20]; scanf("%s", word1);
Characters are read from the keyboard until the first "white space" (space, tab, newline) character is encountered. The input is stored in the character array and the null character is automatically appended.
Note also that the & was not needed in the scanf call
(word1 was used, instead of &word1). This is because
the name of the array by itself (with no index) actually IS a variable
that stores an address (a pointer).