Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between { } and equal sign variables

I'm somewhat new to c++ programming. I couldn't find my answer any where on google so hopefully it can be answered here.

is there a difference between the following

unsigned int counter{ 1 };

or

unsigned int counter = 1;

the book uses the first option and its confusing to me because it doesn't explain the difference. below is the following code from the book i'm following.

#include <iostream>
#include <iomanip>
#include <cstdlib> // contains function prototype for rand()
using namespace std;

int main()
{
    for (unsigned int counter{ 1 }; counter <= 20; ++counter) {
        cout << setw(10) << (1 + rand() % 6);

        // if counter is divisible by 5, start a new line of output
        if (counter % 5 == 0) {
            cout << endl;
        }
    }

}
like image 978
Nick Avatar asked Sep 19 '19 11:09

Nick


People also ask

What is the difference between and == signs?

(=) is a Assignment operator while (==) is a Equal to operator. (=) is used for assigning the values from right to left while (==) is used for showing equality between values.

What is the difference between single and double ==?

Single = is an assignment operator used to assign the value to the variable. Double = = is rational operator used to compare two variable whether they are equal or not.

What is the difference between == and === in programming?

== is used for comparison between two variables irrespective of the datatype of variable. === is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.

What does double == mean?

A double equal sign means “is equal to.” Notice the line above involving the double equal sign?


Video Answer


2 Answers

Yes, they are two different types of initialization in C++.

  • The first one, i.e., unsigned int counter{ 1 }; is a direct initialization.

  • Whereas, the second one, i.e., unsigned int counter = 1;, is a copy initialization.

For all the details you can directly refer to the documentation.

However, I can emphasize:

Copy-initialization is less permissive than direct-initialization: explicit constructors are not converting constructors and are not considered for copy-initialization.

Initialization references here

For unsigned int type (such in your case), there are no real differences between the two initializations.


Note The usage of curly braces in the first statement (unsigned int counter{ 1 }) provides an additional constraint:

Otherwise (if T is not a class type), if the braced-init-list has only one element [...], T is direct-initialized [...], except that narrowing conversions are not allowed.

In other words, the usage of curly braces in the initialization does not allow data looseness.

That is:

unsigned int counter{ 12.3 };  // error!!!

won't compile because you are trying to initialize an integer with a floating-point value.

Note this is a "property" of curly braces in the initialization. It is not strictly related to the initialization type.

In fact, you can also write:

unsigned int counter = { 12.3 };  // error!

which is, instead, a copy initialization, but having curly braces does not allows narrowing conversions.

like image 75
BiagioF Avatar answered Sep 22 '22 00:09

BiagioF


Adding to the other explanations above. I would use the first option(unsigned int counter{ 1 };) to initialize a list of values for a variable and for a single value, I would prefer to use the second option(unsigned int counter = 1;)

Hope this helps.

like image 32
Ishaan S Avatar answered Sep 22 '22 00:09

Ishaan S