Mateus Ledur
Mateus Ledur

Reputation: 37

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

Upvotes: 1

Views: 3009

Answers (5)

ProMechaFox
ProMechaFox

Reputation: 1

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

Upvotes: -1

user2864740
user2864740

Reputation: 61935

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 ..

Upvotes: 6

Lavish Kothari
Lavish Kothari

Reputation: 2331

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

Upvotes: 1

Otrebor
Otrebor

Reputation: 508

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 .

Upvotes: 3

Joey Moto
Joey Moto

Reputation: 49

Do you mean:

int score1, score2, final_score;

Upvotes: 2

Related Questions