Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460 Input and Output Revisited. CGS 3460 Display Integer Variables – I #include Preprocessor: interact with input/output of your computer Start point.

Similar presentations


Presentation on theme: "CGS 3460 Input and Output Revisited. CGS 3460 Display Integer Variables – I #include Preprocessor: interact with input/output of your computer Start point."— Presentation transcript:

1 CGS 3460 Input and Output Revisited

2 CGS 3460 Display Integer Variables – I #include Preprocessor: interact with input/output of your computer Start point of the program int main() Start and finish of function {}{} Printing results int value1, value2, sum; Finish and return value 0 return 0; value1 = 50; value2 = 30; sum = value1 + value2; printf(“The sum of 50 and 30 is %i\n“, sum); Declare Variables Define Values Summation Print the value of an integer variable The sum of 50 and 30 is 80

3 CGS 3460 Display Integer Variables – II #include int main() { int value1, value2, sum; value1 = 50; value2 = 30; sum = value1 + value2; return 0; } printf(“The sum of %i and %i is %i\n“, value1, value2, sum); The sum of 50 and 30 is 80

4 CGS 3460 int: integer Variables – Special Case I Starting with digit “0” lOctal notation. Base 8, not 10 0,1,2,3,4,5,6,7 0177 = 1*64 + 7*8 + 7 = 127 0256 = ? lDisplay %i – print out the decimal value %o – print out the octal value %#o – print out the octal value, starting with 0

5 CGS 3460 int: integer Variables – Special Case II Starting with digit “0x” lHexadecimal notation. Based 16, not 10 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 0x177 = 1*256 + 7*16 + 7 = 375 0xAF = ? 0x2AF = ? lDisplay %i – print out the decimal value %x – print out the hexadecimal value %#x – print out the hexadecimal value, starting with 0

6 CGS 3460 float Variables - III n Display l%f – print out the decimal value l%e – print out the scientific notation l%g – let printf decide the format -4 < value of exponent < 5: %f format Otherwise: %e format

7 CGS 3460 char: character Variables n For single character lEnclosing the character within a pair of ‘ ’ ‘a’ ‘;’ ‘P’ ‘\n’ ‘1’ lOne Byte n Display l%c

8 CGS 3460 _Bool: Boolean Type n For boolean l0/1 lNormally means false/true l1 Byte long n Display l%i

9 CGS 3460 Special variable types n short lusually use less space ladd “h” after “%” in printf n long, long long lusually use more space luse “l” to indicate ladd “l” after “%” in printf n signed, unsigned lspecify whether is a signed quantity luse “u” to indicate unsigned l%u for unsigned

10 CGS 3460 Summary of Data Type TypeExamplesConversion Specification char‘a’, ‘\n’%c _Bool0, 1%i, %u short int1,100, -5%hi, %hx, %ho unsigned short int1, 39, 100%hu, %hx, %ho int -1, 5, 0XAF, 0177 %i, %x, %o unsigned int 5u, 0XAFu, 0177U %u, %x, %o long int0xffffL, 12l%li, %lx, %lo unsigned long int0xffffUL, 12ul%lu, %lx, %lo

11 CGS 3460 Summary of Data Type – cont. TypeExamplesPrintf long long int0xffffLL, 12ll%lli, %llx, %llo unsigned long long int0xffffULL, 12ull%llu, %llx, %llo float12.3f, 3.1e-5f, 0x1.5p10 %f, %e, %g %a double12.3, 3.1e-5, 0x1.5p10 %f, %e, %g %a long double12.3l, 3.1e-5l%Lf, %Le, %Lg

12 CGS 3460 Getting Input n Need input from user lscanf Same format as printf, except put “&” in front of variable names scanf(“%i”, &count); “&” means the "address of“ to store whatever the user enters into the memory address where number is stored Leaving out the & will cause your program to work incorrectly! Exception: double uses %lf in scanf and %f in printf

13 CGS 3460 Formatting Output n Sometimes you would like your output to have nice tabular output n Conversion specification (%i, %f, etc) allows for formatting text n Format : %[flags][width][.prec][hlL]type l[] mean its optional lOnly % and type are mandatory

14 CGS 3460 Flags FlagMeaning -Left – justify value +Precede value with + or - (space)Precede positive value with space character 0Zero fill numbers #Precede octal value with 0, hexadecimal value with 0x; display decimal point for floats; leave trailing zeroes for g or G format

15 CGS 3460 Width and Precision SpecifierMeaning numberMinimum size of field *Take next argument to printf as size of field.numberMinimum number of digits to display for integers; number of decimal places for e or f formats; maximum number of significant digits to display for g; maximum number of characters for s format.*Take next argument to printf as precision

16 CGS 3460 hlL – Type Modifiers TypeMeaning hhDisplay integer argument as a character hDisplay short integer lDisplay long integer llDisplay long long integer LDisplay long double

17 CGS 3460 Example 1 – Formatting Ints #include int main() { int i = 425; short int j = 17; unsigned int u = 0xf179U; long int l = 75000L; long long int L = 0x1234567812345678LL; printf("Integers:\n"); printf("%i, %o, %x, %u\n", i, i, i, i); printf("%x, %X, %#x, %#X\n", i, i, i, i); printf("%+i, %i, %5i, % 5i, %05i, %.7i\n", i, i, i, i, i, i); printf("%i, %o, %x, %u\n", j, j, j, j); printf("%i, %o, %x, %u\n", u, u, u, u); printf("%ld, %lo, %lx, %lu\n", l, l, l, l); printf("%lli, %llo, %llx, %llu\n", L, L, L, L); return 0; } Integers: 425, 651, 1a9, 425 1a9, 1A9, 0x1a9, 0X1A9 +425, 425, 425, 425, 00425, 0000425 17, 21, 11, 17 61817, 170571, f179, 61817 75000, 222370, 124f8, 75000 1311768465173141112, 110642547402215053170,... 1234567812345678, 1311768465173141112

