Overview of C Language
Introduction
C is a powerful general-purpose programming language. It is fast, portable, and available on all platforms. C was developed in the early 1970s by Dennis Ritchie at Bell Labs and is still widely used today for system programming, creating operating systems, and other performance-critical applications.
History of C
The C programming language was originally developed for and implemented on the UNIX operating system by Dennis Ritchie. The language was derived from "B," which was created by Ken Thompson as a revision of the "BCPL" language. C has since become one of the most widely used programming languages of all time.
Features of C
C has several distinctive features:
- Simple and Efficient
- Rich Library
- Portability
- Extensible
- Middle-Level Language
- Memory Management
- Fast and Powerful
Basic Structure of a C Program
A simple C program consists of the following parts:
- Preprocessor Commands
- Functions
- Variables
- Statements & Expressions
- Comments
#include <stdio.h> int main() { printf("Hello, World!"); return 0; }
Compilation and Execution
To compile and execute a C program, follow these steps:
- Write the code in a text editor and save with a
.c
extension, for example,hello.c
- Open a terminal or command prompt
- Navigate to the directory where the file is saved
- Compile the program using a C compiler like GCC:
gcc hello.c -o hello
- Run the executable:
./hello
Hello, World!
Data Types in C
C supports several built-in data types:
int
: Integerfloat
: Floating-point numberdouble
: Double precision floating-point numberchar
: Charactervoid
: Special purpose type
Control Structures
C provides several control structures:
- Decision Making:
if
,if...else
,switch
- Loops:
for
,while
,do...while
- Jump:
break
,continue
,goto
Functions
Functions in C are used to divide the program into smaller parts. Each function performs a specific task. Here's an example of a function in C:
#include <stdio.h> void greet() { printf("Hello from a function!\n"); } int main() { greet(); return 0; }
Hello from a function!
Arrays
Arrays in C are used to store multiple values of the same type in a single variable. Here's an example:
#include <stdio.h> int main() { int numbers[5] = {1, 2, 3, 4, 5}; for(int i = 0; i < 5; i++) { printf("%d\n", numbers[i]); } return 0; }
1
2
3
4
5
Pointers
Pointers in C are used to store the address of variables. They are a powerful feature of the language. Here's an example of using pointers:
#include <stdio.h> int main() { int var = 20; int *ptr; ptr = &var; printf("Value of var: %d\n", var); printf("Address of var: %p\n", (void*)&var); printf("Value stored in ptr: %p\n", (void*)ptr); printf("Value pointed to by ptr: %d\n", *ptr); return 0; }
Value of var: 20
Address of var: 0x7ffedc2c5e0c
Value stored in ptr: 0x7ffedc2c5e0c
Value pointed to by ptr: 20