Quick Lessons

Learning C Programming: Variables, Control Structures, and Loops

C code showing variables, conditions and loops

TL;DR

A beginner's guide to the core building blocks of C: why C is worth learning, variables and data types (with integer sizes on 64-bit systems), arithmetic, relational and logical operators, if/else conditionals, and while and for loops, put together in practical examples.

On this page

Diving into the world of programming can be both exciting and challenging, especially when starting with a language as fundamental as C. If you’re embarking on this journey, you’re likely tackling concepts that are foundational to not just C, but programming in general. In this blog post, we’ll explore some of the core topics you’ll encounter in a project focused on variables, conditional statements, and loops in C.

Why Learn C?

C is often considered the “mother of all languages” in the programming world. Many modern languages, such as C++, Java, and Python, have their roots in C. Learning C provides a strong foundation in programming principles, memory management, and system-level programming, which is invaluable for any aspiring programmer.

Key Concepts to Master

Variables and Data Types

In C, variables are used to store data that your program can manipulate. Each variable has a type, which determines what kind of data it can hold. Common data types include:

  • int: Integer values
  • char: Single characters
  • unsigned int: Non-negative integer values
int age = 25;
char initial = 'A';
unsigned int score = 100;
    

Data Types | Integer types (on most 64-bit computers)

Type Storage size Value range
char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
short 2 bytes -32,768 to 32,767
unsigned short 2 bytes 0 to 65,535
int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long 8 bytes −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long 8 bytes 0 to 18,446,744,073,709,551,615

Arithmetic Operators

Arithmetic operators allow you to perform mathematical operations on variables. The basic arithmetic operators in C include:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modulus)
int sum = 5 + 3;
int difference = 10 - 4;
int product = 6 * 7;
int quotient = 20 / 5;
int remainder = 10 % 3;
    

Conditional Statements

Conditional statements allow your program to make decisions based on certain conditions. The most common conditional statements are if and if...else.

int number = 10;

if (number > 0) {
    printf("The number is positive.\n");
} else {
    printf("The number is not positive.\n");
}
    

Relational and Logical Operators

Relational operators compare two values and return a boolean result (true or false). Logical operators combine multiple boolean expressions.

  • Relational operators: ==, !=, <, >, <=, >=
  • Logical operators: && (logical AND), || (logical OR), ! (logical NOT)
int a = 5;
int b = 10;

if (a < b && b > 0) {
    printf("Both conditions are true.\n");
}
    

Loops

Loops allow you to execute a block of code repeatedly. The while loop is one of the simplest looping structures in C.

int count = 0;

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

Practical Application: Putting It All Together

Let’s write a small program that uses variables, arithmetic operations, conditional statements, and loops to calculate the factorial of a given number.

#include <stdio.h>

int main() {
    int number;
    unsigned int factorial = 1;

    printf("Enter a positive integer: ");
    scanf("%d", &number);

    if (number < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        int i = 1;
        while (i <= number) {
            factorial *= i;
            i++;
        }
        printf("Factorial of %d is %u\n", number, factorial);
    }

    return 0;
}
    

In this program:

  • We declare an integer variable number to store the user input.
  • We use a while loop to calculate the factorial of the entered number.
  • Conditional statements handle the case where the user enters a negative number.

Conclusion

Learning C programming by understanding variables, control structures, and loops forms a solid foundation for any programming endeavor. These basic concepts are not only crucial for mastering C but also for excelling in other programming languages. As you practice writing and debugging your own C programs, you’ll gain confidence and proficiency in problem-solving and logical thinking. Happy coding!

Key takeaways 5

  1. C underpins many modern languages and teaches how computers work.
  2. Variables have types with fixed sizes, such as int, char and long.
  3. Arithmetic, relational and logical operators build expressions.
  4. if, else if and else control decisions.
  5. while and for loops repeat code until a condition changes.

Frequently asked questions

Why learn C programming?

C teaches fundamental concepts such as memory, data types and how programs run close to the hardware, and it influenced languages like C++, Java and Python.

What is the difference between a while loop and a for loop in C?

A for loop puts initialization, condition and update in one line and suits a known number of iterations. A while loop repeats as long as a condition is true and suits unknown iteration counts.

How big is an int in C?

On most modern 64-bit systems an int is 4 bytes (32 bits), but the C standard only guarantees a minimum range, so sizes can vary by platform.

Quick Lessons#c programming#variables#loops#control structures#programming basics

Comments

No comments yet. Start the conversation.

Comments are reviewed before they appear. Be kind; one link max.

Learn it hands-on at AL Academy

Free practical programs in software, automation and IT, with labs and certificates.

Open AL Academy ↗
Keep reading

Related articles