Understand C Variables

In this C programming class, we’ll explain the concepts of C variables using flowcharts and code examples. It is one of the core building blocks in C. Hence, please read and understand with full concentration.

Table of Content

  1. What are Variables in C Programming?
  2. C Variable Naming Rules
  3. Types of Variables in C
    1. Local
    2. Global
  4. Variable Data Types in C
  5. C Program Workflow
    1. What is a Flowchart?
    2. Elements in a Flowchart
  6. Variables Demo in C
    1. Example – C Program to Multiple Two Numbers
      1. Flowchart & Pseudo Code
      2. C Coding Snippet
      3. Program Summary
    2. Example – C Program to Swap Two Integers
      1. Flowchart & Pseudo Code
      2. C Coding Snippet
  7. C Programming Exercise

C Variables – Let’s Understand from Scratch

1. What are Variables in C Programming?

In mathematics, a variable means that its value can change and constant means that its value cannot change.

In C, variables are unique names associated with values. They work as a container and points to a specific location in the program memory.

We can access a C variable directly by its name and also by using the memory address assigned to it.

2. C Variable Naming Rules

C variables are case-sensitive names. Here are a few simple naming conventions for them.

  • A C variable name can include one or more of the following:
    • Letters (In both capital and small case)
    • Digits from 0 – 9
    • Underscore (No other special characters)
  • It must either have an underscore or an alphabet as the first character.
  • No restriction on the name length but 31 is the max suitable for most of the C compilers.

Check below a few examples of valid and invalid C variable names.

int _count = 5; // valid integer variable
int 5th = 5; // error => variable name can't begin with a digit.
char choice = '0'; // valid variable name
int choice = 0; // error => can't have another variable with the same name.

3. C Variables Types

In C programming, variables are of two types:

  1. Local,
  2. Global

3.1. Local Variables in C

Local C variables have a limited scope within its code block delineated with the curly braces. Their placement should happen at the beginning.

The C compiler doesn’t assign a default value to the local variables. Hence, we must provide a value before using them.

The lifetime of a C local variable starts as the execution enters into the code block which is its point of origin and ends after exiting from the same.

However, before learning more about variables, we must cover some basics of data types.

3.2. Global Variables in C

A variable which has a placement in the header section of a C file classifies as global.

The compiler initializes all Global C variables by default as per the following rules.

  • An integer variable with 0 as the default value
  • The char type with ‘\0.’
  • A float type again with 0 as the default
  • A pointer with NULL

It remains active and available throughout the execution until it ends.

4. C Variables Data Types

Data types enable programmers to define variables which can hold the value required by the program or the business logic.

For this class, let’s assume that we’ll only need the following three standard C data types.

4.1. int

The int represents the integer data type. It can contain positive, negative numbers and zero. Any other data included in this will give an error.

4.2. float

The float keyword is to define variables which can hold decimal numbers.

4.3. double

The double is another data type like the float. The variables of this type can also hold decimal numbers but provide better accuracy and precision.

That is all we need to learn at this moment. We will see data types from the depth in the upcoming classes.

Now, we will create a pattern here for all programs hereafter. We will see one example in three different ways.

5. Workflow of A C Program

We can understand the workflow of any C program using the flowcharts. Let’s know what are they and how can they help.

5.1. What is the purpose of a Flowchart?

It is just a visual representation of the functioning of a program created using a pre-defined set of shapes or symbols.

5.2. Elements in a Flowchart

The following table should help you in understanding them.

Flowchart Shapes and Meaning

Flowchart Shapes and Meaning

These are just some basic shapes but enough to get us started.

Let’s learn a few essential terms.

  1. Algorithm: Algorithm is a brief systematic explanation of a program. It just explains the working of a program roughly, not the actual code.
  2. Pseudo Code: It is the code written in a human-readable language such as English.

6. Variables Demo in C

Let us start by a simple program to multiply two numbers.

6.1. Example – C Program to Multiply Two Numbers

6.1.1. Flowchart & Pseudo Code

C Variables Demo - Multiply Two Numbers

C Variables Demo – Multiply Two Numbers

Algorithm:

Step 1: Start.

Step 2: Take input from the user for multiplying two numbers.

Step 3: Multiply two numbers (m=a*b)

Step 4: Display output (m)

Step 5: Stop

6.1.2. C Coding Snippet

Note: Now there are some keywords, which we will see after this code.

#include<stdio.h>
#include<conio.h>

void main()
{
    int a, b, m;

    printf("Enter two numbers: ");
    scanf("%d,%d",&a,&b);

    m= a*b;

    printf("The multiplication of %d and %d is %d",a,b,m);
    getch();
}

6.1.3. Step by Step Program Summary

  1. #include<conio.h>: This header file tells the computer to include console input/output functions. Hence, conio stands for console input/output.
  2. Void main(): It is the primary function in a C program as discussed in the earlier classes.
    Note: You have seen us using the int type with the main. The principal difference here is that void means nothing to return whereas the integer type requires the function to return a numeric value.
  3. scanf(): Used to take input from a user. The %d sign is for indicating that the data type is an integer. We will learn more about this in further chapters. The ampersand (&) sign tells the computer that it should store the value of the taken input in that specific address. E.g., &a denotes that the value of the first input will get stored in the location of a.
  4. getchar(): It tells the program to wait for the reaction of the user. It stands for “Get Character.” The computer holds the output screen for the user to comprehend the output.

The output should come something like this.

Program to Multiply Two Numbers - Output

Now we will see an exciting example of how variables work.

6.2. Example – C Program to Swap Two Integers

6.2.1. Flowchart & Pseudo Code

C Variables Demo - Swap Two Integers

C Variables Demo – Swap Two Integers

Algorithm:

Step 1: Start

Step 2: Initialize two input variables (a, b) and one for swapping(c).

Step 3: Accept input variables from the user (a & b)

Step 4: Swap the two variables

# Swap variables
c=a
a=b
b=c

Step 5: Display the values before and after swapping.

Step 6: Stop.

6.2.2. C Coding Snippet

#include<stdio.h>
#include<conio.h>

void main()
{
    int a,b,c;

    printf("Enter two numbers to swap : ");
    scanf("%d %d",&a,&b);
    printf("Values before swapping: %d %d",a,b);

    c=a;
    a=b;
    b=c;

    printf("\nValues after swapping: %d %d",a,b);
    getchar();
}

The output should come something like this-

Program to Swap Two Integers - Output

You may have realized that we used a third variable for swapping the values of the two inputs. Can you write a program that can do this without using the third variable? If you can do this, then you will certainly get the idea of how variables literary work. If not, do not worry, another example is available next.

We would recommend that you try very hard on this. It is easy.

7. Programming Exercise on C Variables

Write a program to swap values of two variables without using a third variable.

Solution:

#include<stdio.h>
#include<conio.h>

void main()
{
    int a,b;

    printf("Enter two numbers to swap : ");
    scanf("%d %d",&a,&b);
    printf("Values before swapping: %d %d",a,b);

    a = a+b;
    b = a-b;
    a = a-b;

    printf("\nValues after swapping: %d %d",a,b);
    getchar();
}

The output will be the same as the earlier program.

NOTE: We have just covered the basics of variables here. As variables is a vast topic, we will see it in bits and pieces included in other chapters.