Notes on C Programming
1. Introduction to C
C programming language was developed by Dennis Ritchie at Bell
Labs in 1972.
It is a general-purpose, procedural, and structured
programming language.
It became the base for many modern languages such as C++, Java,
Python, and C#.
2. Features of C
1. Simple and Efficient: Easy syntax, closer to English.
2. Structured Language: Supports modular programming with
functions.
3. Portable: Code written in C can run on different machines with
minimal changes.
4. Mid-level Language: Combines the power of low-level assembly
language with the simplicity of high-level languages.
5. Rich Library: Many built-in functions (math, string, I/O).
6. Memory Management: Provides direct access to memory through
pointers.
7. Fast Execution: Closer to machine code.
3. Structure of a C Program
A typical C program contains:
#include <stdio.h> // Preprocessor directive
int main() { // Main function
printf("Hello, World!"); // Output statement
return 0; // Exit status
4. Basic Components of C
Tokens: Smallest unit of a C program (keywords, identifiers, operators,
constants).
Keywords: Reserved words (e.g., int, float, return, while).
Identifiers: Names for variables, functions, arrays.
Data Types:
o Primary: int, float, char, double.
o Derived: arrays, pointers, structures, unions.
Variables: Named memory locations to store data.
Constants: Fixed values that do not change.
Operators:
o Arithmetic (+, -, *, /, %)
o Relational (>, <, ==, !=)
o Logical (&&, ||, !)
o Assignment (=, +=, -=)
o Increment/Decrement (++/--)
5. Input/Output in C
printf() → For displaying output.
scanf() → For taking input.
Example:
int age;
printf("Enter age: ");
scanf("%d", &age);
6. Control Structures
1. Decision-making:
o if, if-else, nested if, switch-case.
2. Loops:
o for, while, do-while.
3. Jump Statements:
o break, continue, goto, return.
7. Functions in C
Definition: A block of code that performs a specific task.
Types:
o Library functions (printf, scanf, sqrt).
o User-defined functions (written by programmers).
Advantages: Reusability, modularity, debugging ease.
8. Arrays
A collection of elements of the same data type stored in contiguous
memory.
Types:
o One-dimensional arrays.
o Two-dimensional arrays (matrices).
o Multi-dimensional arrays.
9. Strings
An array of characters ending with '\0'.
Example:
char name[10] = "Alice";
Common string functions: strlen(), strcpy(), strcat(), strcmp().
10. Pointers
A pointer is a variable that stores the address of another variable.
Declared using * operator.
Example:
int x = 10;
int *p = &x;
printf("%d", *p); // Prints 10
Uses: Dynamic memory allocation, arrays, functions, structures.
11. Structures and Unions
Structure: Collection of variables of different data types grouped
together.
Union: Similar to structure but shares the same memory location.
12. File Handling in C
Allows reading/writing data from files.
Functions: fopen(), fclose(), fprintf(), fscanf(), fgetc(), fputc().
Modes: read (r), write (w), append (a).
13. Memory Management
malloc(), calloc(): Allocate memory.
free(): Release memory.
realloc(): Resize allocated memory.
14. Preprocessor Directives
Commands that are executed before compilation.
Example:
o #include (header files)
o #define (macros)
o #ifdef, #ifndef (conditional compilation).
15. Advantages of C
Portable and fast.
Easy to learn.
Powerful with pointers and memory access.
Rich set of operators and functions.
Basis for system programming (OS, compiler development).
16. Limitations of C
No object-oriented features (like inheritance, polymorphism).
Limited error handling.
Manual memory management (can lead to leaks).
17. Applications of C
Operating systems (UNIX, Linux kernel).
Embedded systems.
Compiler and interpreter development.
Database systems.
Gaming and graphics.
Networking software.
✅ Summary:
C is a powerful, structured, and mid-level language that bridges the
gap between machine-level and high-level programming. It remains one of
the most widely used languages for system software, embedded devices,
and performance-critical applications.