Search

Understanding Variables and Data Types are made easy

Hi, in this post i am going to give overview of what are variables and datatypes in any programming language.

Let's start,

Observe the below 'application form'


It is a very simple form with basic details. But how we wrote those , there are two sections one is left side with fields(name, age, dob, gender, address, mobile no, email id) and second is right side with values(john, 24, 09/09/1996, m, Bangalore, 9988998899, john@gmail.com). In programming languages (Python, C, C++, Java, C#),the fields(name, age, dob, gender, address, mobile no, email id) which are taking values are called as variables.

VARIABLES are the containers which storing the values . Every programming language must have the variables. without variables you cannot develop applications.

As human being we understand which value to write under which field. we can't write 24 to 'name' field. because 24 is the age. If it is a paper we can simply fill that form.

But if it is an online application form the computer should know which type of value should store in which fields(variable). Every programming language has some feature to specify which type of value to store in field. that feature is DATA TYPE.

DATA TYPE is nothing but type(Number,text,boolean) of the value which is storing in the variable.

In almost all programming languages like C, C++, Java, C# etc... we need to specify datatype while declaring variables except in python programming language.

Syntax for declaring datatype in C, C++, Java, C# is,

we can assign the value at the time of declaration or we can assign later,

data_type variable_name = value;
          or 
data_type variable_name;
variable_name= value;

Syntax for declaring datatype in Python is,

variable_name= value;

Note : data type is auto detected in python , we don't need to specify the data type


Built In DATA TYPES :

The following are the common data types in top programming languages. There may be some other data types.

Number data type : this type of variable stores only numbers

int : stores values like 567, 8, 9999

float : stores floating point values like 56.6, 6.9

long : stores long numbers like 98989898989, 12131212121

double : stores decimal values like 79879232.6546, 654654253.2252

Text data type:

char : it stores single character like 'A' , 'Z' , 'c'

string : it stores string values like "john", "Hyderabad"

Boolean data type:

this type of variable stores true or false.


Examples of Variable declaration in different programming languages


C++

#include <iostream>
#include<string>
using namespace std;

int main()
{
    //declaring variables
    string name = "john";
    int age = 24;
    char gender = 'M';
    float weight = 58.9;
    long mobile_no = 9988998899;
    bool disabled = false;
}

JAVA

public class HelloWorld{
    String name = "john";
    int age = 24;
    char gender = 'M';
    float weight = 58.9f;
    long mobile_no = 9988998899L;
    boolean disabled = false;
     public static void main(String []args){
        System.out.println("Hello World");
     }
}


C#

using System;
namespace VariableDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "john";
            int age = 24;
            char gender = 'M';
            double weight = 58.9;
            long mobile_no = 9988998899;
            bool disabled = false;
        }
    }
}

Python

In python no need to put datatype(int, long, double, string), it will auto detect.

name = "john"
age = 24
gender = 'M'
weight = 58.9
mobile_no = 9988998899
disabled = False

This is the basic understanding of variables and data types in programming languages

57 views1 comment

 Contact Us:

                  codecalculatenr@gmail.com

 

About Us : 

        Code Calculate is the biggest online platform