1 / 31

Input and Output Revisited

Input and Output Revisited. Print the value of an integer variable. Display Integer Variables – I. Preprocessor: interact with input/output of your computer. #include <stdio.h>. int main(). Start point of the program. { }. Start and finish of function. int value1, value2, sum;.

Download Presentation

Input and Output Revisited

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Input and Output Revisited

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

  3. Display Integer Variables – II #include <stdio.h> 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. int: integer Variables – Special Case I Starting with digit “0” • Octal notation. Base 8, not 10 • 0,1,2,3,4,5,6,7 0177 = 1*64 + 7*8 + 7 = 127 0256 = ? • Display • %i – print out the decimal value • %o – print out the octal value • %#o – print out the octal value, starting with 0

  5. int: integer Variables – Special Case II Starting with digit “0x” • Hexadecimal 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 = ? • Display • %i – print out the decimal value • %x – print out the hexadecimal value • %#x – print out the hexadecimal value, starting with 0

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

  7. char: character Variables • For single character • Enclosing the character within a pair of ‘ ’ • ‘a’ • ‘;’ • ‘P’ • ‘\n’ • ‘1’ • One Byte • Display • %c

  8. _Bool: Boolean Type • For boolean • 0/1 • Normally means false/true • 1 Byte long • Display • %i

  9. Special variable types • short • usually use less space • add “h” after “%” in printf • long, long long • usually use more space • use “l” to indicate • add “l” after “%” in printf • signed, unsigned • specify whether is a signed quantity • use “u” to indicate unsigned • %u for unsigned

  10. Summary of Data Type

  11. Summary of Data Type – cont.

  12. Getting Input • Need input from user • scanf • 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. Formatting Output • Sometimes you would like your output to have nice tabular output • Conversion specification (%i, %f, etc) allows for formatting text • Format : %[flags][width][.prec][hlL]type • [] mean its optional • Only % and type are mandatory

  14. Flags

  15. Width and Precision

  16. hlL – Type Modifiers

  17. Example 1 – Formatting Ints 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 #include <stdio.h> 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; }

  18. Example 2 – Formatting Floats 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 #include <stdio.h> 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; }

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

  20. Example 4 – Formatting Strings #include <stdio.h> 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 abcde

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

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

  23. Conversion Characters - scanf

  24. Conversion Characters - scanf

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

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

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

  28. Given input “29 w” • scanf(“%i%c”, &i, &c); • i  29 • c  ‘ ‘ • scanf(“%i %c”, &i, &c); • i  29 • c  ‘ w‘

  29. Given input “144abcde 736.55 (wine and cheese)” • scanf(“%i %5c %*f %s”, &i, text, string); • i  144 • text[]  “abcde” • 736.55 Matched but skipped • string[] ”(wine” • scanf(“%s %s %i”, string2, string3, &i2); • string2 and • string3  cheese) • i2  waits for an integer input from user

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

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

More Related