The C Programming Language (Lecture Notes

)

The C Programming Language - Lecture Notes

The C Programming Language

Roman Podraza Lecture Notes

p. 1

The C Programming Language - Lecture Notes

Bibliography
u u

u

B.W. Kernighan, D.M. Ritchie, “The C Programming Language”, Second Edition, Prentice Hall Inc., Englewood Cliffs, NJ, 1988. B.S. Gottfried, “Schaum’s Outline of Theory and Problems of Programming with C”, Schaum’s Outline Series in Computers, McGraw-Hill Publishing Company, 1990. H. Schildt, „C++: The Complete Reference, Fourth Edition”, McGraw-Hill/Osborne, 2003.

p. 2

1

The C Programming Language (Lecture Notes)

The C Programming Language - Lecture Notes

Introduction
u The

C Language Elements Comments
/* This is a comment. A set of any characters */

Identifiers
Variable_Name_2

Keywords are reserved
if, for, while
u Basic

Data Types
-128:127)

char (0:255, int float double short int long int long double unsigned int signed char unsigned long

short long

int

p. 3

The C Programming Language - Lecture Notes

u Literals

Integer constants
1234, 234l, 534L, 67u, 1092UL

Floating point number constants
154.3, 23e-43, 231.0L 154.3f, 23e-4F

Octal constants
077

Hexadecimal constants
0xAE77, 0X12

Strings
"any text in quotas"

Character constants
'A', '\x41', '\101', 65, '\0' special characters: \a, \b, \f, \n, \r, \t, \v, \\, \?, \', \"
u Defining

a constant with a preprocessor directive
10

#define DIMENSION

p. 4

2

The C Programming Language (Lecture Notes)

The C Programming Language - Lecture Notes

u Enumerations

enum boolean {NO, YES}; enum days {MONDAY=1, TUESDAY, WEDNESDAY};
u Variable

declarations

int a, b, c; unsigned int = 0xA2; signed int d; char e, f[10]; short sh; long int lint; float ff; double db = 2.0; long double ldb; const float pi = 3.14f; volatile long vollon = 1L; volatile const int icv = 0;

p. 5

The C Programming Language - Lecture Notes

Operators
u Arithmetic:

+, *, /, -, %
u Relational:

(modulo only for positive integers)

>, >=, <, <=, ==, !=

give results equal to 1(TRUE) or 0 (FALSE) u Logical: && (and), || (or) - Evaluated always from left hand side until the result is determined ! (not). u Casting:
int i = (int) (2.5 * 3.61);
u Incrementation

and decrementation:

++, -int i, j; i = 7; j = i++; j = ++i;
u Bit

operators: & (and), | (or), ^ (exor), << (shift left), >> (shift right), ~ (one's complement).

p. 6

3

The C Programming Language (Lecture Notes)

The C Programming Language - Lecture Notes

u Assignment: =, op= (op: +, -, *, /, %, >>, <<, &, |, ^)

i i i i

= 5 = j = 6 -=2 += j = 4

/* i = (j = 6) */ /* i = i - 2 */ /* i += (j = 4) */

u Conditional operator:

expression1? expression2 : expression3 c = (a > b) ? a : b
u Operator

, (comma)

i = (j=5, ++j)
u

Priorities and evaluation order Table of priorities and associativness of operators Priorities of operators in expressions can be defined (changed) by parenthesis
i = (2 + 3) * 16

u Only

for the following operators: &&, ||, , (comma) and conditional operator the evaluation order is defined from the left hand side to the right hand side. For all other operators the evaluation order is not defined (and is implementation dependent).
p. 7

The C Programming Language - Lecture Notes

u

Example OP.1 int i = 8, j = 2; i = ++j * j++; /* i = 8 i = 9 ? ? */

u

Example OP.2 int i = 0, j = 0, k = 0; i++ && j++ || k++;

p. 8

4

} p. u Loops Instruction while while (expression) instruction Instruction do .while do instruction while (expression).The C Programming Language (Lecture Notes) The C Programming Language . i++.Lecture Notes u Example INSTR.1 if ( a > b ) x = a. 10 5 . is not used after } u Instruction if else if (expression) instruction_1 else // optional part instruction _2 if (expression_1) instruction_1 else if (expression_2) instruction_2 . 9 The C Programming Language . a = 5. else // optional part instruction_n+1 p. expression_2. else x = b. if ( x < 0 ) x = -x. u Compound instruction (a block) { declarations instructions } a hint: .. expression_3) instruction expression_1. Instruction for for (expression_1. while (expression_2) { instruction expression_3..Lecture Notes Instructions u Any expression followed by the semicolon is an instruction (a sentence) expression .

