Programming in C
Overview of C
Concise notes covering the language, program development phases, importance, program structure, a minimal sample program, compilation and execution commands, key concepts, programming style, and a short debugging example.
The Language of C
- C is a structured, procedural, compiled language created by Dennis Ritchie.
- Main properties relevant for exams: portability, efficiency, manual memory control via pointers.
Phases of Developing a C Program
- Edit: write source file `program.c`.
- Compile: `gcc -Wall -c program.c` produces `program.o` and shows compile errors/warnings.
- Link: `gcc program.o -o program` produces executable.
- Run: `./program` on Unix-like systems.
Importance of C
- Foundation for system software and many languages.
- Used for OS, compilers, embedded systems because of speed and low-level access.
Basic Structure of a C Program
- Preprocessor directives: `#include`, `#define`.
- Function definitions with `main()` as entry point.
- Variable declarations and statements.
- Return from `main` typically `return 0;` to signal success.
Minimal example
#include <stdio.h>
int main(void) {
int a = 10;
printf("Value of a = %d
", a);
return 0;
}Compiling and Executing
- Compile and link in one step:
gcc program.c -o program. - Enable warnings:
gcc -Wall program.c -o program. - Run executable:
./program. - Typical compile-time errors: syntax error, undeclared identifier, type mismatch.

Key Concepts
- Data types and range: `int`, `float`, `double`, `char`.
- Operators: arithmetic, relational, logical, bitwise.
- Control flow: `if`, `if-else`, `switch`, loops (`for`, `while`, `do-while`).
- Functions: declaration, definition, call, parameter passing (by value).
- Arrays and strings: contiguous memory, null-terminated char arrays.
- Pointers: address-of `&`, dereference `*`, pointer arithmetic basics.
Programming Style (exam checklist)
- Indent consistently and use meaningful names for variables and functions.
- Comment nontrivial logic; avoid redundant comments for obvious code.
- Check return values for I/O functions when necessary.
- Use compiler warnings to find potential bugs.
Common Debugging Points
Typical mistakes to spot
- Missing ampersand in `scanf` call: use
scanf("%d", &x). - Forgotten semicolon after statements.
- Using uninitialized variables.
- Buffer overflow with strings; always ensure space for null terminator.
Faulty snippet
#include <stdio.h>
int main() {
int a;
printf("Enter number: ");
scanf("%d", a); // wrong: should be &a
printf("Value is: %d", a) // wrong: missing semicolon
return 0;
}Corrected
scanf("%d", &a);
printf("Value is: %d", a);