Basics of C++
Let us talk about some basics of the language.
Identifiers
All variables and functions and objects in C++ need names - they need identifiers.
Identifiers in C++ need to follow some simple rules
-
They should start with either alphabets a-z/A-Z or _ (underline).
-
They are case sensitive.
-
They can contain only alphabets, digits or underline.
-
They need to be unique in their scope.
-
They can not be keywords.
Some correct identifiers are
a, firstVar, second_var, sum_of_numbers, x1
Examples of some invalid identifiers are
1var //starts with digit
sum of numbers//spaces in between
int//is a keyword
Comments
By the way, double slash(//) is used for writing a comment. The text followed by // until the next line is considered as comment and is not part of the program code and is ignored by the compiler.
A multiline comment can also be written using a pair of /* and */. Whatever is enclosed within a pair of /* and */ is considered as comment. This is quite useful in temporarily removing some parts of code.
int main()
{
//this is a comment
int a = 10;
if(a>1) /*this is also
a comment*./
....
}
Data types :
Every variable used in a C++ program must be defined with a data type. A variable must be defined specifying its name and data type before it can be used. (Otherwise you get a compiler error - undefined variable)
C++ has the following basic data types.
-
int
-
float
-
char
-
double
-
bool
int
An int data type is used to store integer values - positive and negative. An integer is stored using 4 bytes in 32 bit machine and is stored using 8 bytes in 64 bit machine.
e.g.
int a=10;
int b = -100;
int c,n1,n2;
Prefix 0 and 0x for integers
An integer literal starting with letter 0 (zero not o) is treated as octal number and a number starting with 0x is treated as hexadecimal integer.
int m = 033; //m is 27
int n = 0x12;//n is 18
float and double
These types are used to store real numbers - both decimal and exponential values.
e.g.
float m = 1.9f; double c = 22.3; float x,y,z;
.
float numbers are stored using 4 bytes and double are stored using 8 bytes. Because of this double gives a better accuracy than float.
By default, a floating point literal value is taken as double. To mention that it is float type, we have to use a suffix of f or F.
A float literal must have a suffix of f or F.
So 1.2 is a double value but 1.2f is a float value.
Also a float variable has a precision of 7 digits and a double (which stands for double precision) has a precision of 15 digits.
char
char variable stores a single character which can be an alphabet, a digit or a special character.
Character variables can be initialized using character literals.
Character literals are enclosed in single quotes. e.g. 'a', '+', '9' etc.
char ch = 'A';ch = ch+10;
Character variables can even be initialized using integer values in the range -256 to 255 corresponding to the ASCII code or unicode of that character.
The second statement in the example above, ch = ch+10 shows us that the character literals can be treated as integers and can be used in arithmetic operations.
e.g.
char ch = 'A';
int m = ch++;//m will be 66 - code of A +1
Strings
character strings or words and longer texts are stored using array of characters. They can also be stored as an object of library class string.
e.g.
char st[15]="Hello world";
string s1="hi world";
In first example. st is a an array of 15 characters and it stores the string "Hello world". st[0] is 'H', st[1] is 'e' etc. We will see more about Arrays later.
In the second example s1 is an object of library class string. It stores the string "hi world".
bool
Bool is a data type which is used to store boolean values - true or false.
bool m = true;bool n = false;
Note that, bool values are read and displayed as integer values 1 and 0 - 1 is true and 0 is false. In fact any non-zero value is true.
Variables
In C++, variables should be defined before use.
When you define a variable, you need to specify its
-
name - should be unique
-
data-type
-
optional initializer
Using undefined variables in program gives a compilation error.
Variable names are identifiers and must follow all the rules of the identifiers mentioned above. Names of variables should be unique in their scope.
Some examples of variable definitions are
int m; char ch;
float a,b,c;
double d =12; float r = 1.2f;
float d;//error - d is already defined
By now, you have noticed that each statement in C++ must end with a semicolon.
Let us write a simple program to read two numbers and print their product.
Note : cin is used to read a value from console and cout is used to display a value on the console.
The above program defines two integer variables a and b. Then it reads them from console (using cin) and displays their product. In the next line, it defines a float variable c and initializes it with 100. Then its square, c*c is displayed. Next a boolean variable d and character variable ch are defined and printed.
The output of this program with 11 and 22 as input is:
Product of 11and 22 is242 The square of number is 10000 The boolean varaible is 0 ch is A
Exercises
-
Define two integer variables x and y. Initialize x with octal value 77 and y with hex value ff3.
-
Define a float variable z and initialize z with -2.6
-
Define a boolean variable bl.
-
Define ch1 and ch2 as character variables and initialize them with a and b.
-
Write the output of the following program
#include<iostream>
using namespace std;
int main()
{
int a = 100;
cout<<a;
}