In C programming, understanding memory management is crucial for writing efficient programs. Memory is used for storing data, program instructions, and other important information during the execution of a program.
Memory in C is divided into different sections, each serving a specific purpose. Here’s a general layout of how memory is organized in a C program:
Stack:
Heap:
free() (for example, when using malloc() or calloc() to allocate memory).Data Segment:
Text Segment:
In C, the programmer has more control over memory compared to higher-level languages. This includes manual memory allocation and deallocation:
Proper memory management is important to prevent memory leaks (not freeing memory) or segmentation faults (accessing unallocated memory).
In C, arithmetic operations are performed using various operators. These include basic operations like addition, subtraction, multiplication, division, and modulus.
Here’s a list of the common arithmetic operators in C:
| Operator | Description | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus (remainder after division) | a % b |
Addition: Adding two numbers:
int result = 5 + 3; // result will be 8
Subtraction: Subtracting one number from another:
int result = 10 - 4; // result will be 6
Multiplication: Multiplying two numbers:
int result = 7 * 6; // result will be 42
Division: Dividing one number by another:
int result = 8 / 4; // result will be 2
Modulus: Finding the remainder of a division:
int result = 10 % 3; // result will be 1 (remainder of 10 divided by 3)
C performs integer division when both operands are integers. The result is an integer, and any decimal or fractional part is discarded.
Example:
int result = 7 / 3; // result will be 2 (fractional part discarded)
To perform floating-point division, at least one operand should be a float or double. For example:
float result = 7.0 / 3; // result will be 2.3333
C provides a wide range of operators that can be categorized into different types:
As discussed earlier, these operators perform basic mathematical operations on numeric values.
Example:
int a = 10, b = 20;
int sum = a + b; // sum = 30
int difference = a - b; // difference = -10
Relational operators are used to compare two values. They return either true (1) or false (0).
| Operator | Description | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
Example:
int a = 10, b = 5;
if (a > b) {
printf("a is greater than b\n"); // This will be printed
}
Logical operators are used to combine conditional statements.
| Operator | Description | Example |
|---|---|---|
&& |
Logical AND | a && b |
| ` | ` | |
! |
Logical NOT | !a |
Example:
int a = 5, b = 10;
if (a < b && b > 0) {
printf("Both conditions are true\n"); // This will be printed
}
Assignment operators are used to assign values to variables.
| Operator | Description | Example |
|---|---|---|
= |
Simple assignment | a = b |
+= |
Addition assignment | a += b |
-= |
Subtraction assignment | a -= b |
*= |
Multiplication assignment | a *= b |
/= |
Division assignment | a /= b |
%= |
Modulus assignment | a %= b |
Example:
int a = 10;
a += 5; // a = a + 5, so a becomes 15
The increment (++) and decrement (--) operators are used to increase or decrease a variable by 1.
| Operator | Description | Example |
|---|---|---|
++ |
Increment by 1 | a++ or ++a |
-- |
Decrement by 1 | a-- or --a |
Example:
int a = 5;
a++; // a becomes 6 (increment by 1)
Bitwise operators operate on binary representations of integers. These operators allow for operations at the bit level.
| Operator | Description | Example |
|---|---|---|
& |
Bitwise AND | a & b |
| ` | ` | Bitwise OR |
^ |
Bitwise XOR | a ^ b |
~ |
Bitwise NOT | ~a |
<< |
Left shift | a << 2 |
>> |
Right shift | a >> 2 |
Example:
int a = 5, b = 3;
int result = a & b; // result will be 1 (binary AND of 5 and 3)
The conditional operator (?:) is a shorthand for an if-else statement.
| Syntax | Description |
|---|---|
condition ? expr1 : expr2 |
If condition is true, expr1 is evaluated, otherwise expr2 is evaluated. |
Example:
int a = 10;
int result = (a > 5) ? 1 : 0; // result will be 1 (since a > 5 is true)
malloc(), calloc(), and free() for dynamic memory management.Understanding these memory concepts, arithmetic operations, and operators is essential for writing effective and efficient C programs.
Open this section to load past papers