Data types

Cpp programming variables and types

Cpp programming variables and types

In this tutorial, we will discuss Cpp programming variables and types

Variable is a very fundamental concept of all programming language for programmers. purpose of the variable in programming which is used to allocate memory(space) in memory location before we start to write programming.

variable in memory

Type of variables in C++

Variable declaration

always, when we start programming, we should be defined essential variables. Variable definition means that the programmer allocate to store value needed space or memory location for programming purpose.

Common variable declaration

Syntax

data_Type variable name;

Or

data_Type variable name1, variable_Name2,variable_name3 ............ variable_name n;

 

A variable declaration always ending with semicolon most programming languages

Here, data type means C++ language has many data type such as int, float, double, char etc..

The variable name is unique to help to identify memory locations

 

variable declaration

Declare a integer variable

Integer type

int age;
int age,marks,total;

 

Declare a charactor variable

Character type

char letter;
char letter, alphabet;

 

Declare a float variable

float

float average;
int average, area;

 

Declare a double variable

double

double balance;
double balance, prize;

 

Variable initialization in C++

In the above examples, we declared variables only, but none of them has been assigned or initialized any suitable values.

in this part, we are providing an initial value to variables or assign the value to variables according to their declaration. it is known as variable initialization.

Syntax

method 1

data_type variable_name=value;  //declaration and initilization in single line

 

method 2

data_type variable_name;  //variable declaration
variable_name=value;//variable initilazation

 

Examples

variable initialization

Declare and initialize a integer variable

Integer type

int age=29;//declaration and initilization of age variable
int marks;//declaration of marks variable
marks=67;//initilization of variables

 

Declare and initialize a charactor variable

Character type

char letter='A';//declaration and initilization of letter variable
char alphabet;//declaration of alphabet variable
alphabet='c';//initilization of variables

 

Declare and initialize a floating point variable

float

float average=78.6;//declaration and initilization of average variable
int area;//declaration of area variable
area=345.6//initilization of variables

 

Declare and initialize a floating point large variable

double

double balance=23456.76;//declaration and initilization of balance variable
double prize;//declaration of prize variable
prize=1234.54;//initilization of variables

 

Rules of Declaring variables in C++

  • A variable name can contain Alphabet letter(Upper case A to Z, Lower case a to z), digit  0 – 9  and the underscore character.
  • The first letter must be a letter or underscore.
  • Space can not be used variable name
  • Special charactor(lsimilar#, $) can not used as variable name.
  • Keyword in C++ language  cannot be used as the variable name
  • Variable names always case sensitive

 

Example program for variable

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int age; //declare a integer variable
    age=34; //initialize a integer variable
    char ch;//declare a character variable
    ch='A';  //initialize a character variable
    float avg; //declare a floating point variable
    avg=78.98;//initialize a floating point variable

    cout << "age is: "<<age << endl;    //display the age
    cout << "letter is: "<<ch << endl;   // display the letter
    cout << "average is: "<<avg << endl;  //display the average
    return 0;
}

 

age is: 34
letter is: A
average is: 78.98

 

Suggested for you

Variable in C language

Variable in Java language

keyword in C language

Keyword in Java language

 

C language type of variables with example
Data type in C programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Share
Published by
Karmehavannan
Tags: Cpp language

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago