Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.

Similar presentations


Presentation on theme: "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4."— Presentation transcript:

1 CS113 Introduction to C Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.eduvetsikas@cs.cornell.edu Lecture 5 : September 4

2 2 The Data Type char The data type char can be thought of as either a character or an integer. Typically, a char has a value 0-255. printf( “%c”, ‘a’ ); /* a is printed */ printf( “%d”, ‘a’ ); /* 97 is printed */ printf( “%c”, 97 ); /* a is printed */ ‘a’ == 97, ‘b’ == 98, …, ‘z’ == 112 ‘A’ == 65, ‘B’ == 66, …, ‘Z’ = 90 ‘0’ == 48, ‘1’ == 49, …, ’9’ == 57 ‘&’ == 38, ‘*’ == 42, …

3 3 Escape codes corresponding to characters For use inside in single-quotes, or double-quotes, for instance in passing a string to printf Exhaustive list in K&R (p. 193) Character Escape Sequence Integer Value Newline \n 10 Backslash (\) \\ 92 Backspace \b 8 Single quote \’ 39 Double quote \” 34 Horizontal tab \t 9 Question Mark \? 63 Null Character \0 0

4 4 Strings Strings are one-dimensional arrays of type char. Hence, they have type char *. By convention, a string in C is terminated by \0 (null character); thus it needs space equal to the size of string +1 A string constant is treated by the compiler as a pointer; also space in memory is allocated for storing the characters. When allocating char arrays that will hold strings, make sure you allocate enough space! char *p = “abc”; printf( “%s %s\n”, p, p+1 ); /* prints abc bc */ /* “abc”[1], *(“abc” + 2) make sense */ CornellUni\0 01234567891011

5 5 Example: “Double” printing #include void new_print( char *s ) { int i; for( i = 0; s[i] != 0; i++ ) { printf( "%c%c", s[i], s[i] ); } void main() { new_print( “Cornell" ); }

6 6 Strings are also char pointers #include void new_print( char *s ) { while (*s) { printf( "%c%c", *s, *s ); s++; } void main() { new_print( “Cornell" ); }

7 7 Some standard string handling functions These are included in library “string.h” char *strcat( char *s1, char *s2 ); –Takes two strings as arguments, concatenates them, and puts the result in s1. The programmer must ensure that s1 points to enough space to hold the result. The string s1 is returned. int strcmp( char *s1, char *s2 ); –Integer is returned that is less than, equal to, or greater than zero, depending on whether s1 is lexicographically less than, equal to, or greater than s2. char *strcpy( char *s1, char *s2 ); –The string s2 is copied into s1. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The value s1 is returned. Implementation for some exist in K&R, but you try implementing them yourselves!

8 8 Example: “squeeze” function (from K&R) /* squeeze deletes all instances of c from s */ void squeeze( char s[], int c ) { int i, j; for( i = j = 0; s[i] != ‘\0’; i++ ) { if( s[i] != c ) { s[j] = s[i]; j++; } s[j] = ‘\0’; }

9 9 Multidimensional Arrays Declaration such as –int b[2][7]; or int c[5][3][2]; In first example, makes 14 ints available for use: –b[i][j] where 0 <= i <= 1, 0 <= j <= 6. #define SIZE 6 void clear_board( int board[SIZE][SIZE] ) { int i, j; for( i = 0; i < SIZE; i++ ) { for( j = 0; j < SIZE; j++ ) { board[i][j] = 0; }

10 10 Initializations int year[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; float x[4] = {1, }; (or float x[4]={1}; ) /* Initialization of rest to 0’s by default */ char uni[20] = “Cornell University”; char uni[] = “Cornell University”; char *uni = “Cornell University”; What is the difference between the three? float y[2][3] = { {1, 2, 3}, {2, 4, 6}}; float y[2][3] = { 1, 2, 3, 2, 4, 6}; /*same*/ What is the difference between the three? float y[2][3] = { {1}, {2, 4}}; float y[2][3] = { 1, 2, 4}; /*same? NO!*/


Download ppt "CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4."

Similar presentations


Ads by Google