C Formatted InputOutput Using Integer Conversion Specifiers include
- Slides: 15
C Formatted Input/Output /* Using Integer Conversion Specifiers */ #include <stdio. h> int main ( ) { printf( "%dn", 455 ); printf( "%in", 455 ); printf( "%dn", +455 ); printf( "%dn", -455 ); printf( "%hdn", 32000); printf( "%ldn", 200000 ); printf( "%dn", 455 ); printf( "%on", 455 ); printf( "%un", -455 ); printf( "%xn", 455 ); printf( "%Xn", 455 ); return 0; } (week 04 specifier. c)
What we observed d --- is same as i in printf o --- Display an unsigned octal number u --- Display an unsigned decimal integer X , x --- unsigned hexadecimal 0 -9 a-f or A-F h or l --- short or long integer
Example 2 n n n /* Using Integer Conversion Specifies */ #include <stdio. h> int main ( ) { printf( "%en", 1234567. 89 ); printf( "%en", +1234567. 89 ); printf( "%en", -1234567. 89 ); printf( "%En", 1234567. 89 ); printf( "%fn", 1234567. 89); printf( "%gn", 1234567. 89 ); printf( "%Gn", 1234567. 89 ); n n n return 0; } (week 04 spe 2. c)
From this Example E or e --- Display a floating-point in 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) (week 04 spe 3. c) L --- Place before any floating-point to indicate a long double For g and G, default is 6 significant digits.
Caraacters and strings n n n n /* Using Integer Conversion Specifiers */ #include <stdio. h> int main ( ) { character = 'A'; char string[ ] = "This is also a string"; const char *string. Ptr = "This is also a string"; n n n printf( "%cn", character ); printf( "%sn", "This is a string" ); printf( "%sn", string. Ptr ); n n n return 0; } (week 04 intspe. c)
Print strings and characters c and s are used to print individual characters and strings n c- requires a char argument n s- requires a pointer to char as an argument n
Common programming Error 1. Using %c to print a string n 2. Using %s to print a character n 3. Usingle quotes around character string is a syntax error. n 4. Using a double quotes around a character constant n
More about c in/out format %p --- Display a pointer value in an n implementation-defined manner. n %n --- store the number of characters already output in the current printf statement. (including all blanks, too) n %% Display the percent character. n
Example n n n n /* Using Integer Conversion Specifiers */ #include <stdio. h> int main ( ) { int *ptr; int x = 12345, y; ptr = &x; n n n n printf( "The value of ptr is %pn", ptr ); printf( "The address of x is %pnn", &x ); printf( "Total characters printed on this line is: %n", &y ); printf( " %dnn", y ); y = printf( "This line has 28 charactersn" ); printf( “%d characters were printednn", y); printf( "Printing a %% in a format control stringn" ); n n n return 0; } (week 04 spe 5. c)
More about numbers’ format n n n n %d 37 -37 %. 4 d 0037 – 0037 %11 d 37 -37 %11. 4 d 0037 -0037 %-11 37 -37 %-11. 4 0037 -0037 %011 d 0000037 -0000037 The default is right justified. By using ‘–’ right after % will make the field left justified.
Printing Literals and Escape Sequences n n n ’ ” ? \ a b f n r t v Cause an audible (bell) or visual alert. Move the cursor back one position on the current line. Move the cursor to the start of the next logical page. More the cursor to the beginning of the current line. More the cursor to the next horizontal tab position. Move the cursor to the next vertical tab position.
Program Example n n n n #include <stdio. h> int main ( ) { int month 1, day 1, year 1, month 2, day 2, year 2; printf( "Enter a date in the form mm-dd-yyyy: "); scanf( "%d%*c%d”, &month 1, &day 1, &year 1 ); printf( "month = %d day = %d year = %dn", month 2, day 2, year 2 ); printf( "Enter a date in the form mm/dd/yyyy: " ); scanf( "%d%*c%d", &month 2, &day 2, &year 2 ); printf( "month = %d day = %d year = %dn", month 2, day 2, year 2 ); return 0; } Notice the * in front of c% as an Assignment Suppression Character to discard characters from input. (week 04 spe 6. c)
Basic Arithmetic Operations n n n n n Example: (week 04 arith. c) #include <stdio. h> int main(void) { int a, b, c, d; printf("Enter an integer: "); scanf("%d", &a); b = a + 4; c = a - 3; d = -a; printf("b is %d, nc is %d, andnd is %d. nn", b, c, d); b = a * 3; c = a / 3; d = a % 3; printf("Nownb is %d, nc is %d, andnd is %d. n", b, c, d); return 0; }
Another Example n Week 04 ---- miles. c
n #include <stdio. h> n #define MILEAGE 28 n int main(void) { int gallons; printf(" Travel Calculatorn"); printf("-----------------n"); n n n n printf("Current mileage is %d miles per gallon. n", MILEAGE); printf("nn. Enter the gallons of gas (whole numbers please): "); scanf("%d", &gallons); printf("Thank you. You will be able to travel %d miles. n", miles( gallons, MILEAGE)); return 0; } /**************** miles() *************/ int miles( int num_gallons, int mileage ) { return ( num_gallons * mileage ); }
- Subtraction integer rules
- Inputoutput devices
- Include stdlib
- #include stdio.h #include stdlib.h #include string.h
- #include stdio.h #include conio.h #include stdlib.h
- Access specifiers in java
- Access specifiers in c++
- Include iostream using namespace std
- Namespace string
- #include iostream using namespace std
- #include iostream using namespace std
- Snmp supports which formatted protocol
- Apa first page