2

I am in first year in BSc Computer Science. I received a comment from my Professor on my recently submitted assignment . I initialized an int variable to zero : int count{0};. The book assigned to us in the course gives only one way to initialize a variable by using an assignment statement. int count = 0;

I don't remember where I learnt the curly braces method to initialize the variable. According to my professor , this is not a legal way to do it. My program runs without any errors in Atom and also on online debugger. I always check my program for errors from two different platforms. So, I am confused whether my method was wrong and was missed by the compiler or this method is legal but not considered standard.

Any clarification will be helpful. Also any advice on good programming practices for debugging, so it doesn't happen again as I lost 4 marks from a 10 mark assignment.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
tark
  • 23
  • 3
  • 1
    See [Initialization](https://en.cppreference.com/w/cpp/language/initialization) and follow the included links for more details. – Richard Critten Nov 02 '21 at 18:43
  • 11
    Your professor seems to have not noticed past 10 years in language development. Uniform initialization with curly braces is valid since C++11. If you don't manage to convince him to upgrade his compiler, you can use `-std=c++98` to make your own compiler conform to the old standard. – Yksisarvinen Nov 02 '21 at 18:45
  • [Good viewing to expand on the documentation](https://www.youtube.com/watch?v=7DTlWPgX6zs). – user4581301 Nov 02 '21 at 18:46
  • Testing on two different IDEs means nothing if they're using the same compiler. You could do all your testing in Atom only if you are able to install gcc and clang, for example. It's worth fighting for those points back, using the cppreference link posted. – sweenish Nov 02 '21 at 18:48
  • Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Nov 02 '21 at 18:49
  • Side note: Online IDEs often miss out on some of the groovy tools you get with a desktop install of the toolchain like profilers and debuggers. It's in your best interests to have a full-featured development environment ([here's a link to installation instructions for a good one](https://stackoverflow.com/a/30071634/4581301)) that is under your control. Even if you have to submit using an online tool, you can get started on your computer and knock most of the bugs out with the extra tools before submitting. – user4581301 Nov 02 '21 at 18:52
  • 4
    @NathanOliver sounds more like the professor needs the book. I suppose the book could be used as a metaphorical bludgeon, though. – user4581301 Nov 02 '21 at 18:53
  • `int count{0};` is completely fine unless you are using C++03 or older, which at this point are antiques. Your professor may be a dinosaur, out of touch with the modern evolution of the language. This syntax has been valid for over 10 years, it isn't reasonable to even call it a new feature anymore. – François Andrieux Nov 02 '21 at 18:54
  • 2
    Before citing any standards or docs to your prof, make sure you check your syllabus or assignment and that it doesn't say that you explicitly have to compile using any old standards. I once had a class that enforced old C syntax on exams. (Old as in "no initial statements in for loops are allowed" old.) It was frustrating, but those were the rules of the game. – General Grievance Nov 02 '21 at 18:58
  • 1
    And absolutely use a light hand when you bring this up with the professor. You don't want to win the argument and find you're dealing with an egomaniac who'll make you pay for those 4 marks many times over for the rest of the course. – user4581301 Nov 02 '21 at 19:09
  • Thanks so much for clarification and advice. I talked to the professor and he gave me 2 marks back and deducted 2 marks for not putting the main comment on top of the program which serves as a heading for the program (What the program meant to do). I will take it as a win . I wouldn't have known about different kinds of variable initialization (at least until second year lol) if the professor haven't deducted marks. At least , now I can look into this topic now . But I am going to use the assignment operator for initialization for this semester to keep him happy . LOL. – tark Nov 02 '21 at 21:18

1 Answers1

5

Here is how you may initialize the variable count of the type int with zero

int count = 0;
int count = { 0 };
int count = ( 0 );
int count{ 0 };
int count( 0 );
int count = {};
int count{};

You may not write

int count();

because this will be a function declaration.

If to use the specifier auto then these declarations

auto count = { 0 };
auto count = {};

must be excluded from the above list because in this case in the first declaration the variable count will have the type std::initializer_list<int> and in the second declaration the type of the variable can not be deduced.

Pay attention to that the initialization of scalar objects with a braced list was introduced in C++ 11.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335