Introduction to C Language
C is one of the most powerful and widely used programming languages in computer science. It is known as the “mother of all programming languages” because many modern languages such as C++, Java, Python, and JavaScript are either directly or indirectly influenced by C. Developed in the early 1970s, C remains popular due to its efficiency, portability, and flexibility.
History and Evolution of C
C was developed in 1972 by Dennis Ritchie at Bell Laboratories. It was created as an evolution of the B programming language to support system software development, especially for the UNIX operating system.
Key milestones:
- 1960s: Assembly language and machine code dominated programming.
- 1969: Ken Thompson developed B programming language.
- 1972: Dennis Ritchie developed C.
- 1989: ANSI C standardization (ANSI X3.159-1989).
- 1999: C99 standard introduced new features.
- 2011 and beyond: C11 and C18 standards refined the language further.
Features of C
C has several features that make it a foundational programming language:
- Simple and efficient.
- Mid-level language (supports both low-level memory management and high-level constructs).
- Structured programming approach.
- Rich library functions.
- Portable across different hardware platforms.
- Supports dynamic memory allocation.
- Powerful pointer manipulation.
Structure of a C Program
A C program follows a specific structure.
#include <stdio.h> // Preprocessor directive
int main() { // Main function
printf("Hello, World!"); // Output statement
return 0; // Exit status
}
Basic structure includes:
- Preprocessor directives (
#include). - Main function (
int main()). - Declarations and statements.
- Return statement.
Basic Syntax and Commands
Some basic commands and syntax in C:
- Printing output:
printf("Hello, C Language\n");
- Reading input:
int x;
scanf("%d", &x);
- Comments:
// Single-line comment
/* Multi-line
comment */
- Compile and run (command line):
gcc program.c -o program
./program
Data Types & Variables
C provides multiple data types to store different values.
- Basic data types:
- int → integers
- float → decimal numbers
- double → large decimal numbers
- char → characters
Example:
int age = 20;
float price = 99.5;
char grade = 'A';
- Variables: Named storage locations used in programs.
int num = 100;
Operators in C
C has various operators:
- Arithmetic operators:
+ - * / % - Relational operators:
== != < > <= >= - Logical operators:
&& || ! - Assignment operators:
= += -= *= /= - Increment/Decrement:
++ --
Example:
int a = 5, b = 3;
printf("%d", a + b); // Output: 8
Control Statements (if, switch, loops)
Control statements manage the flow of execution.
- if-else statement:
if (x > 0) {
printf("Positive");
} else {
printf("Negative or Zero");
}
- switch statement:
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid");
}
- Loops:
// for loop
for(int i=1; i<=5; i++) {
printf("%d ", i);
}
// while loop
int j=1;
while(j<=5) {
printf("%d ", j);
j++;
}
// do-while loop
int k=1;
do {
printf("%d ", k);
k++;
} while(k<=5);
Functions in C
Functions allow code reusability.
#include <stdio.h>
int add(int a, int b) { // Function definition
return a + b;
}
int main() {
int result = add(5, 3);
printf("Sum = %d", result);
return 0;
}
Arrays, Strings, and Pointers
- Arrays: Collection of similar data types.
int arr[5] = {1, 2, 3, 4, 5};
- Strings: Character arrays ending with
\0.
char name[] = "C Language";
printf("%s", name);
- Pointers: Variables storing memory addresses.
int x = 10;
int *ptr = &x;
printf("%d", *ptr); // Prints 10
Structures & Unions
- Structure:
struct Student {
int id;
char name[20];
};
struct Student s1 = {1, "John"};
- Union: Shares memory for all members.
union Data {
int i;
float f;
char c;
};
File Handling in C
C allows file operations such as read, write, and append.
FILE *fptr;
fptr = fopen("data.txt", "w");
fprintf(fptr, "Hello File Handling");
fclose(fptr);
Memory Management (malloc, free, etc.)
Dynamic memory is managed with functions from <stdlib.h>.
int *ptr;
ptr = (int*) malloc(5 * sizeof(int));
if(ptr != NULL) {
for(int i=0; i<5; i++) {
ptr[i] = i+1;
}
}
free(ptr);
Advantages & Limitations
- Advantages:
- Fast execution.
- Portable across systems.
- Supports low-level programming.
- Large community support.
- Limitations:
- No built-in object-oriented features.
- Manual memory management required.
- Limited error handling compared to modern languages.
Real-Life Applications
C is widely used in:
- Operating systems (UNIX, Linux).
- Embedded systems.
- Game development.
- Compilers and interpreters.
- Database systems.
- System drivers and hardware control.
Sample Programs (with commands & output)
- Factorial Program:
#include <stdio.h>
int main() {
int n, fact = 1;
scanf("%d", &n);
for(int i=1; i<=n; i++) {
fact *= i;
}
printf("Factorial = %d", fact);
return 0;
}
- Reverse a string:
#include <stdio.h>
#include <string.h>
int main() {
char str[50];
scanf("%s", str);
for(int i=strlen(str)-1; i>=0; i--) {
printf("%c", str[i]);
}
return 0;
}
Conclusion
C language remains a cornerstone of computer programming. It is not only fast and efficient but also lays the foundation for learning advanced languages like C++, Java, and Python. With its structured approach, direct hardware manipulation, and portability, C continues to be a relevant and powerful language in modern computing.
















