How to define a variable in C++?

C++Object Oriented ProgrammingProgramming

To define a variable in  C++, you need to use the following syntax −

Syntax

datatype variable_name;

You need to know what type of data is your variable is going to hold and what it will be called. The variable name has constraints on what you can name it. Following are the rules for naming variables −

  • Variable names in C++ can range from 1 to 255 characters.
  • All variable names must begin with a letter of the alphabet or an underscore(_).
  • After the first initial letter, variable names can also contain letters and numbers.  
  • Variable names are case sensitive.
  • No spaces or special characters are allowed.
  • You cannot use a C++ keyword (a reserved word) as a variable name.

Here are some examples of acceptable variable names −

mohd       Piyush    abc   move_name    a_123
myname50   _temp      j      a23b9      retVal

Examples of defining variables −

int my_var;
float my_float;
double num;
raja
Published on 16-Feb-2018 06:15:18
Advertisements