C Notes
C Notes
1. **Definition**:
- Constant: A constant is a value that does not change during the execution of a
program. It is a fixed value assigned to a variable or used directly in an
expression.
- Variable: A variable is a storage location in computer memory that can hold
varying values during program execution. It is declared with a data type and can be
assigned new values.
2. **Declaration**:
- Constants are declared using the `const` keyword followed by a data type and a
name, like `const int MAX_VALUE = 100;`.
- Variables are declared with a data type and a name, like `int count;`.
3. **Initialization**:
- Constants must be initialized at the time of declaration and cannot be changed
later in the program.
- Variables can be initialized at the time of declaration or later in the
program.
4. **Usage**:
- Constants are used when a value is known and should not change throughout the
program, like mathematical constants or configuration values.
- Variables are used to store and manipulate data that can change during the
execution of the program, like user input, intermediate calculations, or results.
5. **Memory Allocation**:
- Constants may or may not be allocated memory depending on their usage. If they
are used directly in expressions, they might not be allocated memory separately.
- Variables are allocated memory when they are declared, and the amount of
memory depends on their data type.
6. **Scope**:
- Constants have a global scope if declared outside any function or a local
scope if declared within a function.
- Variables can have either global or local scope depending on where they are
declared.
7. **Modification**:
- Constants cannot be modified once they are initialized.
- Variables can be modified by assigning new values to them during program
execution.
8. **Examples**:
- Constant: `const float PI = 3.14159;`
- Variable: `int age;`
9. **Purpose**:
- Constants provide a way to make code more readable and maintainable by giving
meaningful names to fixed values.
- Variables allow programs to store and manipulate data dynamically, enabling
them to perform tasks efficiently.
# uses of functions in C:
5. **Return Values**: Functions can return values to the caller, providing a way to
communicate results or data back to the calling code.
7. **Testing and Debugging**: Functions make it easier to isolate and test specific
parts of the code, as they can be tested independently before integration into the
larger program.
1. **Initialization**:
- While Loop: In a while loop, initialization of the loop variable (if needed)
is done before entering the loop.
- Do-While Loop: In a do-while loop, initialization of the loop variable (if
needed) is done after entering the loop.
2. **Condition Checking**:
- While Loop: Condition is checked before entering the loop body. If the
condition is false initially, the loop body will not execute.
- Do-While Loop: Condition is checked after executing the loop body at least
once. The loop body is executed once even if the condition is false initially.
3. **Usage**:
- While Loop: Generally used when the number of iterations is not known
beforehand and the loop may not execute at all if the condition is false initially.
- Do-While Loop: Useful when you want to execute the loop body at least once,
regardless of the condition.
4. **Syntax**:
- While Loop:
```
while (condition) {
// statements
}
```
- Do-While Loop:
```
do {
// statements
} while (condition);
```
5. **Exit Condition**:
- While Loop: The loop may not execute at all if the condition is false
initially.
- Do-While Loop: The loop body executes at least once before checking the
condition for the exit.
7. **Flow Control**:
- While Loop: The flow may not enter the loop body if the condition is false
initially.
- Do-While Loop: The flow always enters the loop body at least once before
checking the condition.
8. **Applicability**:
- While Loop: Suitable for scenarios where the loop may or may not execute based
on the condition.
- Do-While Loop: Suitable for situations where you want the loop body to execute
at least once.
9. **Example**:
- While Loop:
```c
while (x > 0) {
printf("x is greater than 0\n");
x--;
}
```
- Do-While Loop:
```c
do {
printf("x is greater than 0\n");
x--;
} while (x > 0);
```
10. **Termination**:
- While Loop: May terminate without executing the loop body if the condition is
false initially.
- Do-While Loop: Executes the loop body at least once before checking the
condition for termination.
These points illustrate the differences between the while loop and the do-while
loop in C.