Download presentation
Presentation is loading. Please wait.
Published byBertina Jennings Modified over 8 years ago
1
Imperative Programming C
2
Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations, loops, conditionals, procedural abstraction Commands make a flowchart
3
Imperative Languages FORTRAN COBOL BASIC Pascal C
4
Pascal Example program Loops; var i: Integer; begin for i := 1 to 10 do begin Writeln('Hello'); Writeln('This is loop ',i); end; end.
5
C Example /*Hello, world” */ #include main() { printf(“Howdy, earth! \n”); return 0; }
6
C Printing Print strings directly Other types must be converted, because they are really numbers: char c = ‘A’; printf(“Print char c: %c.\n”,c); int n = 6; printf(“Print int n: %d.\n”,n);
7
Increment/Decrement (P.S. Same in Java) int x,y,z; x=y=z=1; y = x++; x=1; z = ++x; printf("x++ gives %d\n",y); printf("++x gives %d\n",z);
8
C-isms 0 is false, 1 is true (or non-zero) Same logical operators, &&, ||, ! But there are also bitwise operators &, |, ~ Other bitwise operators: ^, >>, << x ? y : z means “if x, y, else z” (Same as Java) char c = x > 0? ‘T’ : ‘F’
9
Memory Addresses (C) Pointers are addresses of where values are stored in memory. &variable means “address of variable” What happens? int a = 13; printf("a is %d\n",a); printf("&a is %p\n",&a);
10
Pointers in C int* ptr; -> ptr is a pointer to an int (with no pointee!) What does it do? int *ptr; int num = 4; ptr = #
11
More Pointer Fun in C int *ptr; int num = 4; ptr = # *ptr = 5;
12
Pointers in Pascal program Pointers; var p: ^integer; begin new(p); p^ := 3; writeln(p^); dispose(p); end.
13
C Arrays int arInt[5]; int arInt2[5] = {1,4,3,2,1}; int ar[2][3] = {{1,2,3},{4,5,6}};
14
C Strings char str[] = “I like C.”; char *ptrStr = “I love pointers.”; Last character in String is ‘\0’
15
C Strings – Example: Compute length of string int myStrLen(const char* s) { int count = 0; while (*s != ‘\0’) // while(*s){ { count++; s++; } return count; }
16
Preprocessor Directives #define – substitute before compiling: #define MAX 1000 if(x > MAX)…
17
C Scope Modifiers static: don’t delete from memory - permanent duration register: suggest use of register for variable. Can’t ask for address of the variable const: make a variable constant. When pointers are const, their “pointees” are constant
18
Memory malloc: allocates memory. Must give number of bytes wanted. malloc function returns a pointer to the memory. (Null ptr if memory error). free: frees memory. char *ptr; char str[] = “String!”; ptr = malloc(sizeof(char)*7); free(ptr);
19
Defining new types Struct (record in other langs) typedef struct{ char* word; int frequency; int size; } wordInfo; wordInfo wordOne; wordOne.word = “apple”; wordOne.frequency = 0; wordOne.size = 0;
20
Pointers to Structs We often use pointers to structs (do you know why? wordInfo *wiPtr; Can dereference explicitly: wordInfo wi = *wiPtr; Or shortcut straight to the fields: int f = wiPtr->frequency;
21
What C Lacks Iterators Exception Handling Overloading Generics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.