i++. .). } p. /* skip only within a function */ u Instruction switch switch (expression) { case const_expression_1: instructions_1 case const_expression_2: instructions_2 . i++) /* execute n times*/ . high = n-1. for (i = 0. else if (x > v[mid]) low = mid + 1.identifier followed by ':' */ . j++) tab[i][j]++.. } return (-1). 11 The C Programming Language . default: instructions } Values of constant expressions have to be unique p.The C Programming Language (Lecture Notes) The C Programming Language .3 for (i = 0. else return mid..1. while (low <= high) { mid = (low + high) / 2. /* label .. Example INSTR. i < n.. i < 10 && j < 10..5 int binsearch (int x. label: i = 5. int i. /* infinite loop */ for (. Example INSTR.2 while (1). int n) { int low = 0.. 12 6 .Lecture Notes u u u u Example INSTR...4 int tab[10][10].... int v[]. j = 0. if (x < v[mid]) high = mid .. mid..Lecture Notes u Instruction break leave the loop (the given level) terminate execution of instruction switch u Instruction continue skip to (or after) the end of the loop body u Instruction goto skip to the label goto label. /* infinite loop */ Example INSTR.

break.Lecture Notes u Example INSTR. for (i = 0. default: nother++. ndigit[i]). 14 7 . while ( (c = getchar() ) != EOF ) { switch ( c ) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++. nother. } } p. for (i = 0. i++) printf (" %d". nother). i. p. return 0. 13 The C Programming Language .6 #include <stdio. ndigit[10]. ++i) ndigit[i] = 0. printf (". case ' ': case '\t': case '\n': nwhite++. nwhite. nwhite = nother = 0.The C Programming Language (Lecture Notes) The C Programming Language . } u Instruction return return a control from a function return expression. other = %d\n". i < 10.h> int main(void) { int c. nwhite. break.Lecture Notes printf ("digits = "). white spaces = %d. i < 10. break.

Lecture Notes FUN. 16 8 . break. double pop(void).2 /* Calculator of floating point numbers working according to Reverse Polish Notation */ #include <stdio.Lecture Notes Functions and Program Structure u Typical structure of source program (code) #include #define function prototypes external declarations functions u Syntax of functions return_type function_name (parameters) { declarations instructions } u Example FUN.The C Programming Language (Lecture Notes) The C Programming Language . while ( (type = getop(s)) != EOF ) { switch (type) { case NUMBER: push (atof(s)). break.1 void dummy (void) { } p. double op2. case '+': push (pop() + pop()). int main (void) { int type. 15 The C Programming Language .h> #define MAXOP #define NUMBER 100 '0' u Example int getop (char[]). p. char s [MAXOP].h> #include <math. void push (double).

void ungetch (int). } double pop (void) { if (sp > 0) return val[--sp]. double val[MAXVAL].*/ #define MAXVAL 100 int sp = 0. else printf ("error: /0. break. } /* ------------------------------------. case '*': p. break. c. while ( (s[0] = c = getch()) == ' ' || c == '\t') . case '/': op2 = pop(). else printf ("error: stack full\n"). if (op2 != 0. 18 9 .Lecture Notes void push (double f) { if (sp < MAXVAL) val[sp++] = f. default: printf ("error: unknown" command: %s\n". break.0) push (pop() / op2).') return c. s). break. pop()). s[1] = '\0'.*/ #include <ctype.Lecture Notes push (pop() * pop()). break. push (pop() . 17 The C Programming Language . p.8g\n".h> int getch (void). case '\n': printf ("\t%.op2).0\n"). if ( ! isdigit (c) && c != '. int getop (char s[]) { int i. } /* switch */ } /* while */ return 0. else printf ("error: stack empty\n"). } /* ------------------------------. case '-': op2 = pop().The C Programming Language (Lecture Notes) The C Programming Language .

if ( isdigit (c) ) while ( isdigit (s[++i] = c = getch()) ) .Lecture Notes u The scope of external declarations of variables and functions from the point of the first declaration to the end of source file u Definitions and declarations of identifiers the definition can appear exactly once (the memory for the object is allocated and the identifier can have its value set) declarations can appear many times provided all declarations are the same u Example FUN. void call (void) { int z. /* ? */ int sum (int x. extern int first. double val[MAXVAL]. int third = 7. int sum (int x. int second. int y). void dummy (void) {} extern int first. z = sum(3. int third = 8. 19 The C Programming Language . extern double val[MAXVAL]. int sum (int.Lecture Notes i = 0. extern int second. int y) { return x+y. 20 10 . } p. int). if ( c == '. char bufp = 0. } /* ------------------------------. else buf[bufp++] = c.' ) while ( isdigit (s[++i] = c = getch()) ) . int getch(void) { return (bufp>0) ? buf[--bufp] : getchar().The C Programming Language (Lecture Notes) The C Programming Language . } void ungetch (int c) { if (bufp >= BUFSIZE) printf ("ungetch: too many characters \n"). extern int first. } int sp.3 extern int sp. int first = 0. int second. return NUMBER. } p. int).4). /* error */ void dummy (void). if ( c != EOF ) ungetch(c). s[i] = '\0'.*/ #define BUFSIZE 100 char buf[BUFSIZE]. sum (int.

int n) { int low = 0. 31. int mid = (low + high)/2. 'a'. 31. 30. .. 31}...5 int binsearch (int x.. static char ch = 'A'..Lecture Notes u Header files #include #define function prototypes declarations of external variables u Static u and automatic variables static int glob = 0. . static int k. return 1.. . char array[] = "array". 30. 31. void fun_v (void) { auto int c. static int fun_v (void) { . 30. 31. .4 int fun (int x) { int i. } Example FUN. int Year [12] = {31.. 31}. int month [2] = {31. } int fun1 (int y) { int i = 1. 28.. 28.Lecture Notes u Initialization of variables Initialization of static variables . int v[]. 30 . 'b' 'l'.6 int days[] = {31. } Initialization of arrays Example FUN. int high = n ..The C Programming Language (Lecture Notes) The C Programming Language . 'e'..constant expression Initialization of automatic variables ... 31. } p.any valid expression Example FUN.. static int k = 0. 22 11 .. } int glob_i = 0.1.. /* error */ u Recursion direct indirect p.. 28.31}. '\0'}. static int fun_v (void). register auto int y. char table[] = {'t'. 21 The C Programming Language . .. } static int fun_i (void) { register int x..

if (left >=right) return. temp = v[i]. } p. int left. swap (v. putchar (n % 10 + '0').1). qsort (v. last + 1. left. last. left. int i. 23 The C Programming Language . last . v[i] = v[j]. void swap (int v[]. for (i = left+1. right).7 #include <stdio. i <= right.Lecture Notes u Example FUN. last). p. n = -n. 24 12 . int i. i). int right) { int i. } void swap (int v[]. ++last. i++) if (v[i] < v[left]) swap (v. qsort (v.The C Programming Language (Lecture Notes) The C Programming Language . int j). v[j] = temp. left.Lecture Notes last = left.h> void printd (int n) { if (n < 0) { putchar ('-'). swap (v. } if (n / 10) printd (n / 10).8 void qsort (int v[]. int j) { int temp. (left+right) / 2). } u Example FUN.

atof(char *). *x = *y.3 void swap (int x. printf ("a=%i b=%i\n". char *fun_ch (char *).. } 13 . . temp = *x. b). int *y) { int temp. temp = x. *ip = *ip + 10. } u Example PTR. p. ++*ip.The C Programming Language (Lecture Notes) The C Programming Language . y = 2. b = 2. db_ptr = &db. int *iq. iq = ip.2 void g (void) { int x = 5. x = y. . swap (&a. *y = temp.1 void f (void) { int x = 1. a. /* declaration of a pointer to int */ ip = &x. *db_ptr = atof ("123. /* y = x */ *ip = 0. } u Pointers u and function parameters void swap (int *x. a. b). &b). *ip = &x... Example PTR.Lecture Notes double *db_ptr. y = 2. swap (a. (*ip)--. } p. y = *ip. } void test (void) { int a = 1.. } void test (void) { int a = 1. printf ("a=%i b=%i\n".Lecture Notes Pointers and Addresses u Pointers u always define addresses and types of data being indicated by them (an address without a data type stored has a very limited scope of applications). 26 Example PTR.. b = 2. int *ip.. ip = &z[1]. int y) { int temp. b). *ip -=2.54"). y = temp. 25 The C Programming Language . z[10]. double db.

n++) . x = *pa. } u Pointers and Arrays Arrays are stored in a continuos memory area starting from the element with index 0 Array name is the address (a constant) of the first element (with index 0) u Example PTR.5 void ff (void) { int x. 28 14 .h> #include <stdio. if (c == '+' || c == '-') c = getch (). pa = &a[0]. p. for (n = 0. pa = a. n < SIZE && getint (&array[n]) != EOF..4 /* Reading integer numbers */ #include <ctype. pa = a+1..Lecture Notes u Example PTR. sign. 27 The C Programming Language . /* not a number */ return (0). if ( !isdigit(c) && c != EOF && c != '+' && c != '-' ) { ungetch (c). 3}. array[SIZE]. isdigit (c). a[10] = {1.h> int getch (void). void ungetch (int).'0'). 2.Lecture Notes #define SIZE 1000 void fun_x (void) { int n. if (c != EOF) ungetch (c). for ( *pn = 0. while ( isspace(c = getch()) ) /* nothing */ .. *pn *= sign. x = *a. int getint (int *pn) { int c. return c. getint (int *). } p. int *pa. /* read numbers to array */ .The C Programming Language (Lecture Notes) The C Programming Language . x = *(a+1). c = getch() ) *pn = 10 * *pn + (c . x = *pa. pa = &a[1]. } sign = (c == '-') ? -1 : 1.

int n. return n. u If a pointer pa indicates an array element (the array elements have the data type. . for (n = 0. *s != '\0'. n++) n++. i = strlen (ptr). s++) n++. } } p.6 /* Implementation of function calculating length of string */ int strlen (char *s) { int n. *pa = 31. } /* Calling of function strlen*/ void tt (void) { char array[] = "text". } /* Other implementations */ int strlen (char *s) int strlen (char s[]) { { int n.The C Programming Language (Lecture Notes) The C Programming Language . *(pa+2) = 44. pa = a+1.. x = 8. a = pa. i = strlen ("hello. pa = a.Lecture Notes u Example PTR. s[n] != '\0'. world").Lecture Notes x = a[0]. p. pa = a + 5. 30 15 . 8 = x. /* error !!! */ u A curiosity a[1] º *(a+1) º *(1+a) º 1[a] 1[a] = 5. x = *(a . s++) for (n = 0. return n. 29 The C Programming Language . *ptr = array. x = *(pa . which is designated to the pointer) then pa+1 points the next array element pa-1 points the previous array element pa+n points the n-th array element after the current one pa-n points the n-th array element before the current one provided that arithmetic operations on addresses do not result in an address beyond the array. int i. i = strlen (array). return n. /* error !!! */ *(a+4) = 1.2).. *s != '\0'. .5).. /* error !!! */ x = *(a+0). for (n = 0.

} } u Example PTR... char *alloc (int n) { if (allocbuf + ALLOCSIZE .. return n. for (n = 0.Lecture Notes u Operation on addresses adding and subtracting an integer number comparing addresses (of the same data types) subtracting values of two addresses u In assignment operation pointers have to be of the same type (except universal pointer type void *) u Pointers to characters and arrays of characters Example PTR. s++) .The C Programming Language (Lecture Notes) The C Programming Language . . } else return 0..9 /* Comparing declarations and initializations of variables of type char * and char [] */ { char message [] = "text".. . return n. pmessage = "text". } u p. n++) for (n = 0. } p. } u Example PTR. char *pmessage = "text".allocp >= n) { allocp += n.n. return allocp . /* return NULL */ } void afree (char *p) { if ( p >= allocbuf && p < allocbuf + ALLOCSIZE ) allocp = p. static char *allocp = allocbuf. int n.Lecture Notes int strlen (char *s) int strlen (char s[]) { { int n. 32 16 . s[n] != '\0'.8 /* declaration and assignment to variable of type char* */ { char * pmessage. 31 The C Programming Language . *s != '\0'.7 /* Allocation and deallocation of dynamic memory */ #define ALLOCSIZE 10000 static char allocbuf [ALLOCSIZE]. n++.

} u Example PTR. } } p.Lecture Notes u In stdlib library there are functions of dynamic allocation and deallocation of memory.h> #include <string. t++.11 /* Implementations of function copying strings */ /* In library <string. void free (void *ptr)... . "100 chars").. free (ctr).. /* void *malloc (int size) */ void *calloc (size_t count. ctr = (char *) malloc (100 * sizeof (char)). u Example PTR.Lecture Notes /* In the example implementations of function void strcpy (char *s. char *t).10 #include <stdlib.h> there is a function char *strcpy (char *s. void *malloc (size_t size). char *t) { int i. char *t) { while (( *s = *t ) != '/0'){ s++. 33 The C Programming Language . . i = 0. } void strcpy (char *s. 34 17 .The C Programming Language (Lecture Notes) The C Programming Language . are shown */ void strcpy (char *s. /* ERROR */ /* no copying */ /* address t is copied into parameter s */ } void strcpy (char *s. strcpy (ctr.h> { char *ctr. char *t) { s = t. while ( (s[i] = t[i]) != '/0') i++. size_t size).. const char *t) */ p.

char *t) { while (( *s++ = *t++ ) != '/0') . lptr = (char *) malloc (100 * sizeof (char)). } p. *s == *t. readline (line1).13 #include <stdlib. *(lineptr+1)) printf ("Lines are the same\n"). 36 18 . return *s . readline (lptr). 35 The C Programming Language . lineptr[1] = lptr. for (i = 0.Lecture Notes void strcpy (char *s. char line1[100]. t++) if (*s == '\0') return 0. char *t) { while ( *s++ = *t++ ) .*t. } lineptr[0] = line1. char *t) { for (. return. void readline (char *).The C Programming Language (Lecture Notes) The C Programming Language . void fun_l (void) { char *lineptr [20]. return s[i] . return. } void strcpy (char *s. char *t) { int i. if (strcmp (lineptr[0]. else printf ("Lines are different\n"). s[i] == t[i]. } int strcmp (char *s. } p. } u Example PTR. if ( ! lptr ){ error (3). s++.h> void error (int). i++) if (s[i] == '\0') return 0. char *lptr.Lecture Notes u Arrays of pointers and multidimensional arrays Arrays of pointers Example PTR.t[i].12 /* Implementations of function comparing strings */ int strcmp (char *s.

