240 likes | 344 Views
Lecture 1. System programming in C. Summary. General Information Homework process & schedule IDE Overview Introduction to C Simple C Programs and Examples. General Information. Instructor: Ilja Tshahhirov, phone: 6-507-138, e-mail:ilja@previo.ee
E N D
Lecture 1 System programming in C
Summary • General Information • Homework process & schedule • IDE Overview • Introduction to C • Simple C Programs and Examples
General Information • Instructor: Ilja Tshahhirov, phone: 6-507-138, e-mail:ilja@previo.ee • Course WEB page: http://www.itcollege.ee/~ilja/sysprog • Materials: • Kernigham Brian, Ritchie Dennis 1988 The C Programming Language, Prentice Hall. • http://www.cs.columbia.edu/~aya/W3101-01/ • http://www.educationplanet.com/search/Computers_and_the_Internet/C_Programming • MS Visual C help • Practice
General Information contd. • Prerequisites • Some prior programming experience • Some knowledge of Windows environment • Account in the practice class
1. Introduction, program structure, simple data types 2. Simple C programs, control flow 3. More on data types, functions 4. Streams, control flow 5. Preprocessor, strings 6. More on control flow , scooping, debugging 7. Dynamic memory, arrays 8. Pointers, casting 9. Structures 10. Linked lists 11. Recursion 12. Unions, variable number of arguments 13. File operations 14. Network identification and operations 15. Review session 16. Review session. Conclusion Course Syllabus
Home works • Contribute to the course mark (20%) • Submitted on practice class or by e-mail to practice instructor. • 4 home works, each has several assignments.
Integrated Development Environment (IDE) Overview • MS Visual C 6.0 is used as IDE • Short tutorial on the IDE will take place on practice.
C Program Structure • Hello world program: #include <stdio.h> include information about standard library main() define function named main(), no arguments { function statements in braces printf(“Hello!\n”); call function prinntf to print sequence of characters, \n - newline character } • main()function - entry point of each C program: • Program building blocks • functions, consisting of statements • variables • comments! main() { int i = 1; /* initialize i with 1 */ i = 15-1; /* assign 15-1=14 to i */ }
Variables • Is a storage for computation result • Has type - Type defines its interpretation, size, etc. • Should be declared (declaration - announcement of properties of variable) before use. • Example declarations int i; /* i is an integer*/ float f; /* f is an FP number*/ char c1,c2; /*c1 and c2 are characters */
Basic Data Types • Data type - define how to interpret data • Data types: • int (integer numbers) • float (Floating Point (FP) numbers) • double (Double precision FP numbers) • char (characters) • Size of data depends on its type, for a single type may vary among C implementations. To determine size of data type, call sizeof() function - for example, • sizeof(char) = 1 • sizeof(int) = 4 • sizeof(float) = 4 • sizeof(double) = 8
Integers • Two types - signed and unsigned: int j; /* one bit used for sign, the rest - for value */ unsigned int j; /* all bits used for value - larger value may be stored. */ • Boolean values - no specific type, integer types are used: 0 means FALSE, any other value - TRUE (typically 1). • Operations: int j = 1; /*initialization */ j = j+1; /*increment value, same as j++*/ j--; /*decrement value,same as j=-1, same as j -= 1; */ j *= 6; /*multiply j by 6, same as j=j*6;*/ j/=6; /*divide j by 6*/
Integer representation • Represented as a bit pattern • Limits (for 16-bit integer): • Unsigned: 0..65535 • Signed: -32768…+32767
Expressions, Statements • Expression - consists of operands and operators, has value. Types are: • Relational expressions: x>y , 2==y , x!=5 • Arithmetic expressions: x+2, y-- , j*j , 6/3 • Assignment expressions: x=y, x=4 Note: Do not use (x=y) to check equality! • Statements - function building blocks - may be: • simple x=10; • compound {x=5; y=z=3; f *= 5.0;} • loop • do while • multi-way if
Loops • “For” loop for (initialization statement; test statement; iteration statement) statement; Example: for (j=0; j<5; j++) printf(“Hello!\n”); Example: j=0; for ( ; ; ) { printf(“Hello!\n”); if (j>=5) break; j++; }
Loops contd. • “While” loop while (test) statement; Example: j=0; while ( j<5 ) { printf(“Hello!\n”); j++; } Equivalent to for (j=0; j<5; j++) printf(“Hello!\n”);
Loops contd. • “Do while” loop do statement while (test); Example: j=0; do { printf(“Hello!\n”); j++; } while ( j<5 ); equivalent to for (j=0; j<5; j++) printf(“Hello!\n”);
if if (test) /*if test expression is not 0, then execute statement*/ statement Example: if (x>1) x=1; Example: if (x>100) { x=100; printf(“X is larger than 100, truncating it to 100\n”); } Example: if (x>1) if (x>2) x = 0;
if contd. if (test) /* execute true-statement in case test is not 0 */ true-statement; /* otherwise execute false-statement */ else false-statement; Example: int j=1; int k=1; if (j==k) printf(“j is equal to k\n”); else printf(“j differs from k\n”); Example: if (j==k) printf(“j is equal to k\n”); else if (j<k) printf(“j is smaller than k\n”); else printf(“j is larger than k\n”);
Functions • Function definition: int square(int a) { return (a*a); } • Function declaration: int square(int a); /* prototype - now square can be used*/ • Function must be declared before use • Function may be declared via a prototype before it is defined. • Prototype contains all the information needed for compiler to generate function call code: • name; • type of return value; • type of arguments;
Functions cntd. Example: #include <stdio.h> int square(int); main(){ int x,y=2; x=square(y); /*function call*/ printf(“square of %d is %d\n”, y, x); /* another function call*/ }
Standard Input/Output • Standard input - from keyboard • Standard output - to screen • Standard library functions • Input: printf, putchar; puts • Output: scanf, getchar; gets • printf and scanf - have the richest functionality among named functions - are able to work with different types: int j; float f; double d; char c; printf(“%d %f %lf %c\n”, j,f,d,c); scanf(“%d %f %lf %c\n”, &j,&f,&d,&c); & - address operator - when reading value into a variable we use &.
Input-Output Example: main(){ int j; for (j=0; j<5; j++){ printf("Hello!\n"); printf("j=%d\n", j); } } Output: Hello! j=0 Hello! j=1 Hello! j=2 Hello! j=3 Hello! j=4
Sample program #include <stdio.h> /* print Fahrenheit-Celsius table for fahr=0, 20, ..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature table */ upper = 300; /* upper limit of temperature table */ step = 20; /* step size */ fahr = lower; while(fahr <= upper) { celsius = 5 * (fahr-32) /9; printf ("%d\t%d\n", fahr, celsius); fahr = fahr + step; } }