18 CGS 3460 Example 2 – Formatting Floats #include int main() { float f = 12.978F; double d = -97.4583; printf("Floats & Doubles:\n"); printf("%f, %e, %g\n", f, f, f); printf("%.2f, %.2e\n", f, f); printf("%.0f, %.0e\n", f, f); printf("%7.2f, %7.2e\n", f, f); printf("%f, %e, %g\n", d, d, d); printf("%.*f\n", 3, d); printf("%*.*f\n", 8, 2, d); printf("%0*.*f\n", 8, 2, d); return 0; } Floats & Doubles: 12.978000, 1.297800e+01, 12.978 12.98, 1.30e+01 13, 1e+01 12.98, 1.30e+01 -97.458300, -9.745830e+01, -97.4583 -97.458 -97.46 -0097.46

19 CGS 3460 Example 3 – Formatting Characters #include int main() { char c = 'X'; printf("Characters:\n"); printf("%c\n", c); printf("%3c%3c\n", c, c); printf("%x\n", c); return 0; } Characters: X X X 58

20 CGS 3460 Example 4 – Formatting Strings #include int main() { char s[] = "abcdefghijklmnopqrstuvwxyz"; printf("Strings:\n"); printf("%s\n", s); printf("%.5s\n", s); printf("%30s\n", s); printf("%20.5s\n", s); printf("%-20.5s\n", s); return 0; } Strings: abcdefghijklmnopqrstuvwxyz abcde abcdefghijklmnopqrstuvwxyz abcde

21 CGS 3460 More on scanf n Takes multiple modifiers between % and conversion specifier n Modifiers tell different things n Conversion specifiers are pretty much the same as printf with a few exceptions

22 CGS 3460 scanf modifiers n * - field is to be skipped and not assigned n size – Maximum size of an input field n hh – stored in a signed or unsigned char n h – stored in a short int n l – value is to be stored in a long int n ll – value is to be stored in a long long int n L – value is to be stored in a long double

23 CGS 3460 Conversion Characters - scanf CharDescriptionArg Type (pointer to) dValue is read in decimal notation.int (unless h, l, ll modifer is used) iSame as d, except can read octal and hexint (unless h, l, ll modifer is used) uReads integerunsigned int o, xNumber is expressed in octal or hexadecimal respectively and can be lead with 0 or 0x respectively o- int, x – unsigned int (unless h, l, ll modifer is used) a, e, f, g Floating point number can be preceded by a sign or in scientific notation float (unless l, L modifer is used)

24 CGS 3460 Conversion Characters - scanf CharDescriptionArg Type (pointer to) cReads the next character (including w.s. chars) Sizespecifies the number of characters to read char sReads sequence of characters beginning with first non-w.s. and terminated by first w.s. character. (Size indicates a max number of chars to read) char[] of appropriate size […]Indicates a char[] is to be read (like %s); characters within brackets indicate permissible characters; String terminates as soon as non permissible character is reached. (Can be inverted by placing ^ first char[] of appropriate size

25 CGS 3460 scanf characteristics n Skips leading whitespace except when reading with %c or bracket specifier […] n Stop reading a value lField width is reached (if specified) lAn invalid character for that field type is reached n Non formatting characters in scanf string are expected in input lscanf(“%i,%i,%i”, &i, &j, &k); - Expects 3 numbers separated by commas lWhite space characters match an arbitrary number of whitespace characters on the input l% in input is specifed by %

26 CGS 3460 scanf(“%i:%i:%i”, &h, &m, &s); n Reads three ints n Stored in integers h, m and s n Colons separate each n 23:34:52

27 CGS 3460 scanf(“%i%”, &percentage); n Reads one int n Stored in percentage n % symbol follows the int n 23%

28 CGS 3460 Given input “29 w” n scanf(“%i%c”, &i, &c); li  29 lc  ‘ ‘ n scanf(“%i %c”, &i, &c); li  29 lc  ‘ w‘

29 CGS 3460 Given input “144abcde 736.55 (wine and cheese)” n scanf(“%i %5c %*f %s”, &i, text, string); li  144 ltext[]  “abcde” l736.55  Matched but skipped lstring[]  ”(wine” n scanf(“%s %s %i”, string2, string3, &i2); lstring2  and lstring3  cheese) li2  waits for an integer input from user

30 CGS 3460 More examples n scanf(“%80c”, text); lWaits for and reads next 80 characters and stores them in text n scanf(“%[^\n]\n”, buf); lReads entire line into buf (excluding the new line character)

31 CGS 3460 scanf - return n Returns the number of read AND assigned in the function call lscanf(“%i, %f, %i”, &i, &g, &j); returns 3 on successful read and assignment lscanf(“%i, %*f, %i”, &i, &j); returns 2 on successful read and assignment n Use for error checking if(scanf(“%i, %f, %i”, &i, &g, &j) != 3) { printf(“Error\n”); }


Download ppt "CGS 3460 Input and Output Revisited. CGS 3460 Display Integer Variables – I #include Preprocessor: interact with input/output of your computer Start point."

Similar presentations


Ads by Google