Variables in C

explanation of what is a variable, and how variables are created in C. The information applies to all high level programming languages.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.

Types of Data

Some examples of basic data we work with in our day to day life are
  1. Integer numbers like 78, 632
  2. Floating point numbers like 23.7, 975.003
  3. Single characters of text like - 'a', 'Y', '$', etc.,
  4. Strings of characters like words - "hoven", "India", etc.,

Data Types

Each data item has a type associated with it. This type describes the nature of that data. For example we have substances in nature, some are liquid, some are solid and some are gaseous. Each type of substance can be stored only in special types of containers and vessels meant for storing them. For example, liquids can be stored in cans, and bottles only.

Similarly, each data can be stored only in the type meant for it. Integer numbers can be stored in an "int" datatype. Floating point numbers can be stored in "float" datatype. Single character types can be stored in "char" type, and strings of text can be stored in "string" type. The code below creates an int type of variable to store an integer number called 99. Here "i" is the name of the container, and it's type is int.

int main()
{
    int i = 99;

    return 0;

}
More variables have been created in the code below -
int main()
{
    int i = 99;
 
    float h = 9.8;
 
    char c = 'A';
 
    return 0;
 
}

Identifiers

In the code above, the containers "i", "h", etc., are technically called identifiers. You can give any name to an identifier, but these restrictions must be kept in mind -
  1. Identifier names are written only with these characters: a-z, A-Z, 0-9 or an underscore.
  2. The name cannot start with a number.

I/O of integers in C

Create an int variable to store the number 5. Then display the contents of that variable.
#include<iostream>
#include<cstdio>
 
int main()
{
    int c = 5;
    
    printf("Number = %d",c);
    
    return 0;
}

I/O of floats in C

Create a float variable. Obtain an input from the user to initialize that variable. Then display the contents of that variable.
#include<iostream>
#include<cstdio>

using namespace std;
 
int main()
{
 
     float a;
 
     printf("Enter value: ");
 
     cin >> a;
     
     printf("Value = %f ",a); //%f is used for floats
 
     return 0;
 
}

I/O of characters

Create a char variable. Obtain an input from the user to initialize that variable. Then display the contents of that variable.
#include<iostream>
#include<cstdio>

using namespace std;
 
int main()
{
     char var1;
 
     printf("Enter character: ");
 
     cin >> var1; 
 
     printf("You entered %c.",var1); 
 
     return 0;
}

I/O of characters and ASCII code

#include<iostream>
#include<cstdio>

using namespace std;
 
int main()
{
     char var1;
 
     printf("Enter character: ");
 
     cin >> var1; 
     
     printf("You entered %c.",var1); 
 
     cout << "ASCII = " << (int)var1 << endl;
 
     return 0;
}

Swapping two numbers

Write a program to interchange two numbers.
#include<iostream>
#include<cstdio>

using namespace std;
 
int main()
{ 
     int a;
     
     int b;
     
     printf("Enter first no. ");
     
     cin >> a;
 
     printf("Enter second no.");
 
     cin >> b;
     
     int c = a;
     
     a = b;
     
     b = c;
     
     printf("%d \n %d", a, b);
 
     return 0; 
        
}

Swaping - Method 2

write a program to interchange two numbers without using third variable

#include<iostream>
#include<cstdio>
 
using namespace std; 
 
int main() 
{ 
 
    int a, b;
    
    cout << "Enter the first no. = ";
    
    cin >> a;
    
    cout << "Enter the second no. = ";
    
    cin >> b; 
    
    a = a + b;
    
    b = a - b;
    
    a = a - b;
    
    printf("After interchange \n");
    
    printf("First is no. = %d \nSecond is no. = %d", a , b);
    
    return 0 ;
 
}

Square

Ask the user to enter a number. Write code to print the square of that number.
#include<iostream>
#include<cstdio>
 
using namespace std; 
 
int main() 
{ 
 
    int a;
    
    cout << "Enter no. = ";
    
    cin >> a;
    
    int num = a * a;
    
    cout << "Square is = " << num << endl; 
    
    return 0 ;
 
}

Square and cube

Write a program to get square and cube of number. Get the number from the user
#include<iostream>
#include<cstdio>
 
using namespace std; 
 
int main() 
{ 
 
    int a;
    
    cout << "Enter no. = ";
    
    cin >> a;
    
    int square = a * a;
    
    int cube = a * a * a ;
    
    cout << "Square is = " << square << endl; 
    
    cout << "Cube is = " << cube << endl; 
    
    return 0 ;
 
}

Classroom Training

Classroom training in C/C++ is available at our training institute. Click here for details. You can also register yourself here.

Video Tutorial

We have created a video tutorial that can be downloaded and watched offline on your windows based computer. It is in the form of an exe file. No installation is required, it runs by double clicking the file. Since the exe file is hosted on the Google Drive, it should be very safe because Google itself checks for any virus or malware. The video can be watched offline, no internet is required.

Pricing

This single video will cost you USD $5. When you download the video, you will be able to watch the first few minutes for free, as a sample. The video app will prompt you for payment through PayPal and other payment options. If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials The entire set is priced at USD $50.

Screenshots

This is a screenshot of the software that should give you a good idea of the available functionality. Click on the image to see a larger view.screenshot of the video being played in the software

Installation

This software doesn't require any installation. Just download and double-click to run it. It doesn't require administrative priviliges to run. It runs in limited access mode, so should be very safe.

supports windows xp and later Supported Operating Systems

These videos can be run on 32-bit as well as 64-bit versions of the Microsoft Windows Operating Systems. Windows XP and all later OS are supported. If you want to run it on Windows XP and Windows Vista, then you must also have the .NET Framework Version 3.5 installed on your machines. Those users who have Windows 7 and later donot have to worry because these operating systems already have the .NET Framework installed on them.

Download Links

IMPORTANT NOTE: This download contains ONLY ONE video. The video is about the topic of this tutorial.

Want ALL Videos? If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials. TIP: If you plan to buy these videos, then it would be more economical to buy the entire set.

DISCLAIMER: Even though every care has been taken in making this software bug-free, but still please take a proper backup of your PC data before you use it. We shall not be responsible for any damages or consequential damages arising out of the use of this software. Please use it solely at your own risk.

Please download the software here.
download the video Variables in C



Creative Commons License
This Blog Post/Article "Variables in C" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-10-05