Different Types of Instructions in C
In C programming, an instruction is a statement that tells the compiler to perform a specific task. Instructions can be simple statements that perform a single action, or they can be compound statements that perform multiple actions.
There are several types of instructions in C, including:
- Declaration Instructions
- Assignment Instructions
- Input/Output Instructions
- Control instructions
- Function Calls
Declaration Instructions
These are used to declare variables, functions, and other identifiers. For example:
int x; // declaration of a variable x of type int
float y; // declaration of a variable y of type float
void func(int a, float b); // declaration of a function func with two arguments
Assignment Instructions
These are used to assign a value to a variable. For example:
x = 10; // assignment of the value 10 to the variable x
y = 20.5; // assignment of the value 20.5 to the variable y
Input/Output Instructions
These are used to read data from the standard input (usually the keyboard) or write data to the standard output (usually the screen). For example:
int x;
printf("Enter a value for x: "); // output instruction
scanf("%d", &x); // input instruction
Control instructions
These are used to control the flow of execution of a program. If Statements, While Loop and For Loop are the control statements available in C. For example:
if (x > 0)
{
// if statement
// code to execute if x is greater than 0
}
else
{
// code to execute if x is not greater than 0
}
while (x < 10)
{
// while loop
// code to execute as long as x is less than 10
x++;
}
for (int i = 0; i < 10; i++)
{
// for loop
// code to execute 10 times
}
Function Calls
These are used to invoke a function and can be used as part of an instruction. For example:
result = max(x, y); // function call to the max() function
This will call the max()
function with arguments x
and y
and return the maximum value, which is then assigned to the variable result
.