✏️ Explanatory Question

Invalid Variable Names and Reasons

👁 0 Views
📘 Detailed Answer
🟢 Easy
0
Total Views
10
Related Qs
0%
Progress
💡

Answer with Explanation

Invalid Variable Names and Reasons

Question

Which of the following are invalid variable names and why?

BASICSALARY _basic basic-hra
#MEAN group. 422
population in 2006 over time mindovermatter
FLOAT hELLO queue.
team’svictory Plot # 3 2015_DDay

Rules for Naming Variables

  • A variable name can contain letters, digits, and underscore (_).
  • A variable name cannot contain spaces.
  • A variable name cannot start with a digit.
  • Special characters like #, -, ., ', @ are not allowed.

Solution

Variable Name Status Reason
BASICSALARY Valid Contains only letters.
#MEAN Invalid # symbol is not allowed.
population in 2006 Invalid Contains spaces.
FLOAT Valid Contains only letters.
team’svictory Invalid Apostrophe symbol is not allowed.
_basic Valid Underscore at beginning is allowed.
group. Invalid Dot (.) is not allowed.
over time Invalid Contains spaces.
hELLO Valid Contains only letters.
Plot # 3 Invalid Contains space and # symbol.
basic-hra Invalid Hyphen (-) is not allowed.
422 Invalid Variable name cannot start with a digit.
mindovermatter Valid Contains only letters.
queue. Invalid Dot (.) is not allowed.
2015_DDay Invalid Starts with a digit.

Example Program in C

#include 

int main()
{
    /* Valid Variable Names */

    int BASICSALARY = 50000;
    int _basic = 1000;
    int FLOAT = 20;
    int hELLO = 5;
    int mindovermatter = 1;

    printf("BASICSALARY = %d\n", BASICSALARY);
    printf("_basic = %d\n", _basic);
    printf("FLOAT = %d\n", FLOAT);
    printf("hELLO = %d\n", hELLO);
    printf("mindovermatter = %d\n", mindovermatter);

    /* Invalid Variable Names */

    /*
    int #MEAN = 10;
    int population in 2006 = 50;
    int basic-hra = 200;
    int 422 = 15;
    int queue. = 5;
    int 2015_DDay = 100;
    */

    return 0;
}

Program Output

BASICSALARY = 50000
_basic = 1000
FLOAT = 20
hELLO = 5
mindovermatter = 1
Important Note:
Invalid variable names are written inside comments because they generate compiler errors if used directly in the program.