10/6/2015

C Input/Output stdin, stdout, stderr

C opens three input/output devices automatically:

stdin The "standard input" device, usually your keyboard C Language II stdout The "standard output" device, usually your monitor stderr The "standard error" device, usually your monitor CMSC 313

Sections 01, 02 Some C I/O functions automatically use these devices

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

1 10/6/2015

Formatted Console Output printf( ) Conversions Common printf( ) Conversions

• printf( ) outputs formatted text to stdout Conversions specifications begin with % and end with a Spec Conversion performed conversion character. % print integer as a decimal number (base 10) printf( format, arg1, arg2, … ); Between the % and the conversion character MAY be, in %u print integer as unsigned number order A minus sign specifying left-justification • Example: %s print string int n = 3 ; The minimum field width printf (“Value = %d\n”, n) ; A period separating the field width and %f print double as a floating point number precision The precision that specifies %x print integer in (base 16) Maximum characters for a string • format is a string containing Number of digits after the decimal for a floating %c print integer as ASCII character • conversion specifications point Minimum number of digits for an integer • literals to be printed %p An h for "short" or an l (letter ell) for long print pointer in hexadecimal (implementation dependent)

man printf for more documentation.

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

2 10/6/2015

printf( ) Examples Formatted Output Example Keyboard Input

