C Programming
C Programming
## 1. Fundamentals of C Programming
### Introduction
C programming is a powerful and versatile language widely used in
systems and application software, embedded systems, and
resource-constrained applications. Understanding the basics of C
programming lays a solid foundation for developing efficient and
effective programs.
### Data Types
Data types in C define the type of data a variable can hold. They are
broadly classified into primitive and derived data types.
#### Primitive Data Types
1. Integers: Used to store whole numbers.
- `int`: Standard integer type.
- `short int`: Short integer type.
- `long int`: Long integer type.
- `unsigned int`: Unsigned integer type.
Code Example
int a = 10;
unsigned int b = 20;
2. Floating-Point Numbers: Used to store real numbers.
- `float`: Single-precision floating point.
- `double`: Double-precision floating point.
- `long double`: Extended-precision floating point
Code Example
float c = 10.5f;
double d = 20.5;
_____________________________________________
3. Characters: Used to store single characters.
- `char`: Character type.
Code Example
char e = 'A';
_____________________________________________
4. Booleans: Represents true or false values (using `<stdbool.h>`).
Code Example
#include <stdbool.h>
bool f = true;
_____________________________________________
#### Derived Data Types
1. Arrays: Collection of elements of the same type.
- One-Dimensional Array:
Code Example
int arr[5] = {1, 2, 3, 4, 5};
_____________________________________________
- Multi-Dimensional Array:
Code Example
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
_____________________________________________
2. Structures: User-defined data type that groups related variables
of different types.
Code Example
struct Person {
char name[50];
int age;
float salary;
};
struct Person person1;
_____________________________________________
#### do-while
Code Example
do {
// code to be executed
} while (condition);
_____________________________________________
### Jump Statements
#### break
Code Example
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
// code to be executed
}
_____________________________________________
#### continue
Code Example
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
// code to be executed
}
_____________________________________________
#### goto
Code Example
if (condition) {
goto label;
}
// code to be skipped
label:
// code to be executed
_____________________________________________
#### return
Code Example
int function() {
return value;
}
_____________________________________________
## 4. Standard Library
### Input/Output
#### printf()
Code Example
printf("Hello, World!\n");
_____________________________________________
#### scanf()
Code Example
scanf("%d", &a);
_____________________________________________
#### getchar()
Code Example
int c = getchar();
_____________________________________________
#### putchar()
Code Example
putchar(c);
_____________________________________________
### Memory Management
#### malloc()
Code Example
int *ptr = (int *)malloc(10 * sizeof(int));
_____________________________________________
#### calloc()
Code Example
int *ptr = (int *)calloc(10, sizeof(int));
_____________________________________________
#### realloc()
Code Example
ptr = (int *)realloc(ptr, 20 * sizeof(int));
_____________________________________________
#### free()
Code Example
free(ptr);
_____________________________________________
### Mathematical Functions
#### sqrt()
Code Example
#include <math.h>
double result = sqrt(25.0);
_____________________________________________
#### pow()
Code Example
double result = pow(2.0, 3.0);
_____________________________________________
#### log()
Code Example
double result = log(2.0);
_____________________________________________
#### Trigonometric Functions
Code Example
double result = sin(45.0);
_____________________________________________
### String Handling
#### strlen()
Code Example
size_t len = strlen("Hello");
_____________________________________________
#### strcpy()
Code Example
char dest[50];
strcpy(dest, "Hello");
_____________________________________________
#### strcat()
Code Example
char dest[50] = "Hello";
strcat(dest, " World");
_____________________________________________
#### strcmp()
Code Example
int result = strcmp("Hello", "World");
_____________________________________________
### Time and Date
#### time()
Code Example
time_t t;
time(&t);
_____________________________________________
#### clock()
Code Example
clock_t start = clock();
_____________________________________________
#### localtime()
Code Example
struct tm *local = localtime(&t);
_____________________________________________
#### strftime()
Code Example
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", local);
_____________________________________________
## 5. Advanced Concepts
### Preprocessor
#### #define
Code Example
#define PI 3.14159
_____________________________________________
#### #include
Code Example
#include <stdio.h>
_____________________________________________
#### Conditional Compilation
Code Example
#ifdef DEBUG
printf("Debugging\n");
#endif
_____________________________________________
### Functions
#### Recursion
Code Example
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
_____________________________________________
### Pointers
#### Pointer Arithmetic
Code Example
int *p;
p++;
_____________________________________________
#### Pointers and Arrays
Code Example
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
_____________________________________________
#### Pointers and Structures
Code Example
struct Person *ptr;
_____________________________________________
### File I/O
#### fopen()
Code Example
FILE *
fp = fopen("file.txt", "r");
_____________________________________________
#### fread()
Code Example
fread(buffer, sizeof(char), sizeof(buffer), fp);
_____________________________________________
#### fwrite()
Code Example
fwrite(buffer, sizeof(char), sizeof(buffer), fp);
_____________________________________________
#### fclose()
Code Example
fclose(fp);
_____________________________________________
### Dynamic Memory Allocation
Covered under memory management.
### Structures and Unions
#### Defining Structures
Code Example
struct Person {
char name[50];
int age;
float salary;
};
_____________________________________________
#### Accessing Structure Members
Code Example
person1.age = 25;
_____________________________________________
#### Nested Structures
Code Example
struct Company {
char name[50];
struct Person CEO;
};
_____________________________________________
#### Unions
Covered under derived data types.
### Bit Manipulation
#### Bit Fields
Code Example
struct {
unsigned int age : 3;
} Age;
_____________________________________________
### Preprocessor Directives
#### #ifdef, #ifndef, #endif
Code Example
#ifdef DEBUG
// debug code
#endif
_____________________________________________
## 6. Programming Paradigms
### Procedural Programming
Focuses on procedures or routines.
### Structured Programming
Uses clear, structured blocks of code.
### Object-Oriented Programming (OOP)
#### Classes
Not natively supported in C, but can be simulated using structures
and function pointers.
#### Objects
Instances of classes (structures).
#### Inheritance
Not directly supported, but can be simulated.
#### Polymorphism
Achieved through function pointers.
#### Encapsulation
Using structures and functions.
## 7. Development Tools
### Compilers
#### GCC
Popular open-source compiler.
##Shell Command
gcc program.c -o program
_____________________________________________
#### Clang
Another open-source compiler.
##Shell Command
clang program.c -o program
_____________________________________________
##Shell Command
gdb program
_____________________________________________
#### Visual Studio Debugger
Integrated with Visual Studio IDE.
#### LLDB
LLVM debugger.
##Shell Command
lldb program
_____________________________________________
### IDEs
#### Visual Studio
Popular IDE for Windows.
#### Code::Blocks
Open-source, cross-platform IDE.
#### Eclipse
Popular for Java, also supports C/C++.
#### NetBeans
Another versatile IDE.