systems programming
play

Systems Programming 1. Basic C Programming Guillaume Pierre Fall - PDF document

0 Systems Programming 1. Basic C Programming Guillaume Pierre Fall 2007 http://www.cs.vu.nl/ gpierre/courses/sysprog/ 1 Table of contents 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . 2 2. Data types . . . .


  1. 0 Systems Programming 1. Basic C Programming Guillaume Pierre Fall 2007 http://www.cs.vu.nl/ ∼ gpierre/courses/sysprog/ 1 Table of contents • 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . 2 • 2. Data types . . . . . . . . . . . . . . . . . . . . . . . . 14 • 3. Control Structures . . . . . . . . . . . . . . . . . . . . 25 • 4. Variables . . . . . . . . . . . . . . . . . . . . . . . . . 29 • 5. Functions . . . . . . . . . . . . . . . . . . . . . . . . . 36 • 6. The C Preprocessor . . . . . . . . . . . . . . . . . . . 43

  2. 1. Introduction 2 1. Introduction 1. Introduction 3 Literature ◮ Reader (C for Java programmers) ⊲ Available on the Web site ◮ Books ⊲ The C Programming Language (Kernighan, Ritchie) ⊲ The Complete Reference (Schildt) ⊲ The C Puzzle Book (Feuer) ⊲ Many others available.... ◮ Check on Amazon.com or Google

  3. 1. Introduction 4 Overview ◮ Very short history of C ◮ Overview of the differences between C and Java ◮ The C language (keywords, types, functies, etc.) ◮ Preprocessor ◮ Next lecture: pointers ◮ Then: memory management, files 1. Introduction 5 History of C ◮ Created in the mid-70’s as a follow up to the ’B’ and ’BCPL’ languages ◮ Standardized between 1983 and 1988 (ANSI C) ◮ Syntax is the basis for other languages like C++, Objective-C, Java and C#

  4. 1. Introduction 6 History of C ◮ Development of C is related to development of Unix ◮ Unix initially programmed using assembly language ⊲ Assembly instructions are very low-level ⊲ Assembly instructions are machine specific ◮ To make Unix more portable a high-level and portable language was needed ◮ The result was C 1. Introduction 7 C vs. Assembler ◮ Compared to Assembler, C is: ⊲ High level (C has functions, while and for loops, etc.) ⊲ Portable (only the C compiler must be ported for each platform, all applications stay the same)

  5. 1. Introduction 8 C vs. Java [1/2] ◮ C is procedural, not object oriented ⊲ C has no objects, interfaces or packages ⊲ A program only consists of functions and data ◮ C is compiled, not interpreted ⊲ Translated directly to assembly language ⊲ Faster, less portable and very hard to debug . ◮ C has no array bounds, null pointer or cast checks ⊲ You have to detect and handle all problems yourself ◮ C has no garbage collector ⊲ You have to do all of the memory management yourself 1. Introduction 9 C vs. Java [2/2] ◮ C has pointers ⊲ Similar to Java references but. . . ⊲ . . . they can be used in calculations (pointer arithmetic) ⊲ Allows you to use the location of data in computations (not just the value) ⊲ Useful, powerful and a debugging nightmare! ◮ Compared to Java, C is a low-level language ⊲ You can and must do everything yourself ⊲ Suitable for low-level software like device-drivers, communication libraries, operating systems, etc.

  6. 1. Introduction 10 C can be quite complex. . . This program computes and prints the first 800 decimals of π : long a=10000,b,c=2800,d,e,f[2801],g; int main(){ for(;b-c;)f[++b]=a/5; for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b); } ◮ Fortunately, you will never have to write such unreadable code ;-) 1. Introduction 11 Program Structure (1) A Java program consists of: A C program consists of: • Several classes, • Several functions in one class per file. any number of files. • A main method in one of • A main function in one of these classes. these files. • External class libraries (jar • Possibly some header files. files). • External libraries and their header files.

  7. 1. Introduction 12 Example program /* This is a comment */ #include <stdio.h> /* stdio.h is a standard header file */ int main() { /* this function is always called first */ printf("Hello, world!\n"); /* \n is the ’new line’ character */ return 0; /* main() should return 0 when it is finished */ } 1. Introduction 13 Compile and execute your program ◮ Compile your program like this: gcc -Wall hello.c ⊲ This creates an executable file called a.out ⊲ ’ -Wall ’ means ’please output warnings about my program’ ☞ It is better to always use this option. . . ☞ . . . and read the warnings! (even though they are sometimes hard to understand) ◮ Execute your program: $ a.out Hello, world! $ ◮ If you want to call your program with a different name: gcc -Wall -o my_program_name hello.c

  8. 2. Data types 14 2. Data types 2. Data types 15 Basic data types ◮ C knows the following data types: ⊲ char is a single character char x=’a’, y=’\n’, z=’\0’; - Characters are defined between simple quotes (’a’) - Double quotes (”a”) mean something different ⊲ short , int and long are short, normal and long integers int a=3, b=-2; long c=1234567890; ⊲ unsigned short , unsigned int and unsigned long are unsigned versions of short , int and long . ⊲ float and double are normal and double floating-point numbers: float d=3.14159, e=-3.01; f=6.022e23; ◮ Did you notice? There is no ’string’ data type here. . .

  9. 2. Data types 16 Arrays ◮ Defining an array is easy: int a[3]; /* a is an array of 3 integers */ ◮ Array indexes go from 0 to n-1 : a[0] = 2; a[1] = 4; a[2] = a[0] + a[1]; int x = a[a[0]]; /* what is the value of x? */ ⊲ Beware: in this example a[3] does not exist, but your compiler will not complain if you use it! - But your program may have a very strange behavior. . . ◮ You can create multidimensional arrays: int matrix[3][2]; matrix[0][1] = 42; 2. Data types 17 Strings ◮ A string is an array of characters: char hello[15]="Hello, world!\n"; ◮ Unlike in Java, you must decide in advance how many characters can be stored in a string. ⊲ You cannot change the size of the array afterwards ◮ Beware: strings are always terminated by a NULL character: ’ \ 0 ’ ⊲ For example, "Hello" is string of 6 characters: \ 0 H e l l o

  10. 2. Data types 18 Manipulating arrays ◮ You cannot copy an array into another directly ⊲ You must copy each element one at a time int a[3] = {12,24,36}; int b[3]; b = a; /* This will NOT work! */ b[0]=a[0]; b[1]=a[1]; b[2]=a[2]; /* This will work */ 2. Data types 19 Manipulating strings [1/2] ◮ There are standard function to manipulate strings: ⊲ strcpy(destination, source) will copy string source into string destination : char a[15] = "Hello, world!\n"; char b[15]; strcpy(b,a); ☞ Attention: strcpy does not check that destination is large enough to accomodate source . char c[10]; strcpy(c,a); /* This will get you in BIG trouble */

  11. 2. Data types 20 Manipulating strings [2/2] ◮ Instead of strcpy it is always better to use strncpy : ⊲ strncpy takes one more parameter to indicate the maximum number of characters to copy: char a[15] = "Hello, world! \ n"; char c[10]; strncpy(c,a,9); /* Why 9 instead of 10? */ 2. Data types 21 Structures ◮ You can build higher-level data types by creating structures: struct Complex { float real; float imag; }; struct Complex number; number.real = 3.2; number.imag = -2; struct Parameter { struct Complex number; char description[32]; }; struct Parameter p; p.number.real = 42; p.number.imag = 12.3; strncpy(p.description, "My nice number", 31);

  12. 2. Data types 22 Enumerations ◮ enum is used to create a number of related constants enum workdays {monday, tuesday, wednesday, thursday, friday }; enum workdays today; today = tuesday; today = friday; enum weekend {saturday = 10, sunday = 20}; 2. Data types 23 Data type sizes ◮ When doing systems programming it is often important to know how much memory is used for a variable. ⊲ Unfortunately, the size of data types depends on the machine you are working on: char short int long float double pointer Intel x86 1 2 4 4 4 8 4 AMD64 1 2 4 8 4 8 8 Sparc 1 2 4 4 4 8 4 ◮ To get the actual size of a type or a variable, use sizeof : char a[32]; int s1 = sizeof(char); int s2 = sizeof(s1); int s3 = sizeof(a); /* the size of a is 32 bytes */ int s4 = sizeof(Complex);

  13. 2. Data types 24 Using the right-sized integer ◮ Sometimes you really want to decide whether wou want an 8-bit, 16-bit or 32-bit integer ⊲ For example, when you need to process audio files. . . ◮ In this case (and this case only), use the following types: #include <sys/types.h> int8_t a; int16_t b; int32_t c; 3. Control Structures 25 3. Control Structures

Recommend


More recommend