Variables, if, else, while in C Programming

3 min read
Cover Image for Variables, if, else, while in C Programming

C programming comes alive through code, and in this article, we'll embark on a hands-on journey to master the essentials of variables, conditionals, and loops. With concrete examples and clear explanations, we'll build a solid foundation for your C programming adventures.

Arithmetic Operators in Action

Arithmetic operators enable mathematical computations:

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    int sum = a + b;         // Addition
    int difference = a - b;  // Subtraction
    int product = a * b;     // Multiplication
    int quotient = a / b;    // Division
    int remainder = a % b;   // Modulus

    printf("Sum: %d\n", sum);
    printf("Difference: %d\n", difference);
    printf("Product: %d\n", product);
    printf("Quotient: %d\n", quotient);
    printf("Remainder: %d\n", remainder);

    return 0;
}

Logical and Relational Operators Explored

Logical and relational operators facilitate decision-making:

#include <stdio.h>

int main() {
    int age = 25;
    int isStudent = 1;

    if (age >= 18 && isStudent) {  // AND operator
        printf("You are an adult student.\n");
    } else {
        printf("You are not an adult student.\n");
    }

    return 0;
}

Boolean Operations: Embracing True and False

Understanding boolean values drives conditional logic:

#include <stdio.h>

int main() {
    int hasPermission = 0;

    if (!hasPermission) {  // NOT operator
        printf("Access denied.\n");
    }

    return 0;
}

Conditional Statements: If and Else Illustrated

Conditional statements guide program flow:

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("Positive number.\n");
    } else if (num < 0) {
        printf("Negative number.\n");
    } else {
        printf("Zero.\n");
    }

    return 0;
}

Variables and Data Types: A Solid Foundation

Declaring and using variables is fundamental:

#include <stdio.h>

int main() {
    char grade = 'A';
    int age = 30;
    unsigned int distance = 150;

    printf("Grade: %c\n", grade);
    printf("Age: %d\n", age);
    printf("Distance: %u\n", distance);

    return 0;
}

Leveraging While Loops for Iteration

While loops facilitate repetitive tasks:

#include <stdio.h>

int main() {
    int count = 0;

    while (count < 5) {
        printf("Count: %d\n", count);
        count++;
    }

    return 0;
}

Printing Variables with Precision

Formatting specifiers showcase variable values:

#include <stdio.h>

int main() {
    float pi = 3.14159;
    double e = 2.71828;

    printf("Pi: %.2f\n", pi);   // Display with 2 decimal places
    printf("Euler's number: %.4f\n", e);  // Display with 4 decimal places

    return 0;
}

Unveiling ASCII: Characters and Numbers

ASCII character set ties characters to numeric values:

#include <stdio.h>

int main() {
    char letter = 'A';
    int asciiValue = (int)letter;

    printf("Character: %c\n", letter);
    printf("ASCII Value: %d\n", asciiValue);

    return 0;
}

GCC Flags: Architectural Considerations

Using -m32 and -m64 flags for architecture:

# Compile for 32-bit architecture
gcc -m32 my_program.c -o my_program_32

# Compile for 64-bit architecture
gcc -m64 my_program.c -o my_program_64

Conclusion

By exploring C programming with practical examples, you're not just learning concepts—you're applying them to real scenarios. This hands-on approach sets the stage for more advanced C programming endeavors. Whether you're building algorithms, crafting applications, or delving into system-level programming, your understanding of variables, conditionals, and loops is now grounded in code.

Embrace the power of C, and let your code bring ideas to life!