Main Function
The main function is the designated starting point of execution for every C program. When a C program runs, execution automatically begins at the first line of the main function.
Definition: The main function is a special function that initiates program execution and typically returns an integer status code to the operating system upon termination.
A standard main function is written as int main(void) or int main(int argc, char *argv[]). Returning 0 from the main function conventionally indicates successful program execution.
Function Prototyping
Function prototyping involves declaring a function's signature before its actual definition. It informs the compiler about the function's name, return type, and the number and data types of its parameters.
Definition: A function prototype is a declaration that provides the compiler with the interface of a function prior to its usage in the code.
Example of a function prototype:
int add(int a, int b);
Exam-Oriented Notes: Function prototyping allows the compiler to perform type checking on function arguments, preventing type mismatch errors and enabling the compiler to correctly generate code when a function is called before it is defined.
Handling Pointers
A pointer is a variable that stores the memory address of another variable rather than its actual value. Handling pointers involves understanding how to declare pointers, assign addresses, and access or modify data using dereferencing.
- Declaration: int *ptr; declares a pointer to an integer.
- Address-of Operator (&): Retrieves the memory address of a variable. Example: ptr = &x;
- Dereference Operator (*): Accesses or modifies the value stored at the memory address held by the pointer. Example: *ptr = 10;
Common Mistakes with Pointers
Beginners often encounter issues such as uninitialized pointers (wild pointers), dangling pointers (pointing to freed or out-of-scope memory), and memory leaks when dealing with dynamic memory allocation.
C Structures
A structure (struct) in C is a user-defined composite data type that groups related variables of different data types under a single name.
Definition: A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
Example of structure definition and usage:
struct Student {
char name[50];
int age;
float marks;
};
To access structure members, the dot operator (.) is used for standard structure variables, and the arrow operator (->) is used for pointers pointing to structures.
Structure Limitations
While C structures are powerful for data organization, they possess specific language-defined limitations:
- No Direct Comparison: C structures cannot be compared using relational operators like == or != directly. Each member must be compared individually.
- Assignment Limitations: Assigning one structure variable to another of the same type is allowed (e.g., struct1 = struct2), copying all member values, but array members are copied by value reference issues if not managed carefully.
- No Member Functions: Unlike object-oriented languages, C structures cannot contain member functions or methods.
| Feature | C Structures | Arrays |
|---|---|---|
| Data Types | Can hold mixed data types | Can hold only same data types |
| Size | Sum of individual members (plus padding) | Size of element multiplied by count |
| Access Operator | Dot (.) or Arrow (->) | Index operator ([ ]) |