5} }. int b[2][3] = { {1.16 void xz (void) { char *pname[] = { "Illegal month". 38 19 . world Structure of command-line parameters int main (int argc. fun_tab (tab_2).The C Programming Language (Lecture Notes) The C Programming Language . "March" }. } p.\0 world\0 p. echo\0 hello.Lecture Notes 3 3 Multidimensional arrays are often replaced by arrays of pointers Example PTR. 3}. "February". void kl (void) { int tab_2 [5][13]. char *aname[][15] = { "Illegal month". u Command-line parameters Program echo prints out command-line parameters echo hello.Lecture Notes Multidimensional arrays Example PTR. 3}. char argc -> 3 argv @----->[ @--]----> [ @--]----> [ @--]----> [ 0 ] *argv[]). Example PTR. } 3 Order of array elements: b[0][0] b[0][1] b[0][2] b[1][0] b[1][1] b[1][2] b[0][x] b[1][x] 3 3 If a multidimensional array is a function parameter then it is possible to omit the first dimension of the parameter. 2. 37 The C Programming Language . "March" }. "January".15 void fun_tab (int [][13]). "January". {4. "February". 2.14 void fg (void) { int a[6] = {1.

100}. p. char *argv[]) { int i. for (i = 1.Lecture Notes Structures u Structure syntax struct tag { element_1. element_n. *++argv. } name_1.. }.17 /* the first version */ int main (int argc. struct point pt3. (argc>1)?" printf ("\n"). argv[i]. 40 20 . /* the second version */ int main (int argc..1 struct point { int x. int y. (i<argc-1)?" printf ("\n").. pt_2. p. i++) printf ("%s%s". } pt_1. 39 The C Programming Language . u Structure u declaration Example STRUCT. char **argv) { while (--argc > 0) printf ("%s%s". 23}. } ":""). element_2. struct my a_my.. return 0. struct point pt4 = {220. struct my { char c.The C Programming Language (Lecture Notes) The C Programming Language . . . name_n. return 0. i <= argc. } ":""). int code.Lecture Notes Example PTR. b_my = {'A'....

struct list { int value. struct my *s_ptr. int y) { struct point temp.. char *t_char[13]. struct point p2) { p1.x. j = point_ptr->y.x. 41 The C Programming Language .element address_of_structure->element Example STRUCT.. int it.x = x.. } p. j = pt_1. i = (*point_ptr).2 void fstr (void) { struct point pt_1. temp. } u Dereferencing of structure element u structure_name. i = pt_1. return p1. struct list *next. } struct point addpoint (struct point p1. } str_d.3 struct point makepoint (int x.y.y += p2. 42 21 . struct my s_my. .x += p2.y point_ptr = &pt_1.Lecture Notes u Operations on structures copying / assignment addressing dereferencing of element passing by value for function parameter returning as a function result value u Example STRUCT. temp. *point_ptr.Lecture Notes struct { double db.y = y. p1.x. int i. j.The C Programming Language (Lecture Notes) The C Programming Language . return temp. } p.

Lecture Notes Example STRUCT. }. else if (cond < 0) p->left = addtree (p->left. word). root = NULL. char *w) { int cond. struct tnode *addtree (struct tnode *p.4 /* Binary tree with strings stored in a lexicographic order */ #include <stdio. } p. } u p. treeprint (root).h> #include <ctype. int getword (char *. char *). char *strdup (char *). p->word)) == 0 ) p->count++. char word[MAXWORD]. struct tnode *left. w). int).The C Programming Language (Lecture Notes) The C Programming Language . return 0. w).Lecture Notes struct tnode *talloc (void). if (p == NULL) { p = talloc(). } else if ( (cond = strcmp (w. struct tnode *right. p->word = strdup (w). 44 22 . p->left = p->right = NULL. while ( getword (word.h> #include <string. void treeprint (struct tnode *). else p->right = addtree (p->right. 43 The C Programming Language . p->count = 1. int count. return p. MAXWORD) != EOF) if (isalpha (word[0]) ) root = addtree (root.h> #define MAXWORD 100 struct tnode { char *word. struct tnode *addtree (struct tnode *. int main (void) { struct tnode *root.

w++) if ( ! isalnum (*w = getch()) ) { ungetch (*w). } p. return c. --lim > 0. p = (char *) malloc ( strlen(s) + 1 ). } } int getword (char *word.Lecture Notes void treeprint (struct tnode *p) { if (p != NULL) { treeprint (p->left). p->count. } #include <stdlib. treeprint (p->right).The C Programming Language (Lecture Notes) The C Programming Language . } char *strdup (char *s) { char *p.5 struct bit_fields { unsigned int value: 4. } for ( . getch (void). while ( isspace (c = getch()) ) . 46 23 . return p. if (p != NULL) strcpy (p. break. unsigned int valid: 1. unsigned int new_w: 0.Lecture Notes *w = '\0'. return word[0]. p. s). if (c != EOF) *w++ = c. } bit_struct. void ungetch (int). p->word). char *w = word. unsigned int : 4. if ( ! isalpha (c) ) { *w = '\0'.h> struct tnode *talloc (void) { return (struct tnode *) malloc ( sizeof(struct tnode) ). printf ("%4d %s\n". 45 The C Programming Language . } u Bit u fields Example STRUCT. int lim) { int c.

b = 1. bit_struct. } un. u Union u Each element of an union is stored at the same address of memory declaration Example STRUCT.. u Dereferencing of union element union_name.ival = 5.. char *sval.The C Programming Language (Lecture Notes) The C Programming Language .. .value /* error */ u Union syntax union tag { element_1. 48 24 . typedef char *String.. element_n. int x. The new name can be used in a scope of the declaration.Lecture Notes u Example STRUCT.value = 15. u Example STRUCT.element address_of_union->element p.3f.6 union u_tag { int ival. String_ptr sptr.8 typedef int Length. typedef String *String_ptr.. char tab[12] = "nothing". } name_1. . un.. p.fval. String str_2 = "text_2". void fun_t (void) { Length a..valid = 0. u Type definition (declaration typedef) Declaration typedef introduces a new name of type according to the specification. float fval. It is impossible to get address of bit fields (by using operator &) & bit_struct. char ch = 'A'. Sequencing of bits and sequencing of fields are implementation-dependent. 47 The C Programming Language .Lecture Notes bit_struct.7 un. char *str_1 = "text". typedef Length *Len_ptr. name_n. fl_1 = un. There is no new type.fval = 2.

str_2 = &ch. 1 } .printf( "hello.The C Programming Language (Lecture Notes) The C Programming Language .1 int main (void) { int x.return x . return x. world" return main void ) {int x .} p.Lecture Notes x = b. a = 12+x-b. int printf int ( "hello. world").x=1 . x .Lecture Notes Preprocessor u Program u in C consists of lexical atoms Example PRE. str_1 = str_2. x = 1. x x = . } main ( ( void ) { ) int . 49 The C Programming Language . 50 25 . sptr = &str_2. world" ) . printf ("hello. **sptr = 'B'. } p.

51 The C Programming Language . so substitution does not appear in the following line ATOM_var = "ATOM + BTOM". p. 52 26 . a parameter in the angle parentheses <> denote a file from a system directory #include <string. #define name replacement_text The scope of the directive: from the directive to the end of file. The result of preprocessing is a text.h" Files included into a source file can have directives #include. The previously defined symbols can be present in the replacement_text and they are substituted by their definitions.2 #define ATOM 1 #define BTOM ATOM+2 #define LONG_TOM 1 + 2 *ATOM . which has to be the first not blank character in a line of source code.2 is possible to define a macrodefinition with text parameters with the help of directive #define. There must not be currently defined name in the replacement_text.Lecture Notes u Directive #define is used for defining a symbol. The directives #include cannot have direct or indirect referencing to the file in which they are included.h> a parameter in quotes denote a file from any directory #include "my_hdr. which is replaced by a sequence of characters (the definition) in the scope of the directive.h" a file name can be given with a directory path #include "/mainp/source/headers/my_hdr. a preprocessing directive can appear after some blanks (spaces or tabulations) u Directive #include is used for inclusion a content of a source file into the place of the directive. Example PRE.Lecture Notes u Comments in C programming language are omitted by compiler in analysis of a source code. u Directives of the preprocessor begin with character #. u The preprocessor is a program that processes a text according to some rules described by preprocessing directives. Every appearance of lexical atom name is substituted by the replacement_text. The included source file is depicted by the parameter of the directive.BTOM + (int)('A') -12 \ * BTOM /* invalid */ #define CTOM CTOM + 1 The substitution is applied only to lexical atoms.The C Programming Language (Lecture Notes) The C Programming Language . p. u It provided the line is in the scope of the definitions from Example PRE.

x/y). i j x x = = = = 1. ". MAX (i++. /* OK */ u A hint: Parameters of a macrodefinition should be usually enclosed in parentheses in the definition. ?. back) front##back paste (name. ("x/y=%g\n". i++>j++ ? i++ : j++.. x = 5*a>6-b ? 5*a : 6-b. 1) = x.. 6-b). /* ?? */ #define sqr(x) (x)*(x) y = sqr (z+1). y = (z+1)*(z+1). ' then in the string resulting from operator # the character will be represented with the expected prefix \ to output the desired character. #endif p. y = z+1*z+1. j+=2 */ #define square(x) x*x y = square (z+1). 54 27 . dprint(expr) printf (#expr "=%g\n"..Lecture Notes Example PRE. expr) (x/y). u Directive #undef is used for canceling the current definition #undef getchar u Conditional compilation Directive #ifdef #ifdef identifier /* lines included to compilation if the identifier is defined*/ . 2. y) x>y ? x : y /* no spaces */ x = MAX (5*a. p. 53 The C Programming Language . j++). u Operator # in directive #define is used for concatenation of parameters #define paste(front. /* after concatenation of strings*/ define dprint printf printf If in argument of macrocall there is one of the special characters (represented in strings with prefix \) like \. name1 = x. x/y). /* i+=1.Lecture Notes u Operator # in directive #define is used for a such processing of the parameter that it can be properly presented as a part of a string.The C Programming Language (Lecture Notes) The C Programming Language . ("x/y" "=%g\n".3 #define MAX(x.

