TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C++

C++ Variable Declaration

Overview

In this tutorial, we will learn C++ Variable declaration. We need to declare variables in the C++ program before using them in the program.

A variable declaration specifies the data type and the name of the variable used in the program. C++ is a statically typed language. The compiler should know the type and how much space would be needed for the variable at compile time.

Declare single variable

We can specify the type of the variable and the variable name. For example, to declare a variable sno as an integer:

int sno;

C++ Variable Declaration

We can also initialize the variables during the declaration. Initializing a variable is assigning an initial value to the variable. For example, to declare and initialize the variable sno to 1

int sno = 1;

Declare multiple variables

We can also declare multiple variables of the same type with comma-separated names. For example, to declare price, salesTax, and totalPrice as double:

double price, salesTax, totalPrice;

Code Snippet

Let’s declare variables i,j, and k. Assign the variables with values 10,20,30 respectively.

/************************************
* C++ Variables
* Filename: variables.cpp
* Declare variables in C++ Program
* C++ Tutorials - www.TestingDocs.com
**************************************/
#include
using namespace std;
int main()
{
 int i=10,j=20,k=30;
 //output variables
 cout << "i = " << i << endl;
 cout << "j = " << j << endl;
 cout << "k = " << k << endl;
 return 0;
} // end main

Output
i = 10
j = 20
k = 30

 

Multiple Variables in C++ CodeBlocks

—

C++ Tutorials

C++ Tutorials on this website:

https://www.testingdocs.com/c-coding-tutorials/

For more information on the current ISO C++ standard

https://isocpp.org/std/the-standard

Related Posts

g not found OpenSuse

C++ /

Install GCC C++ Compiler on OpenSuse

sum of digits c program

C++ /

Sum of Digits of a Number C++ Program

C++ Hello World Program

C++ /

C++ Hello World Program

C++ /

Object-Oriented Programming Language Examples

C++ /

Object-Oriented Programming Features

‹ C++ Program to Compute Circumference of a Circle› C++ Arithmetic Operators

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version