Download presentation
Presentation is loading. Please wait.
1
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include #include int main ( ) { printf( "%d\n", 455 ); printf( "%d\n", 455 ); printf( "%i\n", 455 ); printf( "%i\n", 455 ); printf( "%d\n", +455 ); printf( "%d\n", +455 ); printf( "%d\n", -455 ); printf( "%d\n", -455 ); printf( "%hd\n", 32000); printf( "%hd\n", 32000); printf( "%ld\n", 2000000000 ); printf( "%ld\n", 2000000000 ); printf( "%d\n", 455 ); printf( "%d\n", 455 ); printf( "%o\n", 455 ); printf( "%o\n", 455 ); printf( "%u\n", 455 ); printf( "%u\n", 455 ); printf( "%u\n", -455 ); printf( "%u\n", -455 ); printf( "%x\n", 455 ); printf( "%x\n", 455 ); printf( "%X\n", 455 ); printf( "%X\n", 455 ); return 0; return 0; } (week04 specifier.c) } (week04 specifier.c)
2
What we observed d --- is same as i in printf d --- is same as i in printf o --- Display an unsigned octal number o --- Display an unsigned octal number u --- Display an unsigned decimal integer u --- Display an unsigned decimal integer X, x --- unsigned hexadecimal 0-9 a-f or A-F h or l --- short or long integer
3
Example 2 /* Using Integer Conversion Specifies */ /* Using Integer Conversion Specifies */ #include #include int main ( ) int main ( ) { { printf( "%e\n", 1234567.89 ); printf( "%e\n", 1234567.89 ); printf( "%e\n", +1234567.89 ); printf( "%e\n", +1234567.89 ); printf( "%e\n", -1234567.89 ); printf( "%e\n", -1234567.89 ); printf( "%E\n", 1234567.89 ); printf( "%E\n", 1234567.89 ); printf( "%f\n", 1234567.89); printf( "%f\n", 1234567.89); printf( "%g\n", 1234567.89 ); printf( "%g\n", 1234567.89 ); printf( "%G\n", 1234567.89 ); printf( "%G\n", 1234567.89 ); return 0; return 0; } (week04 spe2.c) } (week04 spe2.c)
4
From this Example E or e --- Display a floating-point in exponential notation. exponential notation. f --- Display floating-point values G or g --- Display a floating-point value in either floating-point form f or e (or E) (week04 spe3.c) L --- Place before any floating-point to indicate a long double For g and G, default is 6 significant digits.
5
Caraacters and strings /* Using Integer Conversion Specifiers */ /* Using Integer Conversion Specifiers */ #include #include int main ( ) int main ( ) { { char character = 'A'; char character = 'A'; char string[ ] = "This is also a string"; char string[ ] = "This is also a string"; const char *stringPtr = "This is also a string"; const char *stringPtr = "This is also a string"; printf( "%c\n", character ); printf( "%c\n", character ); printf( "%s\n", "This is a string" ); printf( "%s\n", "This is a string" ); printf( "%s\n", string ); printf( "%s\n", string ); printf( "%s\n", stringPtr ); printf( "%s\n", stringPtr ); return 0; return 0; } (week04 intspe.c) } (week04 intspe.c)
6
Print strings and characters c and s are used to print individual characters and strings c and s are used to print individual characters and strings c- requires a char argument c- requires a char argument s- requires a pointer to char as an argument s- requires a pointer to char as an argument
7
Common programming Error 1. Using %c to print a string 1. Using %c to print a string 2. Using %s to print a character 2. Using %s to print a character 3. Using single quotes around character string is a syntax error. 3. Using single quotes around character string is a syntax error. 4. Using a double quotes around a character constant 4. Using a double quotes around a character constant
8
More about c in/out format %p --- Display a pointer value in an %p --- Display a pointer value in an implementation-defined manner. implementation-defined manner. %n --- store the number of characters already output in the current printf statement. (including all blanks, too) %n --- store the number of characters already output in the current printf statement. (including all blanks, too) % Display the percent character. % Display the percent character.
9
Example /* Using Integer Conversion Specifiers */ /* Using Integer Conversion Specifiers */ #include #include int main ( ) int main ( ) { { int *ptr; int *ptr; int x = 12345, y; int x = 12345, y; ptr = &x; ptr = &x; printf( "The value of ptr is %p\n", ptr ); printf( "The value of ptr is %p\n", ptr ); printf( "The address of x is %p\n\n", &x ); printf( "The address of x is %p\n\n", &x ); printf( "Total characters printed on this line is:%n", &y ); printf( "Total characters printed on this line is:%n", &y ); printf( " %d\n\n", y ); printf( " %d\n\n", y ); y = printf( "This line has 28 characters\n" ); y = printf( "This line has 28 characters\n" ); printf( “%d characters were printed\n\n", y); printf( “%d characters were printed\n\n", y); printf( "Printing a % in a format control string\n" ); printf( "Printing a % in a format control string\n" ); return 0; return 0; } (week04 spe5.c) } (week04 spe5.c)
10
More about numbers’ format %d 37 -37 %d 37 -37 %.4d 0037 –0037 %.4d 0037 –0037 %11d 37 -37 %11d 37 -37 %11.4d 0037 -0037 %11.4d 0037 -0037 %-11 37 -37 %-11 37 -37 %-11.4 0037 -0037 %-11.4 0037 -0037 %011d 00000000037 -00000000037 %011d 00000000037 -00000000037 The default is right justified. By using ‘–’ right after % will make the field left justified. The default is right justified. By using ‘–’ right after % will make the field left justified.
11
Printing Literals and Escape Sequences \’ \’ \” \” \? \? \\ \\ \a Cause an audible (bell) or visual alert. \a Cause an audible (bell) or visual alert. \b Move the cursor back one position on the current line. \b Move the cursor back one position on the current line. \f Move the cursor to the start of the next logical page. \f Move the cursor to the start of the next logical page. \n \n \r More the cursor to the beginning of the current line. \r More the cursor to the beginning of the current line. \t More the cursor to the next horizontal tab position. \t More the cursor to the next horizontal tab position. \v Move the cursor to the next vertical tab position. \v Move the cursor to the next vertical tab position.
12
Program Example #include #include int main ( ) int main ( ) { { int month1, day1, year1, month2, day2, year2; int month1, day1, year1, month2, day2, year2; printf( "Enter a date in the form mm-dd-yyyy: "); printf( "Enter a date in the form mm-dd-yyyy: "); scanf( "%d%*c%d%*c%d”, &month1, &day1, &year1 ); scanf( "%d%*c%d%*c%d”, &month1, &day1, &year1 ); printf( "month = %d day = %d year = %d\n", month2, day2, year2 ); printf( "month = %d day = %d year = %d\n", month2, day2, year2 ); printf( "Enter a date in the form mm/dd/yyyy: " ); printf( "Enter a date in the form mm/dd/yyyy: " ); scanf( "%d%*c%d%*c%d", &month2, &day2, &year2 ); scanf( "%d%*c%d%*c%d", &month2, &day2, &year2 ); printf( "month = %d day = %d year = %d\n", month2, day2, year2 ); printf( "month = %d day = %d year = %d\n", month2, day2, year2 ); return 0; return 0; } } Notice the * in front of c% as an Notice the * in front of c% as an Assignment Suppression Character to discard characters from input. (week04 spe6.c) Assignment Suppression Character to discard characters from input. (week04 spe6.c)
13
Basic Arithmetic Operations Example: (week04 arith.c) Example: (week04 arith.c) #include #include int main(void) int main(void) { int a, b, c, d; int a, b, c, d; printf("Enter an integer: "); printf("Enter an integer: "); scanf("%d", &a); scanf("%d", &a); b = a + 4; b = a + 4; c = a - 3; c = a - 3; d = -a; d = -a; printf("b is %d,\nc is %d, and\nd is %d.\n\n", b, c, d); printf("b is %d,\nc is %d, and\nd is %d.\n\n", b, c, d); b = a * 3; b = a * 3; c = a / 3; c = a / 3; d = a % 3; d = a % 3; printf("Now\nb is %d,\nc is %d, and\nd is %d.\n", b, c, d); printf("Now\nb is %d,\nc is %d, and\nd is %d.\n", b, c, d); return 0; return 0; }
14
Another Example Week04 ---- miles.c Week04 ---- miles.c
15
#include #include #define MILEAGE 28 #define MILEAGE 28 int main(void) int main(void) { int gallons; int gallons; printf(" Travel Calculator\n"); printf(" Travel Calculator\n"); printf("---------------------------------\n"); printf("---------------------------------\n"); printf("Current mileage is %d miles per gallon.\n", MILEAGE); printf("Current mileage is %d miles per gallon.\n", MILEAGE); printf("\n\nEnter the gallons of gas (whole numbers please): "); printf("\n\nEnter the gallons of gas (whole numbers please): "); scanf("%d", &gallons); scanf("%d", &gallons); printf("Thank you. You will be able to travel %d miles.\n", miles( gallons, MILEAGE)); printf("Thank you. You will be able to travel %d miles.\n", miles( gallons, MILEAGE)); return 0; return 0; } /******************************* miles() **************************/ /******************************* miles() **************************/ int miles( int num_gallons, int mileage ) int miles( int num_gallons, int mileage ) { return ( num_gallons * mileage ); return ( num_gallons * mileage ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.