C provides several key features that make it powerful and popular.
- It is a structured language that supports functions and modularity, making code reusable and organized.
- C is highly portable, meaning programs written in C can run on different platforms with minimal modification.
- C is efficient because it provides low-level access to hardware while maintaining high-level constructs.
- It supports dynamic memory allocation using malloc() and free().
- Additionally, C is widely used in system programming, including OS development.
-
A structure in C is a user-defined data type that groups related variables of different data types under one name.
- Each variable within the structure is called a "member" or "field". Structures allow you to represent complex data entities like a record (e.g., a 'student' with fields such as name, age, and marks).
- The syntax for a structure declaration is `struct
structure_name {
data_type member1;
data_type member2;
};`
and you can access members using the dot operator (e.g.,`student.name`).
-
Recursion in C refers to the process where a function calls itself in order to solve a problem.
- Recursive functions break down complex problems into simpler subproblems.
- A well-designed recursive function must have a base case that stops the recursion, otherwise it will result in infinite recursion and eventually a stack overflow.
- For example:The classic recursive function for calculating the factorial of a number `n` is:
int factorial(int n)
{
if (n == 0)
return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
-
The 'typedef' keyword in C is used to create an alias for an existing data type.
- This can make code more readable, especially when dealing with complex data types.
For example:
if you want to create a pointer to an integer and use it throughout your program, you can define a typedef:
typedef int* IntPtr;
IntPtr ptr1, ptr2; // ptr1 and ptr2 are both pointers to integers
This simplifies declarations and enhances code clarity.
-
The 'return' statement is used to exit a function and optionally return a value to the caller.
- It allows the function to complete its execution and send a result back.
- If the function is declared with a return type, the 'return' statement must return a value of that type. If the function is of type `void`, no value needs to be returned.
For example:
int add(int a, int b)
{
return a + b; // Returns the sum of a and b
}
-
In C, a function can pass parameters in two ways: pass-by-value and pass-by-reference.
- In pass-by-value, the function gets a copy of the variable's value, and changes to the parameter do not affect the original variable.
- In pass-by-reference, the function receives a pointer to the variable, allowing it to modify the original variable.
- For example, passing an array to a function is usually done by reference, as arrays are passed as pointers:
void modifyArray(int arr[])
{
arr[0] = 100; // Modifies the original array
}
- An array in C is a collection of variables of the same type, stored in contiguous memory locations. Arrays are indexed from zero, and their size is fixed at compile time.
- Arrays provide an easy way to store multiple values of the same type, and they are widely used for tasks like storing lists, matrices, or sequences of values.
- Arrays can be passed to functions by reference, which is efficient for large data sets:
Example
int arr[5] = {1, 2, 3, 4, 5}; // Declare an array of 5 integers
-
The 'const' keyword in C is used to define variables whose values cannot be modified after initialization.
- It is used to improve code safety by ensuring that certain values remain constant throughout the program.
- This can help avoid accidental changes and make the code easier to maintain.
- 'const' can also be used with pointers to create constant pointers or pointers to constant data:
const int x = 10; // x cannot be modified
const int *ptr = &x; // ptr points to a constant value
-
Function pointers are pointers that point to a function instead of a variable. They allow you to pass functions as arguments to other functions, return functions from functions, and implement callback mechanisms.
- Function pointers are commonly used in systems programming, event-driven architectures, or when writing generic functions that can operate on various callback functions:
int add(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int (*operation)(int, int); // Declare a function pointer
operation = add; // Assign the function pointer
printf("%d", operation(3, 4)); // Call the function through the pointer
-
A macro in C is a preprocessor directive that defines a code fragment, which is then substituted by the preprocessor during compilation.
- Macros are defined using the `#define` directive.
- They are commonly used for constant values, inline functions, or to simplify complex expressions:
#define PI 3.14
#define MAX(a, b) ((a) > (b) ? (a) : (b))
While macros are useful, they should be used carefully since they can lead to unexpected behaviors due to lack of type checking.
C has several types of operators, including:
- Arithmetic operators : Used for basic math operations(`+`, `-`, `*`, `/`, `%`).
- Relational operators: Used to compare values (`==`, `!=`, `>`, `<`, `>=`, `<=`).
- Logical operators: Used for logical operations (`&&`, `||`, `!`).
- Bitwise operators : Used for bit manipulation (`&`, `|`, `^`, `~`, `<<`, `>>`).
- Assignment operators : Used to assign values (`=`, `+=`, `-=`, `*=`, `/=`).
- Increment/decrement operators : Used to increment or decrement a variable (`++`, `--`).
- Others : Such as `sizeof`, conditional (`? :`), and comma operators.