Understand C Variables with Flowcharts and Examples – MPS

Understand C Variables with Flowcharts and Example

In this C programming class, we’ll clarify the ideas of C variables utilizing flowcharts and code examples. It is one of many core constructing blocks in C. Hence, please learn and perceive with full focus.

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 Flowcharts?
    2. Elements in a Flowcharts
  6. Variables Demo in C
    1. Example – C Program to Multiple Two Numbers
      1. Flowcharts & Pseudo Code
      2. C Coding Snippet
      3. Program Summary
    2. Example – C Program to Swap Two Integers
      1. Flowcharts & 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 arithmetic, a variable implies that its worth can change and fixed implies that its worth can not change.

In C, variables are distinctive names related with values. They work as a container and factors to a particular location in this system reminiscence.

We can entry a C variable instantly by its title and additionally through the use of the reminiscence deal with assigned to it.

2. C Variable Naming Rules

C variables are case-sensitive names. Here are a number of easy naming conventions for them.

  • A C variable title can embody one or extra of the next:
    • Letters (In each capital and small case)
    • Digits from 0 – 9
    • Underscore (No different particular characters)
  • It should both have an underscore or an alphabet as the primary character.
  • No restriction on the title size however 31 is the max appropriate for a lot of the C compilers.

Check under a number of examples of legitimate and invalid C variable names.

int _count = 5; // legitimate integer variable
int fifth = 5; // error => variable title cannot start with a digit.
char selection = '0'; // legitimate variable title
int selection = 0; // error => cannot have one other variable with the identical title.

3. C Variables Types

In C programming, variables are of two sorts:

  1. Local,
  2. Global

3.1. Local Variables in C

Local C variables have a restricted scope inside its code block delineated with the curly braces. Their placement ought to occur originally.

The C compiler doesn’t assign a default worth to the native variables. Hence, we should present a price earlier than utilizing them.

The lifetime of a C native variable begins as the execution enters into the code block which is its level of origin and ends after exiting from the identical.

However, earlier than studying extra about variables, we should cowl some fundamentals of information sorts.

3.2. Global Variables in C

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

The compiler initializes all Global C variables by default as per the next guidelines.

  • An integer variable with 0 as the default worth
  • The char sort with ‘.’
  • A float sort once more with 0 as the default
  • A pointer with NULL

It stays lively and accessible all through the execution till it ends.

4. C Variables Data Types

Data sorts allow programmers to outline variables which may maintain the worth required by this system or the enterprise logic.

For this class, let’s assume that we’ll solely want the next three commonplace C information sorts.

4.1. int

The int represents the integer information sort. It can comprise optimistic, unfavourable numbers and zero. Any different information included in this will give an error.

4.2. float

The float key phrase is to outline variables which may maintain decimal numbers.

4.3. double

The double is one other information sort just like the float. The variables of this sort may also maintain decimal numbers however present higher accuracy and precision.

That is all we have to study at this second. We will see information sorts from the depth in the upcoming lessons.

Now, we’ll create a sample right here for all applications hereafter. We will see one instance in three alternative ways.

5. Workflow of A C Program

We can perceive the workflow of any C program utilizing the flowcharts. Let’s know what are they and how can they assist.

5.1. What is the aim of a Flowcharts?

It is only a visible illustration of the functioning of a program created utilizing a pre-outlined set of shapes or symbols.

5.2. Elements in a Flowcharts

The following desk ought to enable you to in understanding them.

Flowchart Shapes and Meaning
Flowchart Shapes and Meaning

These are just a few fundamental shapes however sufficient to get us began.

Let’s study a number of important phrases.

  1. Algorithm: Algorithm is a quick systematic clarification of a program. It simply explains the working of a program roughly, not the precise code.
  2. Pseudo Code: It is the code written in a human-readable language such as English.

6. Variables Demo in C

Let us begin by a easy program to multiply two numbers.

6.1. Example – C Program to Multiply Two Numbers

6.1.1. Flowcharts & Pseudo Code

C Variables Demo - Multiply Two Numbers
C Variables Demo – Multiply Two Numbers


Step 1: Start.

Step 2: Take enter from the person 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 key phrases, which we’ll see after this code.


void essential()

    int a, b, m;

    printf("Enter two numbers: ");

    m= a*b;

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

6.1.3. Step by Step Program Summary

  1. #embody<conio.h>: This header file tells the pc to incorporate console enter/output capabilities. Hence, conio stands for console enter/output.
  2. Void essential(): It is the first perform in a C program as mentioned in the sooner lessons.
    Note: You have seen us utilizing the int sort with the essential. The principal distinction right here is that void means nothing to return whereas the integer sort requires the perform to return a numeric worth.
  3. scanf(): Used to take enter from a person. The %d signal is for indicating that the info sort is an integer. We will study extra about this in additional chapters. The ampersand (&) signal tells the pc that it ought to retailer the worth of the taken enter in that particular deal with. E.g., &a denotes that the worth of the primary enter will get saved in the situation of a.
  4. getchar(): It tells this system to attend for the response of the person. It stands for “Get Character.” The laptop holds the output display for the person to understand the output.

The output ought to come one thing like this.

Program to Multiply Two Numbers - Output

Now we’ll see an thrilling instance of how variables work.

6.2. Example – C Program to Swap Two Integers

6.2.1. Flowcharts & Pseudo Code

C Variables Demo - Swap Two Integers
C Variables Demo – Swap Two Integers


Step 1: Start

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

Step 3: Accept enter variables from the person (a & b)

Step 4: Swap the 2 variables

# Swap variables

Step 5: Display the values earlier than and after swapping.

Step 6: Stop.

6.2.2. C Coding Snippet


void essential()

    int a,b,c;

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


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

The output ought to come one thing like this-

Program to Swap Two Integers - Output

You might have realized that we used a 3rd variable for swapping the values of the 2 inputs. Can you write a program that may do this with out utilizing the third variable? If you’ll be able to do this, then you’ll actually get the thought of how variables literary work. If not, do not fear, one other instance is accessible subsequent.

We would advocate that you just try very laborious on this. It is simple.

7. Programming Exercise on C Variables

Write a program to swap values of two variables with out utilizing a 3rd variable.



void essential()

    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);

The output would be the identical as the sooner program.

NOTE: We have simply lined the fundamentals of variables right here. As variables is an unlimited matter, we’ll see it in bits and items included in different chapters.

Python  Recommended Post:

Understand C Variables with Flowcharts and Examples
Source hyperlink

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *


You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.