Educational Outline: C Programming - Part 2
1. Types of Instructions
- In C programming, instructions are the commands used to perform operations.
- There are various types of instructions in C, including:
- Expression Instructions: Perform mathematical and logical operations.
- Control Flow Instructions: Direct the flow of program execution (e.g., if, else, switch).
- Input/Output Instructions: Interact with the user or system (e.g., scanf, printf).
- Declaration Instructions: Define variables and functions.
Example:
int a = 10; // Declaration
printf("Value: %d", a); // Input/Output instruction
2. Integer and Float Conversions
- Type conversion is the process of converting one data type to another.
- In C, there are two types of conversions:
- Implicit Conversion (Automatic): The compiler automatically converts a smaller data type to a
larger one (e.g., int to float).
- Explicit Conversion (Type Casting): The programmer manually converts a data type using
casting (e.g., float to int).
Example of Implicit Conversion:
int a = 5;
float b = a; // Automatically converts int to float.
Example of Explicit Conversion:
float pi = 3.14;
int integer_pi = (int)pi; // Casts float to int.
3. Hierarchy of Operations
- The hierarchy of operations determines the order in which operations are performed in
expressions.
- In C, operators are evaluated in a specific order (precedence).
- Common operator precedence (highest to lowest):
1. Parentheses ()
2. Unary operators (!, ++, --)
3. Multiplication, Division, Modulus (*, /, %)
4. Addition, Subtraction (+, -)
5. Relational Operators (>, <, >=, <=)
6. Equality Operators (==, !=)
7. Logical Operators (&&, ||)
8. Assignment Operators (=, +=, -=)
Example:
int result = 2 + 3 * 4; // Multiplication is performed first, then addition.
4. Associativity of Operators
- Associativity defines the direction in which operators of the same precedence are evaluated.
- Two types of associativity:
- Left-to-Right: Most operators (e.g., +, -, *, /) have left-to-right associativity.
- Right-to-Left: Assignment operators (=, +=) have right-to-left associativity.
Example:
int a = 5, b = 10, c;
c = a + b - 2; // Left-to-right evaluation
5. Control Instructions
- Control instructions alter the flow of execution in a program.
- Types of control instructions in C:
- Conditional Statements: if, else if, switch, which control execution based on conditions.
- Looping Statements: for, while, do-while, which repeat code based on conditions.
- Jump Statements: break, continue, return, which alter the flow inside loops or functions.
Example of Conditional Statement:
if (a > b) {
printf("a is greater");
} else {
printf("b is greater");
Example of Looping Statement:
for (int i = 0; i < 5; i++) {
printf("Iteration %d
", i);