C Language Syntax & Notes
1. Introduction to C:
- Developed by Dennis Ritchie in 1972 at Bell Labs.
- General-purpose, procedural language.
- Foundation for many languages like C++, Java, Python.
- Still widely used in embedded systems, OS kernels, and compilers.
2. Basic Syntax:
Every C program has this general form:
#include <stdio.h> // Header file int main() { // Your code here return 0; }
3. Structure of a C Program:
1. Preprocessor Directives (#include, #define)
2. Global Declarations (variables, constants)
3. main() Function (entry point)
4. Other Functions
4. Data Types:
- int → integers (e.g., int age = 20;)
- float → decimal numbers (e.g., float price = 5.99;)
- char → single characters (e.g., char grade = 'A';)
- double → large decimal precision
- void → no return value
5. Variables & Constants:
- Variables store data and have types.
- Constants are fixed values (use 'const' keyword).
6. Operators:
- Arithmetic: + - * / %
- Relational: == != < > <= >=
- Logical: && || !
- Assignment: = += -= *= /=
- Increment/Decrement: ++ --
7. Control Flow Statements:
- if, if-else, nested if
- switch-case
- for, while, do-while loops
- break, continue, goto
8. Functions:
Syntax:
return_type function_name(parameters) { // code return value; } Functions make code reusable and
organized.
9. Arrays:
- 1D Array: int arr[5];
- 2D Array: int matrix[3][3];
10. Strings:
- Stored as char arrays.
- Use <string.h> functions like strlen(), strcpy(), strcmp().
11. Pointers:
- Variables that store memory addresses.
Example:
int a = 10; int *p = &a; - Pointer arithmetic (*, &).
12. Dynamic Memory Allocation:
- malloc(), calloc(), realloc(), free()
13. Structures:
struct Student { char name[20]; int age; }; - Groups different data types together.
14. File Handling:
- fopen(), fclose(), fprintf(), fscanf()
- Modes: r, w, a, rb, wb
15. Example Program:
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", #); printf("You
entered: %d", num); return 0; }