... #endif x)..4 #define TEST . #ifdef TEST printf ("[Debugging]: x = %i\n".. #endif p..'a' == 25) #if defined (SYMBOL) #if defined SYMBOL #ifdef SYMBOL #if defined SYMBOL && defined TEST && SYMBOL > 20 Directive #if #elif #if const_expression_1 ..Lecture Notes Example PRE. #endif Directive #ifndef #ifndef identifier ......'a' == 25 #if ('z' . #elif const_expression_n ...... if the identifier is if the identifier is if the identifier is p.... #else .. ..Lecture Notes Directive #if #if const_expression . #else .. #elif const_expression_2 .... 56 28 .. #endif Example PRE...The C Programming Language (Lecture Notes) The C Programming Language ..... /* lines included to compilation if the const_expression is defined*/ . #endif Directive #ifdef #else #ifdef identifier .... 55 The C Programming Language .... /* lines included to compilation defined*/ ....5 #if 'z' . /* lines included to compilation not defined*/ . /* lines included to compilation not defined*/ .

The C Programming Language (Lecture Notes) The C Programming Language .h> #include<string. char *). char *). passed as parameters etc. char *).Lecture Notes Advanced Topics u Pointers to functions It is possible to define pointers to functions that can be assigned to. int (*pfi) (char *. A pointer to function stores address of a function code and the function characteristics (return type. 57 The C Programming Language . int right. /* pointer to function */ int *fi (char *. number and types of parameters. #include<stdio. 58 29 . void writelines (char *lineptr[]. number of lines to be sorted */ char *lineptr[MAXLINES].h> #define MAXLINES 5000 /* max. kept in arrays and structures. /* function returning a pointer */ u Example ADV.Lecture Notes Other directives 3 3 3 3 #error message #line number #line number filename #pragma preprocessing_tokens __DATE__ __FILE__ __LINE__ __STDC__ __TIME__ /* /* /* /* /* date of translation */ source-file name */ current line within source file */ conforming translator and level */ time of translation */ Predefined macrodefinitions 3 3 3 3 3 p.1 /* Sorting of read lines (each lines contains a number) according to lexicographic or numeric (option -n) order. void qsort (void *lineptr[]. int (*comp)(void *. int nlines). int readlines (char *lineptr[]. void*)) int numcmp (char *. int nlines). p. int left.

