'How to use spaces in a "variable name"?
I have a very simple question but i just started learning C. I'd really appreciate if i could get some help because i'm trying to understand how the syntax works in the language.
What i want to do is something like this
int Score1, Score2, "Final Score";
the error message i get is this:
error: expected identifier or '(' before string constant
Solution 1:[1]
If you want to define a variable with a name that contains a blank character you cannot do it.
You should try removing the quotes with the variable name final_score .
Solution 2:[2]
In general (and definitely in C), variable names must be valid identifiers1 and cannot contain spaces or quotation marks.
Consider naming the variable finalScore or final_score instead.
1 The syntax of an identifier is succinctly given in The C Programming Language (K&R), A.2.3 Identifiers:
An identifier is a sequence of letters [a-z, A-Z, _] and digits [0-9]. The first character must be a letter; the underscore _ counts as a letter ..
Solution 3:[3]
Do you mean:
int score1, score2, final_score;
Solution 4:[4]
A variable name in C can contain any combination of alphabets, numbers and under-score (_). Any other special characters are not allowed in variable name.
Removing double quotes and using final_score or finalScore will work.
Also note that you can't have a variable name legal if it starts with a digit, although it can start with under-score
Solution 5:[5]
C, along with many other programming languages, don't allow any spaces, and some other special characters in variable names. You can however say something like: finalScore, or final_score
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Otrebor |
| Solution 2 | |
| Solution 3 | Joey Moto |
| Solution 4 | Lavish Kothari |
| Solution 5 | ProMechaFox |
