- Definition: C is a procedural programming language developed in the early 1970s. It is known for its simplicity, efficiency, and flexibility.
- Purpose: Designed for system programming, C is used to develop operating systems, embedded systems, and applications requiring direct hardware manipulation.
- Paradigm: Procedural (structured programming).
- Standardization: The ANSI C standard (C89/C90) and ISO C standard (C99, C11, C17, etc.) define the language.
- Created by: Dennis Ritchie at Bell Labs in 1972.
- Predecessor: C evolved from the B programming language, which was derived from BCPL.
- Influence: C influenced many modern languages, including C++, Java, C#, Python, and JavaScript.
- Standardization:
- ANSI C (C89/C90): First standardized version in 1989.
- C99: Introduced new features like
//comments, variable-length arrays, andbooltype. - C11: Added support for multithreading and atomic operations.
- C17: Minor updates and bug fixes.
- Procedural Language: Programs are divided into functions.
- Portability: C programs can run on different platforms with minimal changes.
- Efficiency: Provides low-level memory access and direct hardware manipulation.
- Modularity: Supports modular programming through functions and libraries.
- Extensibility: Allows the creation of custom libraries and functions.
- Rich Standard Library: Includes functions for input/output, string manipulation, memory management, and more.
C syntax is simple and structured, making it easy to learn and use. Below are some basics:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}- Basic Types:
int,float,double,char. - Derived Types:
array,pointer,struct,union. - Void Type:
void.
int age = 25;
float salary = 50000.50;
char grade = 'A';- Arithmetic:
+,-,*,/,%. - Relational:
==,!=,>,<,>=,<=. - Logical:
&&,||,!. - Bitwise:
&,|,^,~,<<,>>.
// If-Else
if (condition) {
// code
} else {
// code
}
// Switch
switch (expression) {
case 1:
// code
break;
default:
// code
}
// Loops
for (int i = 0; i < 10; i++) {
// code
}
while (condition) {
// code
}
do {
// code
} while (condition);int add(int a, int b) {
return a + b;
}int x = 10;
int *ptr = &x; // Pointer to x
printf("%d", *ptr); // Dereferencingint numbers[5] = {1, 2, 3, 4, 5};struct Person {
char name[50];
int age;
};
struct Person p1 = {"John", 30};- System Programming: Operating systems (e.g., Unix, Linux, Windows).
- Embedded Systems: Firmware for microcontrollers and IoT devices.
- Game Development: Game engines and performance-critical components.
- Compiler Development: Used to write compilers and interpreters.
- Databases: Core components of databases like MySQL and PostgreSQL.
- Networking: Network drivers and protocols.
- Efficiency: Provides fine-grained control over system resources.
- Portability: Code can be compiled and run on various platforms.
- Flexibility: Suitable for both low-level and high-level programming.
- Rich Library: Extensive standard library for common tasks.
- Community Support: Large community and abundant resources.
- No Object-Oriented Programming: Lacks support for OOP concepts like classes and objects.
- Manual Memory Management: Requires explicit allocation and deallocation of memory.
- Error-Prone: Lack of built-in safety features can lead to bugs like buffer overflows.
- Steep Learning Curve: Requires understanding of low-level concepts like pointers.
- C99: Introduced features like variable-length arrays,
//comments, andbooltype. - C11: Added support for multithreading, atomic operations, and anonymous structures.
- C17: Minor updates and bug fixes.
- Books:
- "The C Programming Language" by Brian Kernighan and Dennis Ritchie (K&R).
- "C Programming Absolute Beginner's Guide" by Perry and Miller.
- Online Tutorials:
- GeeksforGeeks, TutorialsPoint, Programiz.
- Practice Platforms:
- LeetCode, HackerRank, CodeChef.
- Continued Relevance: C remains essential for system programming and embedded systems.
- Integration with Modern Tools: C is often used alongside languages like Python and Rust.
- Performance-Critical Applications: C will continue to dominate areas requiring high performance and low-level control.
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n = 5;
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
for (int i = 0; i < n; i++) {
arr[i] = i + 1;
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}C is a foundational language that has stood the test of time. Its simplicity, efficiency, and versatility make it an essential tool for programmers, especially in system-level and performance-critical applications.