std::string str = "text"; // stores a string
int foo = 3; // stores any integer
float bar = 3.14; // stores 32 bit number
double baz = 3.14159265; // stores 64 bit number
#include <iostream>
using namespace std;
int main() {
// To define variables in C++, you have to specify the data type. Example:
int number = 10; // Declares a variable with the integer data type.
float decimal = 3.5; // Declares a variable with the float data type.
double decimalNum = 3.3333; // Doubles are used for more specific points in floats.
string text = "Hello World"; // Declares a variable with the string data type.
bool result = true; // Declares a variable with the boolean data type.
}
Variable is simply a name or a label given to a memory location.
// C++ program to show difference b/w definition and declaration of a variable
#include <iostream>
using namespace std;
int main()
{
// this is declaration of variable a
int a;
// this is initialisation of a
a = 10;
// this is definition = declaration + initialisation
int b = 20;
// declaration and definition
// of variable 'a123'
char a123 = 'a';
// This is also both declaration and definition
// as 'c' is allocated memory and
// assigned some garbage value.
float c;
// multiple declarations and definitions
int _c, _d45, e;
// Let us print a variable
cout << a123 << endl;
return 0;
}
// Syntax for Declaring a single variable
type variable_name;
// Syntax for Declaring multiple variables:
type variable1_name, variable2_name, variable3_name;
// Examples
int a;
int x,y,z;
char c;
char c1,c2,c3;
Code Example |
---|
Cpp :: delete a head node in link list |
Cpp :: Set Specific Time in youtube Video url |
Cpp :: iteration in c++ |
Cpp :: longest increasing subsequence nlogn c++ |
Cpp :: stream in c++ |
Cpp :: c++ new operator |
Cpp :: equal elements in two arrays in c++ |
Cpp :: x += c++ |
Cpp :: accumulate in c++ |
Cpp :: c++ delete int |
Cpp :: no match for ‘operator=’ (operand types are ‘std::basic_ostream’ and ‘int’) |
C :: _CRT_SECURE_NO_WARNINGS |
C :: sleep in c programming |
C :: full installation of clang in ubuntu |
C :: how to print something out to the console c |
C :: save numpy array to text file |
C :: if statement shorthand c |
C :: how to get user input in c |
C :: remove element from np array |
C :: graphics in c |
C :: exclamation mark in c |
C :: c define array size |
C :: c binary search |
C :: pthread c |
C :: Access denied creating xampp-control.ini |
C :: what is syntax in programming |
C :: plt legend top right outside |
C :: functions in c |
C :: getchar |
C :: Palindrome number in c program |