logo codesdope

Gear up


From the last chapter, you know how to print anything on the screen. In this chapter, you will see the different ways in which we can write our code and the way we should write. Let's see these one by one.

Whitespace


In C++, whitespace refers to spaces, tabs and newlines. In some places in our code, whitespace is necessary whereas, in other places, it is just given to improve readability. For example, while writing 'int main()', it is necessary to give a space between int and main() (as they are two different words).

int main()

On the contrary, there is no need to give any space in the following statement with the output operator.

std::cout<<"Hello World";

Although we can give as many whitespaces as we want, the compiler ignores all the unnecessary whitespaces. The following code also runs just fine.

#include <iostream>
int main()
{
std::cout        <<
"Hello World"  ;



return 0;
}
Output
Hello World

In this example, compiler will ignore the space between std::cout and << and the newline between std::cout<< and "Hello World"; and will read the code as std::cout<<"Hello World";.

whitespace in c++

Identifiers


An identifier is the name of any user-defined element. Let's see an example first.

#include <iostream>
int main()
{
	int num;
	num = 2;
	std::cout << num;
	return 0;
}
Output
2

int num; - This declares that variable num is an integer. And by this declaration, num enters into the world of our program. So, 'num' is a variable which will store some integer value.

num = 2; - We are assigning 2 to 'num'. So, now the value of 'num' is 2 (an integer).

Here, num is the name of a variable and thus is an identifier. This example was just to give you an idea of identifiers. You will learn about variables in the coming chapter.

C++ is a case-sensitive language i.e., it distinguishes between uppercase and lowercase. For example, num and Num are different identifiers in C++.

Keywords


There are some predefined reserved words in C++ which have their special meaning and thus cannot be used as identifiers. These words are called keywords.

Let's have a look at C++ keywords.

asm auto bool
break case catch
char class const
const_cast continue default
delete do double
dynamic_cast else enum
explicit export extern
false float for
friend goto if
inline int long
mutable namespace new
operator private protected
public register reinterpret_cast
return short signed
sizeof static static_cast
struct switch template
this throw true
try typedef typeid
typename union unsigned
using virtual void
volatile wchar_t while

using


We know that we write std::cout to tell the compiler that cout is defined in the std namespace. Consider a situation where we have to use cout a large number of times, then writing std:: every time before cout can be a bit annoying. To make our work easier, we use the using keyword.

We can use the using keyword in two ways.

The first one is by declaring that cout is defined in the std namespace by writing using std::cout. Look at the following example.

#include <iostream>
int main()
{
	using std::cout;
	cout << "Hello World" << std::endl;
	cout << "This is my second line" << std::endl;
	return 0;
}
Output
Hello World
This is my second line

In the above example, by writing using std::cout;, we tell the compiler that we will be using cout from the std namespace. So now, there is no need to write std::cout in the rest of the code.
cout << "Hello World" << std::endl; - In this statement, compiler will assume cout as std::cout and "Hello World" will get printed. Note that we have written std:: before endl because we didn't declare endl defined in std namespace, unlike cout.
The next statement cout << "This is my second line"; prints "This is my second line", since compiler again assumes cout as std::cout.

If we use std::cout and cout with some other use, then compiler will prefer std::cout

In the above example, using prevented us from writing std:: each time cout was used. Same as cout, many other objects (like endl, cin) are defined in the std namespace. So we can write the using declaration for all of these thus preventing ourselves from writing std:: before cout and endl again and again as in the following example.

#include <iostream>
int main()
{
	using std::cout;
	using std::endl;
	cout << "Hello World" << endl;
	cout << "This is my second line" << endl;
	cout << "This is my third line" << endl;
	return 0;
}
Output
Hello World
This is my second line
This is my third line

Suppose some of these objects defined in the std namespace are used frequently in our code, then declaring each of these separately belonging to the std namespace with using is not a good option. In that case, there is another way we can use the using keyword and that is by declaring that everything in the code to be defined in the std namespace. We can do this by writing using namespace std;. Let's see the following example.

#include <iostream>
int main()
{
	using namespace std;
	cout << "Hello World" << endl;
	cout << "This is my second line" << endl;
	return 0;
}
Output
Hello World
This is my second line

Here, using namespace std; tells the compiler that everything in the program will belong to the std namespace. Thus there is no need of writing std:: before cout and endl anymore. So whenever the compiler encounters cout or endl, it will assume these as std::cout and std::endl respectively.

using keyword in c++

Taking input


We make our programs for users. So, there must be some way to involve users in our program and to take input from them. In this section, you will learn to do so.

To input some value from user, we use the cin object same as we use the cout object for the output. Like cout, cin is also defined in the std namespace.

cin is also declared in iostream library (short form of input-output stream).

Look at the following example in which the user is entering some value for a variable which is then printed on the screen.

#include <iostream>
int main()
{
	int n;
	std::cin >> n;
	std::cout << n;
	return 0;
}
Output
10
10

First, we declared an integer variable 'n' by writing int n;. So, now we have a variable 'n' which can store a value of an integer.
std::cin >> n; - This statement asks user to enter some value of the variable 'n'. As mentioned above, since cin is also defined in the std namespace, so we wrote std:: before cin. Its syntax is same as that of cout, except that we use the input operator >> after cin in place of the output operator ( << ) which we use in cout.
std::cout << n; - In this statement, we are simply printing the value of the variable 'n'.

Conventions


There are certain conventions which must be followed while writing a program in C++ to reduce the chances of errors and make the code more readable.

Following is a set of rules which must be followed while writing a code in C++.

  • Identifier cannot be given the name of a keyword
  • Identifier can consist of only letters, digits and underscore(_)
  • An identifier can only start with a letter or an underscore. For example, Robin123, numVal_ and _iden are valid identifiers whereas 4num and value@ are invalid.
  • We can also name a function, class or a variable in either CamelCase or underscore_case. Though it is a general convention to name methods or functions with camelCase starting with lower case. For example, getMarks(), setAge().
  • The braces enclosing the body of a function should be aligned with the function name. Although it will not generate any error, but it makes our code more readable.
  • int main()
    {
    }

  • Within the braces, each statement should start from a distance of one tab from the alignment of the braces.
  • int main()
    {
        std::cout << "Hello!";
        return 0;
    }

In theory, there is no big difference between theory and practice. But in practice, there is.
-Yogi Berra

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.