C Programming Notes
C Programming Notes
Programming in C
History of C
- C is known for its efficiency, portability, and ease of use for system-level programming.
- It is procedural, meaning the code is organized into functions that carry out specific tasks.
Structure of C Programs
1. Compilation: The source code (.c file) is converted to machine code by a compiler.
2. Execution: The machine code (usually a .exe file) is run on the computer.
Debugging Techniques
- Debugging is the process of finding and fixing errors in the program.
- int (4 bytes)
- float (4 bytes)
- char (1 byte)
- double (8 bytes)
Example:
Declaration of Variables
int x;
float y;
Modifiers
- Modifiers like short, long, signed, and unsigned adjust the range or storage size of the variable.
Example:
Symbolic Constants
Example:
#define PI 3.14
Storage Classes
Enumerations
Example:
Command-Line Parameters
- Parameters passed to main() when the program is executed from the command line.
printf("%s", argv[1]);
}
Macros
Example:
The C Preprocessor
- Preprocessor commands like #include and #define are executed before compilation.
Operators
Unary Operators
Example:
int x = 5;
x++; // Increment x by 1
- Arithmetic: +, -, *, /, %
Example:
int x = 5 + 3;
Bitwise Operators
Example:
int x = 5 & 3; // Bitwise AND
Assignment Operators
Example:
int x = 5;
x += 2; // x = x + 2
Conditional Expressions
- Short if-else: ? :
Example:
int x = (a > b) ? a : b;
Control Statements
if-else
if (x > 0) {
printf("Positive");
} else {
printf("Negative");
switch
- Selects one of many code blocks to execute.
switch (x) {
goto statement
goto label;
label: printf("Jumped");
Loops
for
while
do-while
Functions
- Declaration:
- Definition:
- Call:
Recursive Functions
Example:
int factorial(int n) {
if (n == 0) return 1;
}
Multifile Programs
Arrays
int matrix[3][3];
Self-Referential Structures
Pointer to Pointer
int **ptr;
Array of Pointers
int *arr[5];
Function of Pointers
File Handling in C
- File handling is done using functions like fopen(), fread(), fwrite(), etc.