C_Programming_Notes
C_Programming_Notes
Features:
- Portable.
- Structured language.
Advantages:
- Fast execution
- Portable code
Disadvantages:
- Less user-friendly
Examples:
- int a = 10;
- float b = 3.14;
- char c = 'A';
- double d = 12.3456;
Types:
- Local Variable
- Global Variable
- Static Variable
- Automatic Variable
- External Variable
Types:
- Arithmetic (+, -, *, /)
Break:
- Used in loops/switch
Continue:
- Used in loops
While Loop:
- Syntax: while(condition)
Do While Loop:
- Syntax: do { } while(condition);
Functions:
- strlen(str)
- strcpy(dest, src)
- strcmp(s1, s2)
- strcat(s1, s2)
9) Functions: Features and Types
Features:
Types:
- User-defined
Example:
int factorial(int n) {
if(n == 0) return 1;
Auto:
- No keyword
- Limited scope
- Fast
External:
- Exists throughout
- Uses extern
- Global access
- Slower
Example:
struct Student {
int id;
char name[20];
float marks;
};
Array:
- Access by index
- Contiguous memory
- Simple use
Structure:
- Dot operator
- Varied memory
- Flexible
- Record storage
14) Structure vs Union
Structure:
- Separate memory
- Multi-value store
- Slower
- struct Student
Union:
- Shared memory
- Faster
- union Data
Array:
- Fixed size
- Multiple elements
- Cannot reassign
- Static
- int arr[10];
Pointer:
- Dynamic memory
- Holds address
- Reassignable
- Flexible
- int *ptr;
16) Call by Value vs Call by Reference
Call by Value:
- Pass copy
- No original change
- Safe
- Slower
- func(a);
Call by Reference:
- Pass address
- Changes original
- Risky
- Faster
- func(&a);
Example:
int a = 10;
int *p = &a;
printf("%d", *p);
Structure:
- Stores data
- Dot operator
- Multiple members
- Memory for data
- struct Student
Pointer:
- Stores address
- One variable
- int *p;
Sequential:
- In order
- Slower
- Simple
- Text files
- fgets()
Random:
- Direct access
- Faster
- Complex
- Databases
- fseek()
fprintf():
- Write to file
- fprintf(fp, ...)
- Output
- Like printf
fscanf():
- fscanf(fp, ...)
- Input
- Like scanf