DEV Community

Debojyoti Chakraborty
Debojyoti Chakraborty

Posted on

A new Way to assign variable in Cpp 10

A new way to assign variable in cpp is:

datatype variable name(any data)

#include<bits/stdc++.h>

using namespace std;

int main(){

int b = 11;
int a(10);

cout<<"new way to print "<<a<<endl;

cout<<"old way to print "<<b<<endl;
}
Enter fullscreen mode Exit fullscreen mode

Output shown here:

➜  ~ g++ test.cpp 
➜  ~ ./a.out      
new way to print 10
old way to print 11
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
selectiveduplicate profile image
Abu Sakib

Yeah it was introduced I think in C++17. It's called constructor initialization (named after how variables are initialized in class constructors). Another way is uniform initialization: int a {10};. Both of these ways can be combined with the old way: int a = (10) and int a = {10}.