Valid Variable Names: Rules & Tests

  • Thread starter Deathfish
  • Start date
  • Tags
    Variable
In summary, when it comes to spotting reserved words in a programming test, it is important to be familiar with the specific language's list of reserved words. An IDE or syntax-highlighting editor can make this easier, but if not available, memorizing the reserved words is necessary. The list of reserved words for C is short and all lowercase, and can be found in the C standard (C11).
  • #1
Deathfish
86
0
Hi all, I would like to clarify regarding valid variable names. Well what I fairly understand are the rules regarding names.. What I am unsure about is -- in a test, is there any way to spot a reserved name? For example to make things difficult, trick question, sometimes the question will include

main
print
System
Int

etc. usually in a test we don't have the list of reserved words
 
Technology news on Phys.org
  • #2
Deathfish said:
Hi all, I would like to clarify regarding valid variable names. Well what I fairly understand are the rules regarding names.. What I am unsure about is -- in a test, is there any way to spot a reserved name? For example to make things difficult, trick question, sometimes the question will include

main
print
System
Int

etc. usually in a test we don't have the list of reserved words

You need to be a bit more specific. What language are you asking about? Different languages have different sets of reserved words that can't be used as variable names.
 
  • #3
Regardless of the programming language, if they gave you the list of reserved words and ask you to spot them in the list, that wouldn't be much of test, would it? If you are supposed to be learning a programming language, I presume they expect you to study its reserved words and memorize them; also, to be aware whether the programming language is case-sensitive or not.
 
  • #4
Looks like C to me.

The list of keywords/reserved words in C is short. All of them are lowercase. The C standard (C11)
from what is usually called N1570:
N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x:
6.4.1 Keywords
Syntax
1 keyword: one of
auto ∗
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
inline
int
long
register
restrict
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
_Alignas
_Alignof
_Atomic
_Bool
_Complex
_Generic
_Imaginary
_Noreturn
_Static_assert
_Thread_local
 
  • #5
One reason programmers like to use an integrated development environment, or IDE, (rather than a simple command window) when writing code, is that the IDE's code window will highlight reserved words in a different color, so it is immediately apparent.

If it isn't possible to get an IDE or syntax-highlighting editor for you language, then you must simply memorize (or keep a list of handy) that language's reserved words. There can be a lot of them, so an IDE is the best solution if you can get it.
 

FAQ: Valid Variable Names: Rules & Tests

1. What are valid variable names?

Valid variable names are names that are used to represent a value or data in a program. They can consist of letters, numbers, and underscores, but cannot start with a number or contain special characters such as #, $, %, etc.

2. What are the rules for creating a valid variable name?

The rules for creating a valid variable name are:
1. It can only contain letters, numbers, and underscores.
2. It cannot start with a number.
3. It cannot contain special characters like #, $, %, etc.
4. It cannot be a keyword or reserved word in the programming language.

3. Can a variable name be longer than one word?

Yes, a variable name can be longer than one word. You can use camel case (myVariableName) or underscores (my_variable_name) to make it easier to read.

4. Can a variable name be the same as a reserved word?

No, a variable name cannot be the same as a reserved word or keyword in the programming language. This will result in an error and your program will not run.

5. How can I test if a variable name is valid?

You can test a variable name by trying to use it in your program. If your program runs without any errors, then the variable name is valid. You can also refer to the specific rules for creating a valid variable name in your programming language.

Similar threads

Replies
3
Views
591
Replies
89
Views
4K
Replies
4
Views
1K
Replies
75
Views
5K
Replies
9
Views
2K
Replies
5
Views
3K
Replies
4
Views
783
Replies
1
Views
1K
Back
Top