C Statements

Learn about different types of statements in C programming

📋 What are C Statements?

Statements are instructions that tell the computer what to do. Every statement in C ends with a semicolon (;).


#include <stdio.h>

int main() {
    int x = 10;           // Declaration statement
    printf("Value: %d", x); // Expression statement
    return 0;             // Return statement
}
                                    

Output:

Value: 10

Types of C Statements

📝

Declaration

Declare variables and functions

int age;
float price = 99.99;

Expression

Perform calculations and operations

x = 5 + 3;
printf("Hello");
🔄

Control

Control program flow

if (x > 0) {
    printf("Positive");
}
🏁

Jump

Transfer control to another part

return 0;
break;
continue;

🔹 Declaration Statements

Declaration statements in C create variables and specify their data types before use. The syntax int age; char grade; declares an integer variable named age and a character variable named grade. Declaring variables tells the compiler how much memory to allocate and what kind of data the variable will hold. You can declare multiple variables of the same type using commas, like int x, y, z;, making code more concise. C requires all variables to be declared before their first use, enforcing type safety and preventing accidental errors.

#include <stdio.h>

int main() {
    // Simple declarations
    int number;
    float temperature;
    char letter;
    
    // Declaration with initialization
    int age = 25;
    float height = 5.9;
    char grade = 'A';
    
    // Multiple declarations
    int x, y, z;
    int a = 1, b = 2, c = 3;
    
    printf("Age: %d, Grade: %c\n", age, grade);
    return 0;
}

Output:

Age: 25, Grade: A

🔹 Assignment Statements

Assignment statements give values to variables after they've been declared. Using the equals sign, you assign values like x = 10; y = 5;, storing data that your program can manipulate. Assignment differs from declaration because it gives an existing variable a new value. You can perform operations while assigning, such as sum = x + y;, combining arithmetic with storage. Understanding the difference between declaration and assignment prevents common beginner mistakes and helps you write more efficient code by reusing variables appropriately throughout your program.

#include <stdio.h>

int main() {
    int x, y, result;
    
    // Simple assignments
    x = 10;
    y = 5;
    
    // Arithmetic assignments
    result = x + y;        // Addition
    result = x - y;        // Subtraction
    result = x * y;        // Multiplication
    result = x / y;        // Division
    
    printf("x = %d, y = %d\n", x, y);
    printf("x + y = %d\n", x + y);
    printf("x * y = %d\n", x * y);
    
    return 0;
}

Output:

x = 10, y = 5
x + y = 15
x * y = 50

🔹 Control Statements

Control statements direct how your C program executes by making decisions and repeating code. The if statement checks conditions using syntax like if (x > 10) {} to run code only when conditions are true. The else statement provides alternative code when conditions are false. The for loop repeats code a specific number of times, while the while loop repeats until a condition becomes false. These control structures allow programs to make decisions, handle different scenarios, and automate repetitive tasks, making them essential for writing functional and responsive applications.

#include <stdio.h>

int main() {
    int number = 15;
    
    // If statement
    if (number > 10) {
        printf("%d is greater than 10\n", number);
    }
    
    // If-else statement
    if (number % 2 == 0) {
        printf("%d is even\n", number);
    } else {
        printf("%d is odd\n", number);
    }
    
    // For loop statement
    printf("Counting: ");
    for (int i = 1; i <= 3; i++) {
        printf("%d ", i);
    }
    printf("\n");
    
    return 0;
}

Output:

15 is greater than 10
15 is odd
Counting: 1 2 3

🔹 Statement Rules

Following C statement rules ensures your code compiles correctly and behaves as intended. Every statement must end with a semicolon, such as printf("Hello");, which signals statement completion. Statements must use proper syntax with correct operators and valid variable names. Multiple statements can be grouped using braces {} to create blocks that execute together. Comments using // or /* */ explain your code without affecting execution. Violating these rules causes compilation errors, making rule mastery essential for every C programmer.

// 1. Every statement ends with semicolon
int x = 5;              // Correct
// int y = 10            // Error: missing semicolon

// 2. Statements are executed sequentially
printf("First\n");      // Executes first
printf("Second\n");     // Executes second
printf("Third\n");      // Executes third

// 3. Compound statements use braces
{
    int a = 1;
    int b = 2;
    printf("Sum: %d\n", a + b);
}

// 4. Empty statement is valid
;                       // This is an empty statement

Output:

First
Second
Third
Sum: 3

🧠 Test Your Knowledge

Which statement is used to declare a variable in C?