C Programming Notezs
====================
1. Introduction to C
C is a general-purpose, procedural programming language developed by Dennis Ritchie in 1972 at Bell Labs.
It is widely used for system software, embedded programming, and application development.
Key Features:
- Structured language
- Machine-independent
- Rich set of operators
- Supports low-level memory manipulation
2. Structure of a C Program
A C program generally includes the following parts:
1. Preprocessor Commands (e.g., #include <stdio.h>)
2. Global Declarations
3. main() function
4. Local Declarations
5. Statements & Expressions
6. Function Calls
Syntax:
#include <stdio.h>
int main() {
// statements
return 0;
}
3. Tokens in C
Basic building blocks of C program:
- Keywords: Reserved words like int, return, if, else
- Identifiers: Names of variables, functions
- Constants: Fixed values, e.g., 10, 'A'
- Variables: Storage locations for data
- Operators: Symbols for operations, e.g., +, -, *, /
- Special Symbols: Braces {}, semicolon ;, comma ,
4. Data Types and Variables
Primary Data Types: int, float, char, double
Derived Data Types: arrays, pointers, structures, unions
Syntax:
int a;
float b;
char c;
double d;
Constants:
- Literal Constants: 10, 3.14, 'A'
- Defined Constants:
#define PI 3.1416
5. Operators and Expressions
Types of Operators:
1. Arithmetic: +, -, *, /, %
2. Relational: ==, !=, >, <, >=, <=
3. Logical: &&, ||, !
4. Assignment: =, +=, -=, *=, /=
5. Increment/Decrement: ++, --
6. Conditional: ? :
Syntax:
int a = 5, b = 10;
int sum = a + b;
6. Input/Output Functions
printf(): Displays output
scanf(): Reads input
Syntax:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}
7. Decision Making
if statement:
if(condition) {
// statements
}
if-else statement:
if(condition) {
// statements
} else {
// statements
}
Nested if:
if(cond1) {
if(cond2) {
// statements
}
}
switch-case:
switch(expression) {
case 1:
// statements
break;
default:
// statements
}
8. Loops
for loop:
for(initialization; condition; increment) {
// statements
}
while loop:
while(condition) {
// statements
}
do-while loop:
do {
// statements
} while(condition);
9. Jump Statements
- break: Exit from loop or switch
- continue: Skip current iteration
- goto: Jump to a labeled statement
Syntax:
break;
continue;
goto label;
label: // statement
10. Arrays
1D Array:
int arr[5]; // array of 5 integers
2D Array:
int arr[3][4]; // 3 rows, 4 columns
11. Strings
A string is an array of characters terminated by '\0'
Syntax:
char str[20];
12. Functions
User-defined functions:
return_type function_name(parameters) {
// statements
return value;
}
Function call:
function_name(arguments);
Recursion:
int factorial(int n) {
if(n <= 1) return 1;
else return n * factorial(n-1);
}
13. Pointers
A pointer stores the address of a variable.
Syntax:
int a = 10;
int *p = &a;
14. Structures and Unions
Structure: Collection of different data types.
struct Student {
int id;
char name[20];
float marks;
};
Union: Same memory for all members.
union Data {
int i;
float f;
char c;
};
15. File Handling Basics
- fopen(): Open a file
- fclose(): Close a file
- fprintf()/fscanf(): Write/Read formatted data
- fread()/fwrite(): Read/Write binary data
Syntax:
FILE *fp;
fp = fopen("[Link]", "r");
fclose(fp);
16. Preprocessor Directives
- #include: Include header files
- #define: Define constants or macros
- #ifdef / #ifndef: Conditional compilation
Syntax:
#include <stdio.h>
#define PI 3.1416
End of C Programming Notezs