




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





While writing program in any language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. You may like to store information of various data types like character, wide character, integer, floating point, double floating point, boolean etc. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory.
C++ offers the programmer a rich assortment of built-in as well as user defined data types. Following table lists down seven basic C++ data types โ Type Keyword Boolean bool Character char Integer int Floating point float Double floating point double Valueless void Wide character wchar_t Several of the basic types can be modified using one or more of these type modifiers โ
When the above code is compiled and executed, it produces the following result which can vary from machine to machine โ Size of char : 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of wchar_t : 4
You can create a new name for an existing type using typedef. Following is the simple syntax to define a new type using typedef โ typedef type newname; For example, the following tells the compiler that feet is another name for int โ typedef int feet; Now, the following declaration is perfectly legal and creates an integer variable called distance โ feet distance;
An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration. Creating an enumeration requires the use of the keyword enum. The general form of an enumeration type is โ enum enum-name { list of names } var-list; Here, the enum-name is the enumeration's type name. The list of names is comma separated. For example, the following code defines an enumeration of colors called colors and the variable c of type color. Finally, c is assigned the value "blue". enum color { red, green, blue } c; c = blue; By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But you can give a name, a specific value by adding an initializer. For example, in the following enumeration, green will have the value 5. enum color { red, green = 5, blue }; Here, blue will have a value of 6 because each name will be one greater than the one that precedes it.
A variable provides us with named storage that our programs can manipulate. Each variable in C++ has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive โ There are following basic types of variable in C++ as explained in last chapter โ Sr.No Type & Description 1 bool Stores either value true or false. 2 char Typically a single octet (one byte). This is an integer type. 3 int The most natural size of integer for the machine. 4 float A single-precision floating point value. 5 double A double-precision floating point value. 6 void Represents the absence of type. 7 wchar_t A wide character type.
a variable multiple times in your C++ program, but it can be defined only once in a file, a function or a block of code.
Try the following example where a variable has been declared at the top, but it has been defined inside the main function โ #include
Same concept applies on function declaration where you provide a function name at the time of its declaration and its actual definition can be given anywhere else. For example โ // function declaration int func(); int main() { // function call int i = func(); }
// function definition int func() { return 0; }
There are two kinds of expressions in C++ โ