-10

9index, break, user_name, CONSTANT, _member

Got this wrong on a test and I'm wondering where I can find the right answer. Would be highly appreciated. Thanks!

  • 2
    Open your IDE, assign variables to each variable name, then you can get your answer. – Yang HG Mar 20 '19 at 02:57
  • 1
    [Here's where you can find the right answer to this question](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Mar 20 '19 at 02:57
  • https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – drescherjm Mar 20 '19 at 03:04

4 Answers4

2

'9index' is wrong. You never start a variable name with numbers. After the first digit is okay.

'break' is a reserved word

Willian Gaspar
  • 301
  • 3
  • 8
2

9index is not a valid identifier because it starts with a digit.

break is not a valid identifier because it is a language keyword.

user_name is a valid identifier.

CONSTANT is a valid identifier

_member may or may not be a valid identifier. The standard explicitly reservies a number of identifiers for use by the implementation (e.g. the compiler or standard library). Identifiers starting with an underscore are reserved at global scope, but not in other scopes (e.g. to name variables of automatic storage duration within a function). The danger with using reserved identifiers is that no diagnostic is required (i.e. the code can successfully compile) and the code has undefined behaviour.

Anything that is not a valid identifier cannot be used as a variable name (among other things).

Peter
  • 35,646
  • 4
  • 32
  • 74
0

All variable names must begin with a letter of the alphabet or an underscore( _ ). You also cannot use reserved keywords. user_name and _member are valid, the rest aren't. -edit- missed the CONSTANT not being CONST as mentioned in another answer.

Mike Hayes
  • 33
  • 5
0
  • 9index - NO. Variables cannot begin with numbers.
  • break - NO. This is a keyword so cannot be a variable name
  • user_name - YES.
  • CONSTANT - YES. If the variable is called CONSTANT and not const
  • _member - YES. Member variables are allowed to begin with underscore
hookenz
  • 36,432
  • 45
  • 177
  • 286