TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Understanding C Variables
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
C Tutorials

Understanding C Variables

Last updated: Sep 12, 2023 2:25 am
By Meenakshi Agarwal
Share
10 Min Read
Understand C Variables with Flowcharts and Examples
SHARE

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 of C. Hence, please read and understand with full concentration.

Contents
Local VariablesGlobal VariablesInteger (int)Floating-point (float)Double (double)Purpose of a FlowchartElements in a FlowchartC Program to Multiply Two NumbersFlowchart & Pseudo CodeCoding SnippetStep-by-Step Program SummaryC Program to Swap Two IntegersFlowchart & Pseudo CodeCoding SnippetSolution:

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 point 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.

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.

Type of Variables in C

In C programming, variables are of two types:

  1. Local,
  2. Global

Local Variables

Local C variables have a limited scope within the 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.

Global Variables

A variable that 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.

C Variables Data Types

Data types enable programmers to define variables that 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.

Integer (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.

Floating-point (float)

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

Double (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.

Role of a Flowchart in C

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

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.

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: An 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.

Demo of C Variables using Sample Programs

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

C Program to Multiply Two Numbers

Flowchart & Pseudo Code

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

Algorithm:

1: Start.

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

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

4: Display output (m)

5: Stop

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

Step-by-Step Program Summary

  1. #include<conio.h>: This header file tells the computer to include console input/output functions. Hence, the term, <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.

C Program to Swap Two Integers

Flowchart & Pseudo Code

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

Algorithm:

1: Start

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

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

4: Swap the two variables

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

5: Display the values before and after swapping.

6: Stop.

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.

Test Exercise on C Variables

Write a program to swap the 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 are a bigger topic, we will see it in bits and pieces included in other chapters.

You Might Also Like

C Programming Language “Struct”

20 C Programs for Beginners to Practice

Learn C Programming – How To Guide for Beginners

Python vs C++ – Is Python or C++ Better?

The Best Top-Down Approach Guide for You in C

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Write your first C program in simple and easy steps. First C Program Simplified for You
Next Article C Datatypes Range and Sizes Understand C Datatypes

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x
x