• scanf reads user input from stdin. int anInt = 5678; double aDouble = 4.123; Use field widths to align output in columns • Syntax for scanf( ) is similar to printf( ) scanf( int i; #define NAME "Bob" format, arg1, arg2, ... ) for (i = 1 ; i < 5; i++) printf("%2d %10.6f %20.15f\n", i, sqrt(i), sqrt(i)); • The format string similar structure to printf( ). /* what is the output from each printf( ) */ printf("%d is a large number\n", anInt); • The arguments must be addresses of the variables. 12 1234567890 12345678901234567890 printf("%8d is a large number\n", anInt); 1 1.000000 1.000000000000000 printf("%-8d is a large number\n", anInt); 2 1.414214 1.414213562373095 printf("%10.2f is a double\n", aDouble); 3 1.732051 1.732050807568877 printf("The sum of %d and %8.4f is %12.2f\n", 4 2.000000 2.000000000000000 anInt, aDouble, anInt + aDouble); printf ("Hello %s\n", NAME);

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

3 10/6/2015

scanf( ) Format String Common scanf( ) Conversions scanf( ) Examples

Spec Conversion performed • The scanf( ) format string usually contains int age; %d a decimal (integer) number conversion specifications that tell scanf( ) how to double gpa; interpret the next "input field". An input field is a %u An unsigned decimal (integer) number char initial; string of non-whitespace characters. %x a hexadecimal number • The format string usually contains: printf("input your middle initial: "); %f – Blanks or tabs which are ignored a floating point number with optional sign, scanf("%c", &initial); // Note & decimal point, and exponent – Ordinary characters which are expected to match the next printf("Input your age: "); (non- whitespace) character input by the user %s a string delimited by white space, NOT an scanf("%d", &age); – Conversion specifications usually consisting entire line printf("input your gpa: "); • % character indicating the beginning of the conversion %c a single character (possibly a whitespace char) scanf("%lf", &gpa); • An optional h, l (ell) or L • A conversion character which indicates how the input field is to be interpreted.

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

4 10/6/2015

Unix I/O redirection Text File I/O fopen()

• Redirect input (read from infile instead of keyboard): a.out < infile • Use fprintf() and fscanf() functions instead of fopen( ) requires two parameters printf() and scanf(). 1. The name of the text file to be opened • Redirect output (write to outfile instead of screen): • Must open file before reading/writing: fopen() a.out > outfile 2. The text file open "mode" • Must close file after all done: fclose() “r” open the file for reading only

• Redirect both: • Use file handle to specify file. “w” create the file for writing; delete existing file a.out < infile > outfile • File handle returned by fopen(): “a” append; open or create the file for writing at the end FILE *myFile ; • Redirect stdout and stderr to outfile “r+” open the file for reading and writing myFile = fopen (“bob.txt”, “r”) ; a.out >& outfile if (myFile == NULL) { “w+” create the file for reading & writing; deletes existing file /* handle the error */ “a+” open or create the file for reading or writing at the end • Redirect stdout to outfile and stderr to errfile } (a.out > outfile) >& errfile

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

5 10/6/2015

fscanf.c Detecting end-of-file with fscanf EOF Example Code

#include #include /* for “exit” */ • When reading an unknown number of data elements from a /* code snippet that reads an undetermined number of int main ( ) file using fscanf( ), we need a way to determine when integer student ages from a file and prints them out { the file has no more data to read, i.e, we have reached the as an example of detecting EOF (scanfEOF.c) double x ; "end of file". */ FILE *ifp ; FILE *inFile; int age; /* try to open the file for reading, check if successful */ /* if it wasn't opened exit gracefully */ • Fortunately, the return value from fscanf( ) holds the ifp = fopen("test_data.dat", "r") ; key. fscanf( ) returns an integer which is the number inFile = fopen( “myfile”, “r” ); if (ifp == NULL) { of data elements read from the file. If end-of-file is if (inFile == NULL) { printf ("Error opening test_data.dat\n"); detected the integer return value is the special value EOF printf ("Error opening myFile\n"); exit (-1); exit (-1); } } fscanf(ifp, "%lf", &x) ; /* read one double from the file */ fclose(ifp); /* close the file when finished */ while ( fscanf(inFile, “%d”, &age ) != EOF ) /* check to see what you read */ printf( “%d\n”, age ); printf("x = %.2f\n", x) ; return 0; fclose( inFile ); }

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

6 10/6/2015

fprintf.c Characters & Strings char Type

/* fprintf.c */ • C supports the char for storing a single #include #include /* exit */ character. int main ( ) { • char uses one byte of memory double pi = 3.14159 ; FILE *ofp ; • char constants are enclosed in single quotes

/* try to open the file for writing, check if successful */ char myGrade = ‘A’; /* if it wasn't exit gracefully */ char yourGrade = ‘?’; ofp = fopen("test.out", “w") ; if (ofp == NULL) { printf ("Error opening test.out\n"); exit (-1); }

/* write to the file using printf formats */ fprintf(ofp, “Hello World\n”); fprintf(ofp, “PI is defined as %6.5lf\n”, pi);

fclose(ofp); /* close the file when finished reading */

return 0; }

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

7 10/6/2015

Special Characters Special Char Example Code Character Library Functions

Use \ for escape sequences. • What is the output from these statements? int isdigit (int c); Determine if c is a decimal digit (' 0' - ' 9' ) For example printf("\t\tMove over\n\nWorld, here I come\n"); int isxdigit(int c); \n is the newline character Determines if c is a hexadecimal digit (' 0' - ' 9' , ' a' - f' , or ' A' - ' F' ) \t is the tab character Move over int isalpha (int c); \" is the double quote (necessary since double quotes are used to enclose strings Determines if c is an alphabetic character (' a' - ' z' or ' A- ' Z' ) World, here I come \' is the single quote (necessary since single quotes are int isspace (int c); used to enclose chars Determines if c is a whitespace character (space, tab, etc) printf("I\' ve written \"Hello World\"\n\t many times\n\a"); \\ is the backslash (necessary since \ now has special int isprint (int c); meaning Determines if c is a printable character \a is beep which is unprintable I' ve written "Hello World“ many times int tolower (int c); int toupper (int c); Returns c changed to lower- or upper-case respectively, if possible

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

8 10/6/2015

Character Library Functions Character Input/Output Strings in C

• Use %c in printf( )and fprintf( )to output a single • String = null terminated array of char. Include header file use character library functions: character. char yourGrade = ‘A’; • null = '\0' #include printf( “Your grade is %c\n”, yourGrade); • String constants in double quotes are null terminated. Technically functions take an int parameter, not char. • Input char(s) using %c with scanf( ) or fscanf( ) • Strings do not "know" their own length. Return type is also int . 0 = False, not 0 = True. char grade, scores[3]; • Initialization: char name4[20] = {‘B’, ‘o’, ‘b’, ‘b’, ‘y’, ‘\0’}; – %c inputs the next character, which may be whitespace man ctype.h for more functions and complete documentation. scanf(“%c”, &grade); char name5[6] = “Bobby”; // this is NOT assignment – %nc inputs the next n characters, which may include whitespace char name6[ ] = “Bobby”; scanf(“%3c”, scores); // note – no & needed

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

9 10/6/2015

String Output Dangerous String Input Safer String Input char name[ ] = “Bobby Smith”; printf( “My name is %s\n”, name); • Why is the following dangerous? char name[22]; printf(“ Enter your name: “); char book1[ ] = “Flatland”; char book2[ ] = “Brave New World”; char name[22]; scanf( “%21s”, name); // Will stop after 21 (note: not 22) chars printf(“ Enter your name: “); // (need last byte for ‘\0’ // Minimum field width, and right- and left-justify scanf( “%s”, name); printf (“My favorite books are %12s and %12s\n”, book1, book2); printf (“My favorite books are %-12s and %-12s\n”, book1, book2); • Long names will overwrite memory.

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

10 10/6/2015

C String Library C String Library (2) C String Library (3)

• Must #include • Some safer versions from the C string library have • C provides a library of string functions. • These functions look for the ‘\0’ character to determine the end an additional size parameter. • To use the string functions, include . and size of the string – strncpy( char s1[ ], const char s2[ ], int n ) – strlen( const char string[ ] ) • Some of the more common functions are listed • Copies at most n characters of s2 on top of s1. • Returns length of string, not counting ‘\0’ • Does not null terminate s1 if length of s2 >= n !!! here on the next slides. – strcpy( char s1[ ], const char s2[ ] ) • The order of the parameters mimics the assignment operator • To see all the string functions, type • Copies s2 on top of s1. Must have enough space in s1!!! man string.h at the unix prompt. • The order of the parameters mimics the assignment operator – strncmp ( const char s1[ ] , const char s2[ ], int n ) – strcmp ( const char s1[ ] , const char s2[ ] ) • Compares up to n characters of s1 with s2 • Returns < 0, 0, > 0 if s1 < s2, s1 == s2 or s1 > s2 lexigraphically • Returns < 0, 0, > 0 if s1 < s2, s1 == s2 or s1 > s2 lexigraphically – strcat( char s1[ ] , const char s2[ ]) – strncat( char s1[ ], const char s2[ ] , int n) • Appends (concatenates) s2 to s1. Must have enough space in s1!!! • Appends at most n characters of s2 to s1

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

11 10/6/2015

String Code Simple Encryption Arrays of Strings

char c, msg[] = "this is a secret message"; • An initialized array of string constants char first[10] = “bobby”; int i = 0; char months[ ][4] = { char last[15] = “smith”; char code[26] = /* Initialize our encryption code */ “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, char name[30]; {'t','f','h','x','q','j','e','m','u','p','i','d','c', “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec” char you[ ] = “bobo”; 'k','v','b','a','o','l','r','z','w','g','n','s','y'} ; }; strcpy( name, first ); printf ("Original phrase: %s\n", msg); int m; strcat( name, last ); for ( m = 0; m < 12; m++ ) printf( “%d, %s\n”, strlen(name), name ); /* Encrypt */ printf( “%s\n”, months[ m ] ); while( msg[i] != '\0‘ ){ strncpy( name, last, 2 ); if( isalpha( msg[ i ] ) ) { printf( “%d, %s\n”, strlen(name), name ); c = tolower( msg[ i ] ) ; • Alternative: use typedef msg[ i ] = code[ c - ‘a’ ] ; typedef char Acronym[4]; int result = strcmp( you, first ); } Acronym months[ ] = { result = strncmp( you, first, 3 ); ++i; “Jan”, “Feb”, “Mar”, “Apr”, “May”, “Jun”, strcat( first, last ); } “Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec” printf("Encrypted: %s\n", msg ) ; };

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

12 10/6/2015

sprintf() Structs C++ vs C

• sprintf( ) works just like printf( ) or fprintf( ), but puts its • Suppose you were assigned a write an application “output” into the specified character array. about points and straight lines in a coordinate plane. • The character array must be big enough. • In C++, you’d correctly design a Point class and a Line class using composition. char message[ 100 ]; • What about in C? int myAge = 4; sprintf( message, “I am %d years old\n”, age); printf( “%s\n”, message);

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

13 10/6/2015

No Classes in C No Classes in C struct Definition

• The general form of a structure definition is • Because C is not an OOP language, there is no struct tag way to combine data and code into a single • Because C is not an OOP language, there is no way to Note the semi-colon { entity. combine data and code into a single entity. • Related data and functions form an "Abstract Data Type." member1_declaration; • C does allow us to combine related data into a member2_declaration; structure using the keyword struct. Accessibility is enforced by a programmer's good judgment and not by the compiler. member3_declaration; • All data in a struct variable can be accessed by . . . • C does allow us to combine related data into a structure any code. memberN_declaration; using the keyword struct. • Think of a struct as an OOP class in which all }; • All data in a struct variable can be accessed by any data members are public, and which has no where struct is the keyword, tag names this kind of struct, code. methods. and member_declarations are variable declarations • Think of a struct as an OOP class in which all data which define the members. members are public, and which has no methods.

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

14 10/6/2015

C struct Example Using struct Members Initializing a struct

• Defining a struct to represent a point in a coordinate plane int main ( ) struct point middle = { 6, -3 }; { struct point point is the struct tag struct point leftEendPt, rightEndPt, newEndPt; { printf(“Left end point cooridinates “); is equivalent to: int x; /* x-coordinate */ scanf( “%d %d”, &leftEendPt.x, &leftEndPt.y); int y; /* y-coordinate */ }; printf(“Right end point’s x-coordinate: “); struct point middle; scanf( “%d %d”, &rightEendPt.x, &rightEndPt.y); middle.x = 6; middle.y = -3; • Given the declarations // add the endpoints struct point p1; newEndPt.x = leftEndPt.x + rightEndPt.x; struct point p2; newEndPt.y = leftEndPt.y + rightEndPt.y; we can access the members of these struct variables: // print new end point * the x-coordinate of p1 is printf(“New endpoint (%2d, %2d)\n”, newEndPt.x, newEndPt.y); * the y-coordinate of p1 is * the x-coordinate of p2 is return 0; * the y-coordinate of p2 is }

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

15 10/6/2015

struct Variants struct + typedef struct Assignment

typedef struct point { struct point p1; struct point { int x, y; struct point p2; } POINT; int x, y; p1.x = 42; } endpoint, upperLeft; p1.y = 59; defines the structure named point and defines POINT as a typedef (type alias) for the struct. p2 = p1; /* structure assignment copies members */ defines the structure named point POINT upperRight; The values of p2’s members are the same as p1’s members. AND E.g. p1.x = p2.x = 42 and p1.y = p2.y = 59 is now equivalent to: declares the variables endpoint and upperLeft to be struct point endpoint; of this type.

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

16 10/6/2015

struct within a struct Arrays of struct Arrays within a struct typedef struct line LINE lines[5]; // or struct line lines[5] • Structs may contain arrays as well as primitive types { POINT leftEndPoint; printf(“%d\n”, lines[2].leftEndPoint.x); struct month POINT rightEndPoint; { } LINE; int nrDays; char name[ 3 + 1 ]; LINE line1, line2; line1.leftEndPoint.x = 3; }; line1.leftEndPoint.y = 4; struct month january = { 31, “JAN”};

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

17 10/6/2015

A Bit More Complex Size of a struct struct month allMonths[ 12 ] = { • As with primitive types, we can use sizeof( ) to {31, “JAN”}, {28, “FEB”}, {31, “MAR”}, determine the number of bytes in a struct {30, “APR”}, {31, “MAY”}, {30, “JUN”}, {31, “JUL”}, {31, “AUG”}, {30, “SEP”}, int pointSize = sizeof( POINT ); {31, “OCT”}, {30, “NOV”}, {31, “DEC”} }; int lineSize = sizeof (struct line); // write the code to print the data for September printf( “%s has %d days\n”, As we’ll see later, the answers may surprise you! allMonths[8].name, allMonths[8].nrDays);

// what is the value of allMonths[3].name[1]?

Adapted from Richard Chang, CMSC 313 Spring 2013 Adapted from Richard Chang, CMSC 313 Spring 2013

18