Lecture 2
C Programming Language
Before Start: Using Linux Ubuntu in the Cloud.
Lecture
Outline
- The C Language
- History
- Data types (built-in, structures)
- Functions
- Pointers, arrays, address arithmetic
- Memory allocation
- Strings
Theory
Builtin data types:
char
unsigned char
short
unsigned short
int
unsigned int
long
unsigned long
float
double
__int8_t
__int16_t
__int32_t
__int64_t
void *
size_t
Structures:
struct point {
int x;
int y;
};
struct point {
int x;
int y1 : 16;
int y2 : 16;
};
Size of various data types:
#include <stdio.h>
typedef struct {
int x;
int y;
} point_t;
int main() {
printf("sizeof(char) = %ld\n", sizeof(char));
printf("sizeof(int) = %ld\n", sizeof(int));
printf("sizeof(long) = %ld\n", sizeof(long));
printf("sizeof(float) = %ld\n", sizeof(float));
printf("sizeof(double) = %ld\n", sizeof(double));
printf("sizeof(void *) = %ld\n", sizeof(void *));
printf("sizeof(point) = %ld\n", sizeof(point_t));
return 0;
}
Input/output:
int x;
scanf("%d", &x);
printf("%d", x);
Functions and function pointers:
#include <stdio.h>
void print(int x, int y) {
printf("%d %d\n", x, y);
}
typedef void (* func_t)(int, int);
void test(func_t func) {
(*func)(10, 20);
}
int main() {
void (* func )(int, int) = &print;
(*func)(10, 20);
test(func);
return 0;
}
Dynamic memory allocation (use malloc
and free
):
#include <stdio.h>
int main() {
int i, n;
printf("Enter array size:\n");
scanf("%d", &n);
int* array = malloc(sizeof(int) * n);
printf("Enter array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
printf("Array:\n");
for (i = 0; i < n; i++) {
printf("array[%d] = %d\n", i, array[i]);
}
free(array);
return 0;
}
Workshop
Outline
- Discuss main features of C and differences between C++ and other languages
- Discuss questions from this document
Debugging C programs
Tasks
- Write a program in C, which does the following:
- defines the point structure;
- inputs an integer value
N
; - allocates an array of
N
points (assume the coordinates a non-negative); - inputs
N
points; - inputs a point;
- finds the point closest to the specified point in the array of points.
Notes: use
malloc
andfree
to allocate and deallocate the array respectively.
Homework
-
Write a program in C that inputs two integer values
x
andy
, call functionswap
that takes the values as arguments and swaps them, prints the values after the swap. - Write a program in C, which does the following:
- inputs an integer value āNā;
- allocates an array of āNā integer elements;
- fills the array with integer values from the standard input;
- reverses the array;
- prints the resulting array;
- dellocates the array.
Notes: use
malloc
andfree
to allocate and deallocate the array respectively. - Write a program in C, which does the following:
- inputs two integer values
N
andM
; - allocates a matrix of size
N * M
and fills it with values from standard input; - transposes the matrix;
- prints the resulting matrix;
- deallocate the matrices.
Note: the matrices must be allocated with
malloc
and deallocated withfree
. - inputs two integer values
- Write a program in C, which does the following:
- creates a singly-linked list;
- add to the list numbers from the standard input until user inputs
0
; - reverses the list;
- prints the resulting list;
- deallocates the list.
Note: use
malloc
andfree
to allocate and deallocate list entries respectively.
References
- Brian Harvey. CS 61C: C: Introduction, Pointers, & Arrays
- [KRC] Brian W. Kernighan, Dennis Ritchie. C Programming Language 2nd Edition. 1988.
- C programming language (Wikipedia)
- C data types
- Function Pointer in C
- Dynamic 2-D arrays in C
- C reference on cppreference.com