DEV Community

Cover image for #2 Understanding Variables, Input and Output: C++ and DSA
Bilal Sadiq
Bilal Sadiq

Posted on

#2 Understanding Variables, Input and Output: C++ and DSA

Last blog we studied the introduction to C++. In this blog post, we'll be studying about variables, how to take input from the user and how to dispaly the output and how they form the building blocks of C++ progrms.

What are variables?

Variables are conainers that hold data in a program. they act as placeholders for storing information that can be manipulated and used through out the program's execution. In C++, variables must be declared beforethey are used. A declaration specifies the data type of the variable and the name of the variable.
Syntax and examples for variable declaration:

Image description

Once the variables are declared you can assign values to it using assignment operator "=":

Image description

Beside assigning values to the variables manually, one can take them as input from the user, which is discussed below.

Input in C++:

Getting input from the user is a basic requirement in many programs. This can be done using "cin" stream available in C++.
To get input from the user you need to include the header file "isotream" in your program as shown below :

Image description

Example of taking input using "cin" stream :
Image description

This will show the Output as:
Image description

In this example, the program prompts the user to enter marks, reads the input using "cin", and then displays the entered value using "cout".

Output in C++:

Output in C++ is achieved using the cout stream. It is used to display information to the console.

Example demostrating Output in C++:

Image description

Output from the above code:
Image description

In this example, the program outputs the values of variables marks, cgpa, and grade to the console.

This was all for today. Next we will be seeing the Operators in C++.
Happy Coding!

Top comments (0)