C Programming Notes – Unit 1 & Unit 2
Unit 1: Basics of Computer Systems and C Programming
1. Components of a Computer System:
- Memory: Stores instructions and data (RAM, ROM, Cache, Secondary storage).
- Processor (CPU): Executes instructions (ALU for arithmetic/logic, CU for control).
- I/O Devices: Input (keyboard, mouse) and output (monitor, printer).
- Storage: Hard disk, SSD, external drives for permanent storage.
- Operating System: Interface between hardware and user, manages resources.
- Assembler: Converts assembly code into machine code.
- Compiler: Translates entire source code into machine code at once.
- Interpreter: Translates and executes line by line.
- Loader: Loads programs into memory for execution.
- Linker: Links multiple object files into a single executable.
2. Algorithms, Flowcharts, and Pseudo Code:
- Algorithm: Step-by-step procedure to solve a problem.
- Flowchart: Graphical representation of an algorithm.
- Pseudo Code: Human-readable code-like representation.
Example: Largest of two numbers
Algorithm:
Step 1: Start
Step 2: Input A, B
Step 3: If A > B then Print A else Print B
Step 4: Stop
3. Programming Basics in C:
- Structure of a C Program:
#include
int main() {
// statements
return 0;
}
- Errors:
* Syntax Error: Mistake in code rules.
* Logical Error: Wrong logic but code runs.
- Source Code: Human-written code.
- Object Code: Compiled intermediate code.
- Executable Code: Final runnable program.
4. Components of C Language:
- Tokens: Keywords, identifiers, constants, operators, punctuation.
- Standard I/O: printf(), scanf().
- Fundamental Data Types: int, float, char, double.
- Variables: Named memory locations.
- Storage Classes: auto, static, extern, register.
Unit 2: Operators, Control Statements, and Loops
1. Operators and Expressions:
- Arithmetic Operators: +, -, *, /, %
- Relational Operators: <, >, <=, >=, ==, !=
- Logical Operators: &&, ||, !
- Bitwise Operators: &, |, ^, <<, >>
- Assignment Operator: =
- Operator Precedence: *, /, % > +, - > relational > logical.
2. Conditional Branching:
- if, if-else, nested if-else, switch-case.
Example:
if(a > b) {
printf("A is greater");
} else {
printf("B is greater");
}
3. Iteration (Loops):
- while, do-while, for loops.
- Multiple loop variables possible in for loop.
- break: exits loop.
- continue: skips current iteration.
- goto: jumps to a label (not recommended).
Examples:
- While loop (print 1 to 5):
int i=1;
while(i<=5) {
printf("%d", i);
i++;
}
- For loop (factorial):
int i, fact=1, n=5;
for(i=1; i<=n; i++) {
fact *= i;
}
printf("Factorial = %d", fact);