Fundamentals of C Programming Language
Compilation Process in Linux C
C Compilers
- GNU GCC
- Microsoft Visual C++ (MSVC)
- Apple Clang
- Intel C++ Compiler
- Default Linux compiler: cc
Programming Language Classification
Compiled Languages (C, C++, Java)
- Convert source code to machine instructions
- Perform type safety checks
- High performance
- Less flexible
Interpreted Languages (Python, JavaScript, Java)
- Execute source code directly in interpreter
- High flexibility and expressiveness
- Easier to learn
- Lower performance
Compilation Steps
C source files:
.hheader files: Contain declarations.csource files: Contain implementations
Example compilation command:
gcc hello.c -o hello
Breeakdown of compilation process:
- Preprocessing & Assembly
gcc -S hello.c -o hello.s - Compilation to Object File
gcc -c hello.s -o hello.o - Linking
gcc hello.o ... -o hello
C Language Basics
Variables and Types
// Variable declaration
int count;
double price;
// Constants
const int MAX_VALUE = 100;
Type System
- Integer types:
short,int,long,long long - Floating-point:
float,double - Character:
char - Boolean:
bool
Operators
- Arithmetic:
+,-,*,/,% - Bitwise:
&,|,^,<<,>>,~ - Logical:
&&,||,! - Relational:
==,!=,>,<,>=,<=
Control Structures
Conditional Statements
if (condition) {
// code block
} else if (condition) {
// code block
} else {
// code block
}
switch (variable) {
case value1:
// code
break;
default:
// code
}
Loops
// For loop
for (int i = 0; i < 10; i++) {
// code
}
// While loop
while (condition) {
// code
}
// Do-while loop
do {
// code
} while (condition);
Functions
// Function declaration
int add(int a, int b) {
return a + b;
}
Pointers
int num = 10;
int *ptr = #
printf("Value: %d", *ptr);
Strings
char str[] = "Hello";
char *ptr = "World";
Structures
struct Point {
int x;
int y;
};
struct Point p1 = {10, 20};
Memory Management
// Dynamic allocation
int *arr = malloc(10 * sizeof(int));
// Release memory
free(arr);