Declare and Assign Values to Variables in C++:
(1) In this blog, we will see how to declare and assign values to variables.
- int myInt = 10;
- double myDouble = 10.5;
- char myChar = ‘A’;
- string myString = “My first String”;
- bool myBool = True;
(2) Sample C++ language code and its output, as shown:
#include <iostream> using namespace std; int main() { //Declare int variable intmyfirstinteger = 10; cout<< “Integer Variable Value:” <<myfirstinteger<< “\n”; //Declare double variable doublemyfirstdouble = 10.5; cout<< “Double Variable Value:” <<myfirstdouble<< “\n”; //Declare char variable charmyfirstchar = ‘A’; cout<< “Char Variable Value:” <<myfirstchar<< “\n”; //Declare bool variable boolmyfirstbool = true; cout<< “Bool Variable Value:” <<myfirstbool<< “\n”; //Declare string variable stringmyfirststring = “My First String”; cout<< “String Variable Value:” <<myfirststring<< “\n”; return 0; } |
(3) After that go to the “Build” menu and select the “Build” option for publishing the code as shown.
(4) After building the code, press the “Run” button, as shown.
(5) After pressing the “run” button, the output screen is opened, as shown.