31
CGS 3460 Input and Output Revisited

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

Embed Size (px)

Citation preview

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

     

CGS 3460

Input and Output RevisitedInput and Output Revisited

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

     

CGS 3460

Display Integer Variables – IDisplay Integer Variables – I#include <stdio.h>

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 variableThe sum of 50 and 30 is 80

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

     

CGS 3460

Display Integer Variables – IIDisplay 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

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

     

CGS 3460

int: integer Variables – Special Case I

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

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

     

CGS 3460

int: integer Variables – Special Case II

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

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

     

CGS 3460

float Variables - IIIfloat 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

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

     

CGS 3460

char: character Variableschar: character Variables For single character

Enclosing the character within a pair of ‘ ’• ‘a’• ‘;’• ‘P’• ‘\n’• ‘1’

One Byte

Display %c

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

     

CGS 3460

_Bool: Boolean Type_Bool: Boolean Type For boolean

0/1 Normally means false/true 1 Byte long

Display %i

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

     

CGS 3460

Special variable typesSpecial 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

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

     

CGS 3460

Summary of Data TypeSummary of Data TypeType Examples Conversion

Specificationchar ‘a’, ‘\n’ %c

_Bool 0, 1 %i, %u

short int 1,100, -5 %hi, %hx, %ho

unsigned short int 1, 39, 100 %hu, %hx, %ho

int -1, 5, 0XAF, 0177 %i, %x, %o

unsigned int 5u, 0XAFu, 0177U %u, %x, %o

long int 0xffffL, 12l %li, %lx, %lo

unsigned long int 0xffffUL, 12ul %lu, %lx, %lo

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

     

CGS 3460

Summary of Data Type – cont.Summary of Data Type – cont.Type Examples Printf

long long int 0xffffLL, 12ll %lli, %llx, %llo

unsigned long long int 0xffffULL, 12ull %llu, %llx, %llo

float 12.3f, 3.1e-5f, 0x1.5p10

%f, %e, %g

%a

double 12.3, 3.1e-5, 0x1.5p10

%f, %e, %g

%a

long double 12.3l, 3.1e-5l %Lf, %Le, %Lg

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

     

CGS 3460

Getting InputGetting 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

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

     

CGS 3460

Formatting OutputFormatting 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

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

     

CGS 3460

FlagsFlags

Flag Meaning- Left – justify value

+ Precede value with + or -

(space) Precede positive value with space character

0 Zero fill numbers

# Precede octal value with 0, hexadecimal value with 0x; display decimal point for floats; leave trailing zeroes for g or G format

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

     

CGS 3460

Width and PrecisionWidth and Precision

Specifier Meaningnumber Minimum size of field

* Take next argument to printf as size of field

.number Minimum 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

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

     

CGS 3460

hlL – Type ModifiershlL – Type Modifiers

Type Meaninghh Display integer argument as a character

h Display short integer

l Display long integer

ll Display long long integer

L Display long double

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

     

CGS 3460

Example 1 – Formatting IntsExample 1 – Formatting Ints#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;}

Integers:425, 651, 1a9, 4251a9, 1A9, 0x1a9, 0X1A9+425, 425, 425, 425, 00425, 000042517, 21, 11, 1761817, 170571, f179, 6181775000, 222370, 124f8, 750001311768465173141112, 110642547402215053170, ...1234567812345678, 1311768465173141112

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

     

CGS 3460

Example 2 – Formatting FloatsExample 2 – Formatting Floats#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;}

Floats & Doubles:12.978000, 1.297800e+01, 12.97812.98, 1.30e+0113, 1e+01 12.98, 1.30e+01-97.458300, -9.745830e+01, -97.4583-97.458 -97.46-0097.46

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

     

CGS 3460

Example 3 – Formatting CharactersExample 3 – Formatting Characters

#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;}

Characters:X X X58

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

     

CGS 3460

Example 4 – Formatting StringsExample 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:abcdefghijklmnopqrstuvwxyzabcde abcdefghijklmnopqrstuvwxyz abcdeabcde

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

     

CGS 3460

More on scanfMore 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

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

     

CGS 3460

scanf modifiersscanf 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

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

     

CGS 3460

Conversion Characters - scanfConversion Characters - scanfChar Description Arg Type (pointer to)

d Value is read in decimal notation. int (unless h, l, ll modifer is used)

i Same as d, except can read octal and hex int (unless h, l, ll modifer is used)

u Reads integer unsigned int

o, x Number 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)

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

     

CGS 3460

Conversion Characters - scanfConversion Characters - scanfChar Description Arg Type (pointer to)

c Reads the next character (including w.s. chars) Sizespecifies the number of characters to read

char

s Reads 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

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

     

CGS 3460

scanf characteristicsscanf 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 %%

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

     

CGS 3460

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

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

     

CGS 3460

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

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

     

CGS 3460

Given input “29 w”Given input “29 w” scanf(“%i%c”, &i, &c);

i 29 c ‘ ‘

scanf(“%i %c”, &i, &c); i 29 c ‘ w‘

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

     

CGS 3460

Given input “144abcde 736.55 (wine and cheese)”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

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

     

CGS 3460

More examplesMore 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)

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

     

CGS 3460

scanf - returnscanf - 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 checkingif(scanf(“%i, %f, %i”, &i, &g, &j) != 3)

{

printf(“Error\n”);

}