C
well-known master
Staff member
Owner
The C language was developed between 1969-1973 at Bell Labs (now Nokia Bell Labs) by Dennis Ritchie. It was originally created to develop the UNIX operating system running on DEC PDP-11 computers. C evolved from the B language created by Ken Thompson, which in turn was inspired by BCPL.
C became extremely popular because of:
✔ High performance (close to hardware)
✔ Portable (works on many platforms)
✔ Used in OS development (Linux, Windows kernels)
10. Pointers
C became extremely popular because of:
- Its high portability
- Its ability to access hardware-level features
- Its efficiency approaching assembly language
- Its structured yet flexible architecture
Dennis Ritchie - Creator of C
Dennis MacAlistair Ritchie (1941-2011) was an American computer scientist who created the C language and co-developed the UNIX operating system with Ken Thompson. For his contributions, Ritchie received numerous awards including the Turing Award (1983), National Medal of Technology (1998), and IEEE Richard W. Hamming Medal (1990).C Language Tutorial from Beginner to Advanced
1. C Programming Fundamentals
Basic C Program Structure
C:
#include <stdio.h> // Preprocessor directive
int main() { // Main function
// Statements
printf("Hello, World!\n");
return 0; // Return value
}
Basic Data Types
- int: integer numbers
- float: single-precision floating point
- double: double-precision floating point
- char: single character
- void: empty type
Variables and Constants
C:
int age = 25;
const float PI = 3.14159;
char initial = 'D';
2. Operators and Expressions
Arithmetic Operators
C:
int a = 10, b = 3;
int sum = a + b; // 13
int remainder = a % b; // 1
Relational and Logical Operators
C:
if (a > b && b != 0) {
printf("a is greater than b and b is not zero\n");
}
3. Control Structures
Conditional Statements
C:
if (score >= 70) {
printf("Passed\n");
} else if (score >= 50) {
printf("Remedial\n");
} else {
printf("Failed\n");
}
Loops
C:
// For loop
for (int i = 0; i < 10; i++) {
printf("%d ", i);
}
// While loop
int j = 0;
while (j < 10) {
printf("%d ", j);
j++;
}
4. Functions
C:
// Function declaration
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b;
}
// Function call
int result = add(5, 3);
5. Arrays and Strings
C:
// Array
int numbers[5] = {1, 2, 3, 4, 5};
// String
char greeting[] = "Welcome";
6. Pointers
C:
int var = 20;
int *ptr = &var; // ptr stores address of var
printf("Value of var: %d\n", *ptr); // Pointer dereferencing
7. Structures and Unions
C:
struct Student {
char name[50];
int id;
float gpa;
};
union Data {
int i;
float f;
char str[20];
};
8. Dynamic Memory Management
C:
int *arr = (int*)malloc(5 * sizeof(int)); // Allocation
if (arr == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
// Use memory
for (int i = 0; i < 5; i++) {
arr[i] = i * 2;
}
free(arr); // Free memory
9. File Handling
C:
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
printf("Failed to open file\n");
return 1;
}
fprintf(file, "Sample text for file\n");
fclose(file);
10. Advanced Concepts
Macros and Preprocessor
C:
#define SQUARE(x) ((x) * (x))
#define DEBUG 1
#if DEBUG
printf("Debug mode active\n");
#endif
Bit Manipulation
C:
unsigned int flags = 0;
flags |= 0x1; // Set bit 0
flags &= ~0x1; // Clear bit 0
int bit = (flags >> 3) & 0x1; // Get bit 3
Recursive Functions
C:
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
11. Modular Programming
Header File
C:
// mymath.h
#ifndef MYMATH_H
#define MYMATH_H
int add(int a, int b);
int subtract(int a, int b);
#endif
Implementation
C:
// mymath.c
#include "mymath.h"
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
12. Debugging and Optimization
Debugging Techniques
- Use strategic printf statements
- Utilize debuggers like gdb
- Check compiler warnings
- Use static analyzers
Code Optimization
- Avoid unnecessary loops
- Use efficient algorithms
- Minimize memory access
- Utilize register variables when appropriate
13. Best Practices
- Write clear comments
- Use descriptive naming
- Break code into small functions
- Handle errors properly
- Write portable code
Complete C Language Guide with Meanings and Uses
1. Introduction to C
What is C?
C is a procedural programming language developed in 1972 by Dennis Ritchie at Bell Labs. It is widely used for system programming, embedded systems, and low-level development due to its efficiency and control over hardware.Why Learn C?
✔ Foundation for other languages (C++, Java, Python)✔ High performance (close to hardware)
✔ Portable (works on many platforms)
✔ Used in OS development (Linux, Windows kernels)
2. Basic Structure of a C Program
Example Code:
C:
#include <stdio.h> // Header file for input/output functions
int main() { // Main function (entry point)
printf("Hello, World!\n"); // Print to console
return 0; // Exit program successfully
}
Explanation:
| Part | Meaning | Usage |
|---|---|---|
| #include <stdio.h> | Includes standard I/O library | Needed for printf(), scanf() |
| int main() | Program entry point | Where execution begins |
| printf() | Prints output | Displays text on screen |
| return 0; | Exits program | Indicates success |
3. Data Types in C
Primitive Data Types:
| Type | Meaning | Usage |
|---|---|---|
| int | Integer (whole numbers) | int age = 25; |
| float | Floating-point (decimal) | float pi = 3.14; |
| double | Double-precision float | double bigNum = 3.14159; |
| char | Single character | char letter = 'A'; |
| void | No type | Used in functions that return nothing |
Example:
C:
int age = 30; // Integer
float salary = 4500.50; // Floating-point
char grade = 'A'; // Character
4. Variables & Constants
Variables:
- Used to store data.
- Must be declared before use.
C:
int count = 10; // Integer variable
float price = 99.99; // Float variable
Constants:
- Fixed values that cannot change.
C:
const float PI = 3.14159; // Constant (read-only)
5. Input & Output (I/O)
Printing Output (printf)
C:
printf("Value: %d", 100); // %d = integer
printf("Name: %s", "Alice"); // %s = string
Reading Input (scanf)
C:
int age;
scanf("%d", &age); // Reads an integer
6. Operators
Arithmetic Operators
C:
int a = 10, b = 5;
int sum = a + b; // Addition (+)
int diff = a - b; // Subtraction (-)
int mul = a * b; // Multiplication (*)
int div = a / b; // Division (/)
int mod = a % b; // Modulus (%)
Comparison Operators
C:
if (a == b) // Equal to
if (a != b) // Not equal
if (a > b) // Greater than
if (a < b) // Less than
Logical Operators
C:
if (a > 0 && b > 0) // AND (both true)
if (a > 0 || b > 0) // OR (either true)
if (!(a == b)) // NOT (inverse)
7. Control Structures
If-Else
C:
if (age >= 18) {
printf("Adult");
} else {
printf("Minor");
}
Loops
For Loop
C:
for (int i = 0; i < 5; i++) {
printf("%d\n", i); // Prints 0 to 4
}
While Loop
C:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
8. Functions
Definition & Usage
C:
// Function declaration
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b;
}
// Function call
int result = add(5, 3); // Returns 8
9. Arrays & Strings
Arrays
C:
int numbers[5] = {1, 2, 3, 4, 5}; // Integer array
printf("%d", numbers[0]); // Prints 1
Strings
C:
char name[] = "John"; // String (array of chars)
printf("%s", name); // Prints "John"
[HR][/HR]
10. Pointers
Basics of Pointers
C:
int num = 10;
int *ptr = # // Pointer stores address of 'num'
printf("%d", *ptr); // Dereferencing (prints 10)
11. Structures
Defining a Structure
C:
struct Person {
char name[50];
int age;
};
// Usage
struct Person p1 = {"Alice", 25};
printf("%s is %d years old", p1.name, p1.age);
12. File Handling
Reading & Writing Files
C:
FILE *file = fopen("data.txt", "w"); // Open for writing
fprintf(file, "Hello, File!"); // Write to file
fclose(file); // Close file
13. Dynamic Memory Allocation
malloc() & free()
C:
int *arr = (int*)malloc(5 * sizeof(int)); // Allocates memory
arr[0] = 10;
free(arr); // Releases memory
C is a powerful, efficient, and widely used language for system programming, embedded systems, and game development. Mastering C helps in understanding how computers work at a low level and serves as a foundation for learning C++, Python, and other languages.Relate Threads
C
bởi Administrator,
Latest Threads
C
bởi Administrator,
