CMP507 Data Structure and Algorithm in C Note: #4 Mini C Programming Language Preprocessors #define name constant #define macroname macrodefinition #include#ifdef id #else #endif This is a very basic subset of C programming language. The purpose is to provide the beginner a primitive set of syntax and programming elements in C. C is made of functions either recursively or composely. C provides also bit manipulation and pointer access. A function is a procedure which always returns a value. The main program name of a C program must be "main". Function name must be followed by () even no parameters. Pointer manipulation is done element by element, not byte by byte. In C, a string is defined as an array of characters which always terminated by '\0'. The following functions are used to manipulate string in C language: strcpy strcat strcmp strlen strncmp strncpy strrchr strchr strncat tolower toupper An expression consists of a constant, a variable, or two or more constants and variables combined by one or more binary operators. An expression always yields a single value. The categories of statements in C: 1. Input getchar(); gets(buffer); scanf("format", item list); fscanf(file pointer,"format", item list); fgetc(file pointer); fgets(buffer, size, file pointer); fread(buffer, size, number, file pointer); sscanf(buffer,"format", item list); 2. Output putchar(character); puts(buffer); fputs(buffer, size, file pointer); printf("format",item list); fprintf(file pointer, "format", item list); sprintf(buffer,"format", item list); 3. Expression and Assignment i++; i--; k = i + j % m / n - f + sqrt(h); 4. Selection if ( i==j ) statment; if( i >= j ) statement; else statement; switch(c) { case 1: statement; break; case 2: statement; break; default: statement; break; } 5. Iteration statements a. for(i=0; i<=100; i++) statement; b. while ( i != j ) statement; c. do statement while ( i >= j); 7. Flow control break; continue; return; exit; goto identifier; 8. Compound statement { statements } Data types and Declarations in C int i,j,k[10][20], *iptr; float f,m,n[12][12][24], *fptr; char c,d,e[100], *cptr; struct structuretag { char name[24]; int salary; char telephone[13]; } employee[100], *empptr; Operators: 1. Arithmetic: +,-,*,%,/ 2. Bitwise: &,|,^,~,>>,<< 3. Logical: &&,||,! 4. Relational: >,>=,<,<=,==,!= 5. Array [] 6. Function call () 7. Structure union member . --> 8. Address & 9. Indirection reference * 10. Size of object sizeof(obj) 11. Casting (data type) Format specifiers 1. decimal %d 2. string %s 3. character %c Special ascii codes \" \' \v \f \t \r \n \0 \ddd (octal) C language is case-sentive, that means the lowercase character is different from the uppercase chacater. A C statement must be terminated by a semicolon ';' character. This is mini C language. The purpose is to provide you some primitive syntax and statements to get you started. In order to learn the C language, you will have to read "The C programming language by Kernighan & Ritchie" VC++ is a superset of C language, so if you want to be proficient in VC++, you had better be good in C language.