+ 6

How do i know where to put brackets { } in c++ program?

i m very confused where should i put those curly brackets😢😢😢

10th Jan 2018, 1:06 PM
DEEPENDU PANDIT
DEEPENDU PANDIT - avatar
3 Answers
+ 8
Also in variable initialisation. (c++11) int a {5}; char b {'b'}; etc see: http://www.informit.com/articles/article.aspx?p=1852519 and https://stackoverflow.com/a/18222927
10th Jan 2018, 1:48 PM
jay
jay - avatar
+ 2
The opening bracket is for the beginning of a function, while the closing one is for the end. For example: #include <iostream> using namespace std; int main() { cout << "Hello world!"; return 0; } The main function's content begins and ends with curly brackets. Hope this helps :)
10th Jan 2018, 1:17 PM
Jacob Garcia
Jacob Garcia - avatar
+ 2
also it help you to hide any variables to the local namespace. #include <iostream> int main() { int a = 1; { int b = 2; cout << a << b; // OK. print 12 } cout << a; // OK. print 1 cout << b; // error. undefined variable } This trick useful when some functional placed to destructor.
10th Jan 2018, 1:31 PM
Alexander Kuslya
Alexander Kuslya - avatar