1 - C Language
1 - C Language
Jalal Kawash
2
Section Objectives
At the end of this section you will
1. Learn how to write programs in C
2. Use C pointers and athematic
3. Exploit I/O files
3
Outline
1. Basics
2. Functions
3. Branching and looping
4. Arrays
5. Pointers
6. Structures and memory allocation
7. Files
8. Mixing C and Assembly
4
5
Example C Program
6
Basic Operators
• Addition (a+b)
• Subtraction (a-b)
• Multiplication (a*b)
• Division (a/b)
• Modulus division (a%b)
9
10
Functions
11
Value Parameters
• Do not change outside the function
Output: 0, 0
12
Reference Parameters
• Change outside the function
*n is a pointer
= variable
holding the
address on n
&n = address
of n
Output: 8, 9
13
Command-line Arguments
• Mechanism to collect user input from the command line:
prog <list of arguments>
• The main method can have two arguments:
▫ argc = argument count
▫ argv = argument vector
int main(argc,argv)
int argc;
char *argv[];
{
…
}
14
Command-line Arguments
• argc is computed by the compiler
• argv[0] = program name
15
16
17
• Logical:
▫ AND: &&
▫ OR: ||
▫ NOT: !
18
If Statement
19
Switch Statement
20
Bitwise Operators
Examples
• value = eFlag & mask
• value = eFlag >> n
• value = eFlag << 3
• value ~= eFlag
• value |= eFlag
• value = (~0 ^ eFlag) << 5
22
While Loop
while (condition) {
body
}
23
24
Do-While Loop
do {
body
} while (condition);
Equivalent to:
body
while (condition) {
body
}
25
For Loop
for (initialization; condition; index update){
Body
}
int i;
for(i = 0; i < 100; i++)
printf("%d ",i);
26
27
Arrays
• To declare:
int y[100];
int x[10][20];
Pointers
• A variable that contains an address (for another
object in memory)
• The address operator & returns the address of a
variable
• The indirection operator * declares a pointer
variable
33
Pointers Example
Variable n at some address abc
int n = 100;
abc 100
int *ptr; Variable ptr at some address fgh
fgh ?
ptr = &n; fgh abc
Variable m at some address lmn
int m = *ptr
lmn 100
34
abc 100
Variable ptr at some address fgh
int *m; fgh abc
Variable m at some address lmn
• g = f or f = g
▫ Not allowed; compiler error
36
Pointer Arithmetic
int a[N], *ptr;
ptr = a; // array has two names now
ptr+n or a+n refer to a[n]
ptr + 1
ptr + 2
ptr + 3
41
ptr + 1
ptr + 2
ptr + 3
ptr + 4
ptr + 5
ptr + 6
ptr + 7
42
ptr + 1
43
Pointer Arithmetic
• A pointer may be displayed as an unsigned
variable
• Pointers can be incremented or decremented
ptr++;
ptr -= 2;
44
• *(fptr + 5) == a[5]
• *(++fptr + 5) == a[6]
45
4. Indirect operator
int n,m,*p1,*p2;
p1 = &n;
p2 = &m
*p1 = 20; \\ assign 20 to n
*p2 = 4 + (*p1)
\\ assign 4 + the value of n to m
48
Strings
• A string is a character array terminated with NULL
Pointers to Functions
• Declared as
type (*funcName)();
Pointers to Functions
#include <stdio.h>
int isEven(int n){ return !(n%2);}
int isOdd(int n){ return (n%2);}
int main() {
int (*foo)(int);
foo = &isEven;
int m = foo(2);
/* Also can be called as */
m = (*foo)(2);
foo = &isOdd;
m = foo(2);
return 0;
}
52
53
Structures
• Groups variables into a record
struct Student {
long int id;
char fname[20];
char lname[20];
};
Unions
• Similar to structures in syntax, but only holds the value
for one member only
• i.e., defines a singe variable
• The size is the size of the largest member
union Answers {
int answer1;
float answer2;
} someAnswers;
someAnswers.answer1 = 12;
someAnswers.answer2 = 15.5;
\\ erases answer1!
55
56
Memory Allocation
• Declaring a pointer to a buffer will only reserve
memory for the pointer, not the buffer
• Functions to allocate (reserve) memory in C:
▫ malloc()
▫ alloc()
▫ realloc()
▫ calloc()
▫ free()
57
ptr = malloc(size)
• Allocates size contiguous bytes
• ptr is a pointer to char type
• If allocation fails, malloc returns 0
• Include #stdlib.h
ptr = malloc(size);
58
ptr = (float *)
malloc((sizeof) float) * size);
▫
59
ptr = alloc(size)
ptr = realloc(ptr,newSize)
• Reallocates the old buffer of ptr to newSize
• Buffer can grow or shrink
ptr = malloc(size);
…
ptr = realloc(ptr, size*2);
61
ptr = calloc(n,objSize)
• Allocates a buffer of objects
• Used to allocate memory for non-simple types
▫ Such as structures
• n is the buffer size
• objSize is the size of individual object size
62
free(ptr)
Files
• Declare: FILE *inFile, *outFile;
• Open: inFile = fopen(“fork.c”,”r”);
outfile = fopen(“fork1.c”,”w”);
• Read: fscanf(inFile, “%c”, &ch);
• Write: fprintf(outFile, “%c”, ch);
• Close: fclose(inFile);
fclose(outFile);
65
Files
• EOF: Delimits the end of a file
while (ch = fgetc(inFile) != EOF) {…}