C++ variables allow programs to store and work with different types of data. There are string, integer, floating point, character, and boolean variable types. Variables must be declared with a valid name and type before use. Variable names must start with a letter, cannot contain spaces or special characters besides underscores, and cannot be keywords. C++ is case sensitive so variable names like amount and Amount are different. Each variable type stores specific kinds of data like integers for whole numbers and strings for text.
C++ variables allow programs to store and work with different types of data. There are string, integer, floating point, character, and boolean variable types. Variables must be declared with a valid name and type before use. Variable names must start with a letter, cannot contain spaces or special characters besides underscores, and cannot be keywords. C++ is case sensitive so variable names like amount and Amount are different. Each variable type stores specific kinds of data like integers for whole numbers and strings for text.
Variables in C++ • Allow storage of data internally for the program • Allow storage of information from the user • There are different types of variables which service different needs • Examples: –Variables that store texts – Variables that store integers (positive & negative numbers) –Variables that store decimals (floating point numbers) Declare/Create variables • In order to use a variable in C++, we have to first declare i.e., create it • Model: variable_type variable_name;
• variable_type: The type of a variable, depends on the
type of data we want to store. • variable_name: The name of a variable, how we want to refer to it in the rest of the program. Legal Variable Names • C++ rules for legal variable names – 1. Must start with a letter, can end with numbers – 2. Must not have a space in the name – 3. Can not have special characters • Only allowed character is "_ "(underscore) – 4. Must not be a C++ keyword • Suggestions for variable names – Variable names should be meaningful – Variable names should be easy to read Illegal Variable Names • Example of illegal variable names –int number of cakes; • Has spaces – int 1number; • Begins with a number – int discount%; • Contains a symbol – double int; • Contains a keyword, int – int string; • Although not a keyword, it will not allow us to declare and use string variables. Important Note • C++ is case sensitive!! • Examples: – int hello; //declares a variable hello – int Hello; //declares another variable Hello – Int hello; //error, Int is not a C++ type – Double amount; //error, Double is not a type Types of variables String • Variable declaration – string name; – string address; – string day; • Examples of strings – name = “Vincent"; – address = "65-30 Kissena Blvd."; – day = "2"; • Examples of INVALID strings – name = ' Vincent '; – address = 65-30 Kissena Blvd.; – day = 2; int (integers) • Variable declaration – int number; – int year; – int age; • Examples of integers – number = 14; – year = 2011; – age = 30; • Examples of INVALID int – number = "14"; – year = '2011'; – age = "thirty"; – age = 34.5 double (decimals, high precision) • Variable declaration – double pi; – double e; • Examples of double (decimals) – pi = 3.1415926535; – e = 2.71828; • Examples of INVALID double – pi = "3.141"; – pi = ' 3.141 '; char (characters) • Variable declaration – char c; – char newline; – char code; • Examples of char (characters) – c = 'c'; – newline = '\n'; – code = 65; //max number is 127 • Example of INVALID char – code = 456; – newline = "\n"; bool(boolean: true or false) • Variable declaration – bool reply; – bool answer; • Examples of bool(bloolean) – answer = true; – answer = false; – reply = 0; – reply = 1; • Examples of INVALID bool – answer = “true”; – reply = ‘0’; //value becomes true