Chris J Michael cmicha1@lsu.edu 28 August 2008 C Language Summary Heavily Influenced by the GNU C Reference Manual: http://www.gnu.org/software/gnu-c-manual/
Introduction -C98, or the original ANSI C standard -Lower level than C++ & Java -gcc: The GNU C compiler 2
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; $ gcc hello.c -o hello.out $./hello.out Hello World $ _ 3
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Whitespace is ignored #include <stdio.h> int main(void){printf("hello World\n");return 0;} 4
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Include the library that provides printing functions 5
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Every C program must have a main function int main(void) The main function returns an integer value. It must be named main. In this case, it does not receive any parameters. 6
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; The scope encloses the function body 7
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Call the print function with the desired argument printf("hello World\n"); Call the function printf Send it the string to print End statements with a semicolon 8
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Call the print function with the desired argument printf("hello World\n"); Strings literalsare enclosed in double-quotes. \n represents a new line 9
Hello World #include <stdio.h> int main(void) { printf("hello World\n"); } return 0; Since main is defined to return an integer usually a zero is returned 10
Identifiers -Used to name variables, functions, and userdefined data types -A group of letters, decimal digits, _ -Cannot start with a number -Examples: foo my_var a1 Something_Clever_1 x C is case-sensitive! 11
Constants -Integer Constants: Octal Hexidecimal Equivalent -Character Constants: Enclosed in single-quotes -Real Number Constants: -String Constants: "It s a trap." "It s a" "It s\ a trap." 138 23 4002145 032 0x1f1f \ 'c' 'X' '\n' '\'' '\\' '\"' 3.14 5..1 0.2 500e9 3e-5 " trap." "It s\na\ntrap." "\"It s a trap\"" It s a trap. It s a trap. Exponent 12
Constants -Integer Constants: Octal Hexidecimal Equivalent -Character Constants: Enclosed in single-quotes -Real Number Constants: -String Constants: "It s a trap." "It s a" "It s\ a trap." 138 23 4002145 032 0x1f1f \ 'c' 'X' '\n' '\'' '\\' '\"' 3.14 5..1 0.2 500e9 3e-5 " trap." "It s\na\ntrap." "\"It s a trap\"" It s a trap. It s a trap. Exponent 13
Primitive Data Types Sizes may change from system to system! -Integer types unsigned char /* Holds a value from 0 to 256 */ signed char /* Holds a value from -127 to 128 */ char -Real number types /* May either be unsigned char or signed car depending on the system. This type should be used for character constants. */ int /* Holds a value from -2,147,483,648 to 2,147,483,647 */ unsigned int /* Holds a value from 00000000000000 to 4,294,967,647 */ float /* Single precision floating point */ double /* Double */ Enclose comments in these 14
Enumeration Definitions -Used to name integer values enum operating_system { BSD, /* The value of BSD will be 0 */ LINUX, /* LINUX will be 1, etc. */ MS_WIN, DOS, TOS }; Naming is optional enum { SLACKWARE=23, /* Here we set the first item to 23... */ FEDORA, /* so FEDORA will be 24, etc. */ GENTOO, DEBIAN }; 15
Structure Definitions -A programmer defined type made of variables and other data types. struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; }; You should provide a name Members are declared like variables are normally declared 16
-At definition struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; } particle_a, particle_b; -After definition struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; };... Structure Declarations This way is usually preferred struct particle particle_a, particle_b; 17
Structure Member Access -Use the member access operator, the '.' struct particle { int x, y, z; float velocity_x; float velocity_y; float velocity_z; };... struct particle particle_a, particle_b; particle_a.x = 4; particle_a.y = -4; particle_a.z = 1; particle_a.velocity_x = 1.0; particle_a.velocity_y = 12e3; particle_a.velocity_z = 0; 18
Structure Member Access -Of course, structures may contain other structures struct point { int x, y, z; }; struct velocity { float x, y, z; }; struct particle { struct point p; struct velocity v; };... struct particle particle_a; particle_a.p.x = 4; particle_a.v.x = 1.0; 19
Array Declarations and Access -Specify type and amount when declaring Multidimensional, 10x10 array int my_int_array[10]; struct particle particles[10][10]; -Specify array element when accessing my_int_array[0] = 9; /* The first element */ my_int_array[9] = 46; /* The last element */ particles[3][7].p.x = 4; This structure was defined in the last slide Careful not to overstep array boundaries! The compiler may not warn you about this! my_int_array[10] = 6; my_int_array[-1] = 0; 20
Strings -Strings are just arrays of characters char my_string[20] = "Hello World"; Initialization H e l l o W o r l d \0 my_string[5] = '\0'; The null character Hello World H e l l o \0 W o r l d \0 Hello You cannot assign string literals after initialization! my_string = "Hello World"; But don't worry! There are some library functions that help us copy string literals into arrays. More on that later. 21
Strings -Strings are just arrays of characters char my_string[20] = "Hello World"; Initialization H e l l o W o r l d \0 my_string[5] = '\0'; The null character Hello World H e l l o \0 W o r l d \0 Hello You cannot assign string literals after initialization! my_string = "Hello World"; But don't worry! There are some library functions that help us copy string literals into arrays. More on that later. 22
Strings -Strings are just arrays of characters char my_string[20] = "Hello World"; Initialization H e l l o W o r l d \0 my_string[5] = '\0'; The null character Hello World H e l l o \0 W o r l d \0 Hello You cannot assign string literals after initialization! my_string = "Hello World"; But don't worry! There are some library functions that help us copy string literals into arrays. More on that later. 23
Pointers There is nothing difficult here, just keep the basics in mind. 24
Pointers -Pointers hold memory addresses of stored variables -You can create a pointer for any data type. -The two basic operators: Indirection Address 25
Pointers These are the contents of the memory locations -Keep the memory model in mind These are addresses of the memory locations The denotes uninitialized or junk data in memory 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 26
Pointer Declarations -To declare a pointer of any type, simply use the indirection operator before the 0x00 identifier int *p_to_int; float *p_to_f1, *p_to_f2; -Use caution when declaring multiple pointers at once int *p1, *p2, i1; These are pointers This is an integer 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 27
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 28
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 0x08 0x09 29
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 30
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 0x05 31
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ Don't let this initialization confuse you. All we're really doing is int *p;... p = &i; 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 0x05 32
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 0 0x06 0x07 1 0x08 0x09 0x05 33
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 4 0x06 0x07 1 0x08 0x09 0x05 34
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 4 0x06 0x07 1 0x08 0x09 0x07 35
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; 0x00 0x01 0x02 0x03 0x04 0x05 4 0x06 0x07 7 0x08 0x09 0x07 36
Pointer Access -Use the address operator to set a pointer int i = 0; /* Will be located at 0x05 */ int j = 1; /* Will be located at 0x07 */ int *p = &i; /* Will be located at 0x09 */ *p = 4; p = &j; *p = 7; i = *p; This is called a dereference, it obtains the value stored at the memory location the pointer holds 0x00 0x01 0x02 0x03 0x04 0x05 7 0x06 0x07 7 0x08 0x09 0x07 37
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 38
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l 0x00 0x01 0x02 0 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x09 39
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l 0x00 0x01 0x02 0 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 40
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; Indirect member access operator 0x00 0x01 0x02 0 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 41
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; 0x00 0x01 0x02 2 0x03 1 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 42
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; p->y is the same as (*p).y 0x00 0x01 0x02 2 0x03 4 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 43
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; l.x = p->z; 0x00 0x01 0x02 2 0x03 4 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 44
-Similar concept Pointers to Structures struct loc { int x, y, z; }; struct loc l; /* Will start at 0x02 */ struct loc *p; /* Will be at 0x08 */ l.x = 0; l.y = 1; l.z = 0; p = &l p->x = 2; p->y = 4; l.x = p->z; 0x00 0x01 0x02 0 0x03 4 0x04 0 0x05 0x06 0x07 0x08 0x02 0x09 45
Type and Storage Specifiers -Common Type Specifier: const const float pi = 3.14159; pi is declared as read only -Common Storage Specifier: extern extern int seconds;... Must have an "extern" and "non-extern" declaration int seconds = 3600; seconds is visible to all files linked the your project 46
Expressions -An expression is one operand and zero or more operators 13 constants may be operands 36 + 16 addition operator calculate_sum(1, 1) functions with return values may be operands (8 * (12 + 4)) use parentheses to group expressions -- innermost expressions are evaluated first 47
Unary Operators Increment: ++x x++ Decrement: --x x-- Positive: +x Negative: -x Logical Negation:!x Bitwise Negation: ~x Address: &x Indirection: *x Size of: sizeof(x) Type casting: (int)x (float)x (type)x Array Subscript: x[3] 48
Binary Operators Addition: x + y Subtraction: x - y Multiplication: x * y Be careful about types Division: x / y Modulus: x % y Bit shift: x << y Bitwise AND: x & y Bitwise OR: x y Bitwise XOR: x ^ y Comparison: x == y x!= y x < y x <= y x > y x >= y Logical: x && y x y Assignment: x = y Compound Assignment: x += y x -= y x *= y x /= y etc. 49
Binary Operators Comma: x, y Member Access: x.y x->y The Ternary Operator x? y : z 50
Next Time -A closer look at the highlighted operators -Statements: actions and control flow -Functions -Program structure -Helpful code snippets 51
52