0% found this document useful (0 votes)
6 views

C Programming

Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Programming

Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C Programming: A Comprehensive Guide

## 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;
_____________________________________________

3. Unions: Similar to structures but with a key difference: all


members share the same memory location.
Code Example
union Data {
int i;
float f;
char str[20];
};
union Data data;
_____________________________________________
4. Pointers: Variables that store the address of another variable.
Code Example
int *p;
int x = 10;
p = &x;
_____________________________________________
## 2. Operators in C
Operators are symbols that perform operations on variables and
values.
### Arithmetic Operators
Used to perform basic arithmetic operations.
Code Example
int a = 10, b = 20;
int sum = a + b; // Addition
int diff = a - b; // Subtraction
int prod = a * b; // Multiplication
int quotient = a / b; // Division
int remainder = a % b;// Modulus
_____________________________________________
### Relational Operators
Used to compare two values.
Code Example
if (a == b) // Equal to
if (a != b) // Not equal to
if (a > b) // Greater than
if (a < b) // Less than
if (a >= b) // Greater than or equal to
if (a <= b) // Less than or equal to
_____________________________________________
### Logical Operators
Used to combine conditional statements.
Code Example
if (a && b) // Logical AND
if (a || b) // Logical OR
if (!a) // Logical NOT
_____________________________________________
### Bitwise Operators
Operate on bits and perform bit-by-bit operations.
Code Example
int result = a & b; // AND
result = a | b; // OR
result = a ^ b; // XOR
result = ~a; // NOT
result = a << 1; // Left shift
result = a >> 1; // Right shift
_____________________________________________
### Assignment Operators
Used to assign values to variables.
Code Example
a = 10; // Simple assignment
a += 1; // a = a + 1
a -= 1; // a = a - 1
a *= 2; // a = a * 2
a /= 2; // a = a / 2
a %= 2; // a = a % 2
_____________________________________________
### Increment/Decrement Operators
Used to increment or decrement the value of a variable by one.
Code Example
a++; // Increment
a--; // Decrement
_____________________________________________
## 3. Control Structures
Control structures dictate the flow of control in a program.
### Conditional Statements
#### if-else
Code Example
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
_____________________________________________
#### switch
Code Example
switch (expression) {
case constant1:
// code to be executed if expression == constant1
break;
case constant2:
// code to be executed if expression == constant2
break;
default:
// code to be executed if expression doesn't match any case
}
_____________________________________________
### Loops
#### for
Code Example
for (initialization; condition; increment) {
// code to be executed
}
_____________________________________________
#### while
Code Example
while (condition) {
// code to be executed
}
_____________________________________________

#### 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

#### Function Prototypes


Code Example
void function();
_____________________________________________
#### Function Parameters
Code Example
void function(int a, char b);
_____________________________________________
#### Function Return Types
Code Example
int function();
_____________________________________________

#### 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
_____________________________________________

#### Microsoft Visual C++


Popular compiler for Windows.
### Debuggers
#### gdb
GNU debugger.

##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.

You might also like