No history yet

C Instruction Sets

Instructions to the Machine

A C program is a sequence of instructions you give to a computer. When you compile your code, the compiler's job is to translate these human-readable instructions into machine code that the processor can execute directly. These instructions fall into three broad categories: Type Declaration, Arithmetic, and Control. Think of them as telling the computer what kind of information to expect, what to do with it, and in what order to do it.

Declaring Your Variables

Before you can use a variable, you must tell the compiler what it is. This is done with a type declaration instruction. It's a simple but non-negotiable rule in C: all variables must be declared before they are used.

A declaration serves two purposes. First, it tells the compiler the variable's name. Second, it specifies the variable's data type, which informs the compiler how much memory to set aside.

int customer_id;
float account_balance;
char initial;

When the compiler sees int customer_id;, it reserves a block of memory (typically 4 bytes) to store an integer and associates it with the name customer_id. Likewise, it allocates space for account_balance and initial based on their types.

You can also assign a value at the moment of declaration. This is called initialisation.

int quantity = 100;

An interesting situation arises when the type of the value doesn't match the type of the variable. C has strict rules for these cases.

int items_in_stock;
items_in_stock = 12.75; // A float value is assigned to an int variable

In this scenario, C doesn't round the number. It performs a truncation, simply chopping off the decimal part. So, items_in_stock will hold the value 12, not 13. This happens because of how different number types are represented in memory. An integer variable's memory space has no way to store fractional information, so it's discarded during the assignment. This is an example of an implicit type conversion - something C does automatically.

Performing Calculations

Arithmetic instructions are the workhorses of most programs. They tell the computer to perform calculations. An arithmetic statement in C involves an assignment operator (=) and an expression made up of variables, constants, and arithmetic operators.

// A simple arithmetic statement
float simple_interest;
float principal = 5000.0;
float rate = 0.05;
float time = 2.5;

simple_interest = principal * rate * time;

The right side of the = is evaluated first, and the result is then stored in the variable on the left. But what happens when an expression has multiple operators? C follows a strict order of operations, also known as operator precedence.

PriorityOperatorsAssociativity
1st* / %Left to Right
2nd+ -Left to Right
3rd=Right to Left

Multiplication, division, and the modulo operator have the highest priority. Addition and subtraction are next. The assignment operator is last. If operators have the same priority, they are evaluated based on their associativity. For most arithmetic, this is left-to-right.

For example, in y = 3 * 5 + 2, the multiplication (3 * 5) happens first, followed by the addition (15 + 2), and finally the assignment to y. You can always use parentheses ( ) to override the default order, as expressions inside parentheses are always evaluated first.

Controlling the Flow

So far, our instructions would execute in a simple, linear sequence. But to write useful programs, we need to control the flow of execution. We need the ability to make decisions and repeat actions.

Control instructions allow us to do just that. They determine the order in which other instructions are executed. There are three basic types of control structures:

Sequence Control: The default mode. Instructions are executed one after another in the order they are written.

Decision Control: Allows the program to follow different paths based on a condition (e.g., if-else, switch).

Loop Control: Causes a set of instructions to be repeated until a condition is met (e.g., for, while, do-while).

These structures are the fundamental building blocks of program logic. By combining them, you can create complex algorithms that respond to different inputs and situations. For example, a decision control structure can check if a user entered a valid password, and a loop control structure can process every item in a shopping cart. We will dive deeper into the syntax and application of these control structures in the next section.

Quiz Questions 1/5

What is the primary role of a compiler in the C programming process?

Quiz Questions 2/5

What value will the variable final_count hold after this C code is executed?

int final_count;
double precise_value = 42.99;
final_count = precise_value;

Understanding these three types of instructions is the first step towards mastering C. They provide the basic grammar for communicating any task to the computer.