3. C Fundamentals
Variables, constants, data types, and scope -- the building blocks of every C program.
Variables and scope
- Declaration:
int x; - Initialization:
int y = 10; - Scope: block (local), file (static), global
Constants
- Literals:
42,3.14,'a',"hello" #define PI 3.14159const int DAYS = 7;
Data types
- Primary:
char,int,float,double,void - Qualifiers:
short,long,signed,unsigned - Derived: arrays, pointers, functions
- User-defined:
struct,union,enum
Examples
Swap Two Numbers (swap_temp.c)
Demonstrates variable declaration, initialization, and assignment:
#include <stdio.h>
int main(void) {
int a, b, temp;
printf("Enter two integers (a b): ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swap: a=%d, b=%d\n", a, b);
return 0;
}
Add Two Numbers (add_two_numbers.c)
Shows basic data types (int), function return values, and formatted output:
#include <stdio.h>
int add(int a, int b) { return a + b; }
int main(void) {
printf("add(3,4) = %d\n", add(3,4));
return 0;
}