### Functions - **Definition:** Block of code that performs a specific task. - **Syntax:** ```c return_type function_name(parameter_list) { // function body return value; // optional } ``` - **Example:** ```c int add(int a, int b) { return a + b; } ``` - **Call by Value:** Arguments are copied to parameters. Changes inside the function do not affect original variables. - **Call by Reference:** Addresses of arguments are passed. Changes inside the function affect original variables (using pointers). ### Loop and Decision Statements #### Loops - **`for` loop:** Executes a block of code a specific number of times. ```c for (initialization; condition; increment/decrement) { // code to be executed } ``` - **`while` loop:** Executes a block of code as long as a condition is true. ```c while (condition) { // code to be executed } ``` - **`do-while` loop:** Executes a block of code at least once, then continues as long as a condition is true. ```c do { // code to be executed } while (condition); ``` #### Decision Statements - **`if-else` statement:** Executes different code blocks based on a condition. ```c if (condition) { // code if condition is true } else { // code if condition is false } ``` - **`else if` ladder:** Multiple conditions checked sequentially. ```c if (condition1) { // code if condition1 is true } else if (condition2) { // code if condition2 is true } else { // code if no condition is true } ``` - **`switch` statement:** Multi-way branch based on the value of an expression. ```c switch (expression) { case value1: // code for value1 break; case value2: // code for value2 break; default: // code if no match } ``` ### Arrays - **Definition:** Collection of elements of the same data type stored in contiguous memory locations. - **Declaration:** `data_type array_name[size];` - **Initialization:** ```c int numbers[5] = {10, 20, 30, 40, 50}; int scores[] = {100, 95, 88}; // size inferred ``` - **Accessing Elements:** `array_name[index]` (indices start from 0). - **Multi-dimensional Arrays:** `data_type array_name[row_size][col_size];` ```c int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; ``` ### Structure and Union #### Structures (`struct`) - **Definition:** User-defined data type that allows to combine data items of different kinds. - **Syntax:** ```c struct structure_name { data_type member1; data_type member2; // ... }; ``` - **Declaration & Initialization:** ```c struct Student { char name[50]; int roll_no; float marks; }; struct Student s1 = {"Alice", 101, 85.5}; ``` - **Accessing Members:** `s1.roll_no` #### Unions (`union`) - **Definition:** User-defined data type similar to structures, but all members share the same memory location. Only one member can hold a value at any given time. - **Syntax:** ```c union union_name { data_type member1; data_type member2; // ... }; ``` - **Example:** ```c union Data { int i; float f; char str[20]; }; union Data data; data.i = 10; // If you then assign data.f = 22.5, data.i will be corrupted/overwritten ``` - **Key Difference:** Structures allocate memory for ALL members, Unions allocate memory for the LARGEST member. ### `sizeof()` Operator - **Definition:** Unary operator that returns the size, in bytes, of a variable or a data type. - **Syntax:** - `sizeof(variable_name)` - `sizeof(data_type)` - **Example:** ```c int x; printf("Size of int: %zu bytes\n", sizeof(int)); // Outputs size of int (e.g., 4) printf("Size of x: %zu bytes\n", sizeof(x)); // Outputs size of x (e.g., 4) char name[] = "C Programming"; printf("Size of name: %zu bytes\n", sizeof(name)); // Outputs 14 (13 chars + null terminator) struct Student { char name[50]; int roll_no; float marks; }; printf("Size of Student struct: %zu bytes\n", sizeof(struct Student)); ``` - **Return Type:** `size_t` (an unsigned integer type).