60 30 . char *s2) { double v1. } p. "-n") == 0 ) numeric = 1. qsort (v. ++last. (left+right)/2). int. comp). left. v2. right.Lecture Notes swap (v. 0. qsort (v. if (v1 < v2) return -1. void *)) (numeric ? numcmp : strcmp) ). i). last). v[left]) < 0 ) swap (v. v1 = atof (s1).Lecture Notes int main (int argc. int(*comp)(void *. 59 The C Programming Language . (int (*) (void *. char *argv[] ) { int nlines. numeric = 0. swap (v. last+1.The C Programming Language (Lecture Notes) The C Programming Language . int left. last. if (left >= right ) return. void swap (void **. } else { printf ("Input too big to sort\n"). last-1. nlines-1. void *)) { int i. MAXLINES)) > 0 ) { qsort ( (void **)lineptr. v2 = atof (s2). left. left. i <= right. writelines (lineptr. i++) if ( (*comp)(v[i]. else return 0. return 1. p. int right. comp). int). if ( argc > 1 && strcmp (argv[1]. last = left. } } void qsort (void *v[]. nlines). for (i = left+1. else if (v1 > v2) return 1. if ( (nlines = readlines (lineptr.h> int numcmp (char *s1. } #include<math. return 0.

h> #define BUFSIZE 80 int readlines (char *lineptr.The C Programming Language (Lecture Notes) The C Programming Language . c = getchar(). } #include <stdlib. i. minus. char buf[BUFSIZE]. if ( c=='+' || c=='-' ){ minus = (c == '-').Lecture Notes minus = 0. temp = v[i]. int nlines) { int i. int i. c = getchar(). i < nlines. lineptr[i]). v[j] = temp. p. int j) { void *temp. c = getchar(). v[i] = v[j].' && i < BUFSIZE ) { buf[i++] = c. 61 The C Programming Language . while ( c == ' ' || c == '\t' ) c = getchar(). int nlines) { int c.Lecture Notes void swap (void *v[]. } while ( isdigit(c) && i < BUFSIZE ) { buf[i++] = c. /* white chars after a sign */ } if ( isdigit(c) || c == '. while ( isdigit(c) && i < BUFSIZE ) { buf[i++] = c. while (1) { while ( c = getchar() == ' ' || c == '\t' ) . lines = 0. } if ( c == '. for (i = 0. /* white chars at the beginning */ i = 0. } p. i++) printf ("%s\n". c = getchar(). 62 31 .' ) if (minus) buf[i++] = '-'. } void writelines (char *lineptr[].

fa()[]. f(). 64 32 . *p. a[]. /* !!! */ /* ??! */ /* ??! */ /* ??! */ p. } while ( c != '\n' && c != EOF ) c = getchar (). strcpy (lineptr [lines-1].Lecture Notes if ( i > 0 && lines < nlines ) { buf[i] = '\0'. if ( c == '\n' ) continue. lineptr[lines++] = (char *) malloc (strlen (buf) + 1 ). (*pa)[]. buf). /* !!! */ (*fpf())(). /* ??! */ Parser of complex declarations Simplified grammar of complex declarations (for lexical analysis) dcl: direct-dcl: optional *s direct-dcl name ( dcl ) direct-dcl ( ) direct-dcl [ optional size ] p. *(*pap)[]. /* ??! */ faa()[][]. /* ??! */ *afp[](). (*paf)[](). ff()(): ***ppp. (*pfa)()[]. (*paa)[][]. (**ppf)(). aa[][]. } return lines. int int int int int int int i. /* ??! */ afa[]()[]. (*apa[])[].The C Programming Language (Lecture Notes) The C Programming Language . **app[]. 63 The C Programming Language . aaa[][][]. /* !!! */ (*pf)(). /* ??! */ aff[]()(): /* ??1 */ ***fpp(). af[](). if ( c == EOF ) break. /* /* /* /* /* /* /* /* !!! !!! !!! !!! ??! !!! ??! ??! */ */ */ */ */ */ */ */ /* !!! */ /* !!! */ int int int int int int int int int int int int int aaf[][](). /* !!! */ *fap()[]. *fp(). /* ??! */ faf()[](). (*pff)()(). /* ??! */ *ffp()(). *aap[][]. *(*pfp)(). (*fpa())[].Lecture Notes int int int int int int int int int int int int int (**ppa)[]. } u Complex declarations A list of simplified declarations int int int int int int int *ap[]. /* ??! */ fff()()(). (*apf[])(). **pp. /* ??! */ *ffa()()[].

PARENS. out[0] = '\0'. [] */ p. char token [MAXTOKEN]. int gettoken (void). token). char out[1000]. 65 The C Programming Language . if (tokentype != '\n') printf ("syntax error \n"). ().Lecture Notes void dcl (void). int main (void) { while (gettoken() != EOF) { strcpy (datatype. else printf ("%s: %s %s\n".The C Programming Language (Lecture Notes) The C Programming Language . dcl(). } p.h> #include <ctype. /* . out.Lecture Notes Example of parsing declaration int (*pfa[])() ( * pfa [ ] ) ( ) | | | | | | | | name | | | pfa | | | | | | | | direct-dcl | | | is | | | | | | | | direct-dcl | | array | \ / / | \ dcl / | of pointers \ \ / / \ | / / direct-dcl / (to) function \ / direct-dcl and | dcl OK u Example ADV. char datatype [MAXTOKEN]. name.2 #include <stdio. void dir_dcl (void). char name [MAXTOKEN]. int tokentype. } return 0. BRACKETS}. datatype).h> #include <string. 66 33 .h> #define MAXTOKEN 100 enum {NAME.

return (PARENS). } ) void dir_dcl (void) { int type. 67 The C Programming Language . " array").> 0) strcat (out. for (ns = 0. else { strcat (out. while (ns-. } } p. token). if ( c == '(' ) { if ( (c = getch()) == ')' ) { strcpy (token. " pointer to"). 68 34 . } } int gettoken (void) { int c. if (tokentype != ')') printf ("error: missing )\n"). void ungetch (int). " of"). ns++. } else { ungetch (c). if (tokentype == '('){ dcl(). gettoken() == '*'. getch (void). while ( (c = getch()) == ' ' || c == '\t' ) /* */.Lecture Notes void dcl (void) { int ns. p. token).Lecture Notes while ( (type=gettoken()) == PARENS || type == BRACKETS ) if (type == PARENS) strcat (out. strcat (out. " function returning"). } else if (tokentype == NAME) strcpy (name. dir_dcl(). else printf ("error: expected name or (dcl) \n"). char *p = token. strcat (out. return tokentype = '('. "()").The C Programming Language (Lecture Notes) The C Programming Language .

69 The C Programming Language . ) *p++ = c.The C Programming Language (Lecture Notes) The C Programming Language . break: } } va_end (ap). *p = '\0'. ival).) body of the function At least one parameter has to appear on the const_parameter_list u Example ADV. return tokentype = BRACKETS. break. case 's': for (sval = va_arg (ap. double dval. /* argument pointer */ char *p. ungetch (c). int ival. } else return tokentype = c. *sval. return tokentype = NAME. break. printf ("%d". /* ap points argument after fmt */ for (p = fmt. isalnum ( c = getch() ). } u Functions with variable number of parameters Syntax of function return_type function_name (const_parameter_list. } else if ( isalpha(c) ) { for (*p++ = c..3 #include <stdarg. . default: putchar (*p). ) . char *).... continue. } switch (*++p) { case 'd': ival = va_arg (ap. int). *sval. va_start (ap. printf ("%f". break. dval). case 'f': dval = va_arg (ap. (*p++ = getch()) != ']'. 70 35 .) { va_list ap.Lecture Notes else if ( c == '[') { for (*p++ = c. /* clear */ } p.Lecture Notes void minprintf (char *fmt. double).h> p. *p.h> #include <stdio. fmt). sval++) putchar (*sval). . p++) { if (*p != '%') { putchar (*p).

0). return 0. min = va_arg (ap. i++) printf ("%d is the least from %d. va_list ap. 0). void va_end (va_list ap). j. min_arg (3.h> #include <stdio. i <= 10.Lecture Notes void va_start (va_list ap. p..) { int i. i < arg_no. int). for (i = 2.Lecture Notes for (i = -10. Argument pointer ap set to the first element of the variable parameter list (or after the last one from the constant parameter list). i. va_end ap.. parmN). 71 The C Programming Language . The outcome of the macrodefinition is undefined if it is called and there is no more parameters to be processed.h> int min_arg (int. u Example ADV. . int j = 3. int min. Macrodefinition is called before the variable parameter list can be accessed. The function should know (check) the number and types of parameters from the variable parameter list. Macrodefinition should be called if va_start was executed. . i.. arg_no). The macrodefinition blocks accessing the variable parameter list.The C Programming Language (Lecture Notes) The C Programming Language . type). int main (void) { int i.4 #include <stdarg. 72 36 . j. next_arg. } p. i++) if ( (next_arg = va_arg (ap. va_start (ap. } int min_arg (int arg_no. return (min). type va_arg (va_list ap.). %d & %d\n". Macrodefinition is called to access successive elements of the variable parameter list. int)) < min ) min = next_arg..

u Function int feof (FILE *fp) checks the condition of end-of-file for a file associated with fp. EOF. write. The directory path is constructed according to rules defined by the operation system e.c" "c:\dos\sys\file.1" "$HOME/Subdir/ala.txt.h>) there is a definition of structure FILE.The C Programming Language (Lecture Notes) The C Programming Language . a current FILE *fp. "r+b" "w" "wb" "w+" "wb+". stdout and stderr.) a program a pointer to the structure FILE is declared to depict a file. opening mode. 74 37 . read and create binary file append to text file append to binary file append to. FILE *fopen (char *file_name. p. A file can be opened in one of the following modes: "r" read text file "rb" read binary file p. ". error. read and create binary file int fclose (FILE *fp) closes a file associated with fp. which contains implementation information (buffer address.Lecture Notes "r+" "rb+". 73 The C Programming Language .prg" If the file cannot be opened the function returns NULL. read and create text file write. File buffers are swept and released. u In position in the file.. The function returns zero if the file was closed properly or EOF otherwise. it can contain the directory path. char *mode) u Function is used for opening a file with the given name and in the given mode. "a+b" u Function read and write text file read and write binary file write text file write binary file write.Lecture Notes Input / Output Functions u In the standard input/output library (<stdio. It is possible to open concurrently up to FOPEN_MAX files including always opened streams: stdin. The file name can be a full one. "w+b" "a" "ab" "a+" "ab+". write.g. etc. read and create text file append to./Dir12/my_file. The function returns zero if the condition is not satisfied or a number different from zero otherwise.

The indicator of error is set until one of the following functions is called: clearerr. The format is defined by parameter format. fclose or rewind. where conversion specifications are started with character %.Lecture Notes u Function char *strerror (int error) (from library string) returns address of the standard message associated with value of error .) are used for formatted output of variable number of data.g. The functions print the formatted data respectively to: 3 stream stdout 3 file defined by parameter fp starting from address str Integer values returned by the functions are numbers of characters printed if function call was successful. At the beginning of the program the variable is set to zero. Up to 509 characters is guaranteed by ANSI standard to be outputted by a single function call. 75 The C Programming Language . Parameter format is a string. Some library functions (e. Number and types of error codes are implementation dependent.The C Programming Language (Lecture Notes) The C Programming Language .. u Functions int printf (char *format. p.) int fprintf (FILE *fp.. . The function returns zero if the condition is not satisfied or a number different from zero otherwise.Lecture Notes u Function int fflush (FILE *fp) flushes the buffer associated with (file) fp. The function returns zero if there was no error or EOF otherwise. The meaning of error codes of the variable is implementation dependent. char *format. from stdio) store an error number in this variable.. u Function int ferror (FILE *fp) checks whether an error has been encountered for a file associated with fp. . 76 38 . u Macrodefinition errno declared in <errno...) int sprintf (char *str. Data from the variable parameter list are printed successively according to successive conversion specifications. . Otherwise a negative number is returned. The error message is preceded by the string (the function parameter) followed by character :. char *format.. String format contains any p.h> is expanded into a global variable of type volatile int. u Function int perror (const char *string) checks value of variable errno and writes the standard error message to stream stderr.

g.dddddd. Conversion specifications 3 start with character % 3 can have the following (optional) fields: – character : left alignment – character + : number is preceded by its sign – character space : number is preceded by . f. i. which are printed to a proper output. x.or space – character # : applicable to numeric data. e in number format is replaced by E – f double to floating point number in format [-]d. • decimal point is printed for conversions e. \t). 77 The C Programming Language . g. • 0x(0X) is printed for conversions x (X) – character 0 • zeros are printed instead of leading spaces for conversions d. at least one digit appears on the left hand side of decimal digit and on the right hand side of it there is number of digits resulting from the precision (default number is 6). i. o. : separator of two numbers p.The C Programming Language (Lecture Notes) The C Programming Language . u. at least one digit appears on the left hand side of decimal and on the right hand side of it there is number of digits resulting from the precision (default number is 6). G. for precision equal to 0 the decimal point is not printed p. o. E.Lecture Notes characters (including control ones like \n. e. f. for precision equal to 0 the decimal point is not printed – E like conversion e. u. E.Lecture Notes – second number : precision • number of characters for a string • number of digits after the decimal point • minimum number of digits for integer number – character h : short value or l (L) : long value 3 have one of following letter denoting a conversion type: – c int or unsigned integer to single character – d int to signed integer number – e double to floating point number in format [-]d. • zero after decimal point is printed for conversions g and G.dddddd e+/-dd. X – first number : minimum width of output field – character . 78 39 . x. G.appears in the specification then the leading zeros option is ignored for conversions d. X • if precision is given or character .

) int fscanf (FILE *fp. wor: :hello. it does not influence the printing – o int to octal number – p void* to address in format defined by implementation – s char* to string of characters.. world". string).. world: :hello. 79 The C Programming Language . char *format. 80 40 .10s: :%-10s: :%. format :%s: :%10s: :%.. s). number of characters is limited by the precision – u int to unsigned integer – x int to hexadecimal number with digits a-f – X int to hexadecimal number with digits A-F Precision in a conversion specification can be depicted by character * and then the precision is defined value of the successive argument from the variable argument list – g printf ("%. . printf ("%s". string).) int sscanf (char *str.10s: wynik :hello. where the number of characters printed so far is stored. wor: :hello. wor : char *s = "%s%n%". u Functions int scanf (char *format. p. . printf (s)..1 char *format. . if exponent is smaller than -4 or if it is greater than the precision then format e is taken – G like g but choice is between formats E or f – i like d – n associated with a pointer to int. field_width.) is used to formatted inputting a variable number of values.15s: :%-15s: :%15..10s: :%-15. p.Lecture Notes double to floating point number in format e or f number of digits is equal to precision. world: :hello.*s".The C Programming Language (Lecture Notes) The C Programming Language . world: :hello. world : : hello. printf (format. char *format.. world: :hello.Lecture Notes u Example IO. usually format f is chosen. char *string = "hello.

Conversion specifications consist of 3 character % 3 a set of optional fields – character * : omitting a currently read lexical atom – number : maximal width of field of read characters – letter h : modifier for short int or l (L) : modifier for long int (long double) 3 letter . 81 The C Programming Language .a specification of conversion p.Lecture Notes – c – d – e – E – f – g converts into characters (char) a number of characters defined by a field width (default: 1) and stores them in memory from address char * passed as the argument. If no conversions were possible then the functions return EOF. a number of read characters up to this point is stored under the given address int * converts an atom representing an octal number and stores it at address int * converts an atom representing address in an implementation dependent form and stores it at address void * p. The white characters are omitted for all conversions except c and [. If on the variable parameter list there is less arguments than number of conversion specifications then behaviour of the functions is undefined. '\0' is not added converts an atom into integer number stored at address int * converts an atom representing number in exponential form into double number stored from address double * like conversion e converts an atom representing number in decimal form into double number stored from address double * converts an atom representing number in exponential or decimal form into double number stored from address double * – G – i – n – o – p like conversion g like conversion d does not read a data. from file defined by fp and from memory starting from address adr. The functions collect characters into lexical atoms separated by white characters. Arguments on variable parameter list have to be addresses of data of appropriate types according to successive conversion specifications. The functions get data respectively from stream stdin.The C Programming Language (Lecture Notes) The C Programming Language . 82 41 . The value returned by the functions is a number of successfully performed conversions.Lecture Notes Conversion specifications from format define the way of translating input characters into values.

Lecture Notes Example IO. which is stored at address unsigned int * converts an atom representing hexadecimal number into unsigned integer number.The C Programming Language (Lecture Notes) The C Programming Language .Lecture Notes – s – u – x converts an atom (without white characters) into a string written into memory starting from address char *. e. printf ("i = %d. text = read_this_text** u u Function char *fgets (char *string. } $ test 45.3 int main (void) { int i. &chartab[5]). c. array). The reading is terminated if n-1 characters has been read or end of line (\n) has been encountered or end of file (EOF) has appeared. chartab. a character not belonging to the set terminates the lexical atom. The string is terminated by '\0'. the string is written at address char * and terminated by '\0'. scanf ("%d. text = %s\n". 84 42 . char chartab[16]. i. no input is performed (like for other "common" characters) – [/] a set of characters between [/] has to fit characters read from input. int n.is defined by implementation . if after [ there is ^ it means that valid are characters NOT belonging to the set. If '\n' was read then it is written to the memory. c = C. c. which is stored at address unsigned int * like conversion x the format fits to the single character %. world" into char array[20] */ scanf ("%[^abcd]".2 /* Let us read a string "hello. return 0. chartab). %*s %%%c %5s %s".often it denotes the range of character set. if the first character of the set is] or ^] then ] belongs to the set. 83 The C Programming Language . /* in the array string "hello.worl" can be found */ u Example IO. Function returns the address of the string if everything was OK or NULL if EOF or an error was encountered. FILE *fp) reads characters from file fp to memory starting from address string. &c. p. the meaning of . &i. ignore_it %C read_this_text** i = 45.g. c = %c. [a-z] – X – % p. character '\0' is appended at the end of string converts an atom into unsigned integer number.

strchr. u Function int puts (char *string) writes characters stored at address string into standard output file.Lecture Notes u ANSI C Libraries . tan. sqrt. strlen. u Function int ungetc (int c. The function returns non-negative number if it has terminated correctly or EOF otherwise. stdarg signal assert time limit. 3 rand. log10. float p. math 3 exp. tanh. FILE *fp) gets back character c to file fp opened for reading. ceil. asin. sin. sinh. The function returns value c if it has terminated correctly or EOF otherwise. malloc. fabs. srand.Lecture Notes u Function is not written. isalnum. islower.The C Programming Language (Lecture Notes) The C Programming Language . isupper.Selected functions string 3 strcat. u Function char *gets (char *string) int fputs (char *string. cosh. log. floor. Function returns the address of the string if everything was OK or NULL if EOF or an error was encountered. The reading is terminated if end of line (\n) has been encountered or end of file (EOF) has appeared. 85 The C Programming Language . p. The string is terminated by '\0'Character'\n' is not written to the memory. strrchr ctype 3 isalpha. strncpy. 86 43 . 3 abort. FILE *fp) writes characters stored at address string into file fp. strcmp. atan. setjmp. cos. realloc. Character '\0' reads characters from standard input file to memory starting from address string. qsort. atexit. isdigit. Character '\0' is replaced into '\n'. exit. strncmp. atan2. 3 bsearch. 3 system. isspace. tolower stdlib 3 calloc. strncat. pow. toupper. The function returns non-negative number if it has terminated correctly or EOF otherwise. acos. strcpy. free.

Sign up to vote on this title
UsefulNot useful