The Big Master

Difference between the declaration and definition of a variable

A declaration introduces the existence of a variable to the compiler. It...

A declaration introduces the existence of a variable to the compiler. It informs the compiler about the variable’s name and data type without allocating memory for it.

A definition allocates memory for the variable and assigns it a specific memory address.

Variable name rules in C++:

Variable names in C++ should conform to specific standards and conventions in order to ensure the code’s clarity, readability, and appropriate functionality. The following are the variable naming rules in C++:

  1. Valid Characters:
    • Variable names can contain letters (both uppercase and lowercase), digits, and underscores.
    • The first character of the variable name must be a letter or an underscore.
    • C++ is case-sensitive, so AREAOFCIRCLE, areaofcircle, and AreaOfCircle are considered different variable names.
  2. Reserved Keywords:
    • Variable names cannot be the same as C++ keywords (e.g., int, double, if, while).
    • Keywords have a specific meaning in the language and cannot be used as variable names.
  3. Length:
    • Variable names can be of any length, but it’s recommended to keep them reasonably short and descriptive.
  4. Special Characters not Allowed:
    • Variable names cannot contain special characters like punctuation marks or spaces.
    • Use underscore instead of space to separate characters (e.g., my_variable).
  5. Meaningful Names:
    • Choose meaningful and descriptive names that convey the purpose and content of the variable. (e.g. RollNo, Marks, Grade )

Examples of valid and invalid variables:

Here are 50 examples of valid variable names and 50 examples of invalid variable names with the reasons why they are invalid:

Valid Variable Names:

age

salary

isStudent

fullName

numOfStudents

firstName

lastName

address

PI

totalMarks

num1

num2

counter

averageScore

isValid

isAdminUser

x

y

z

radius

username

password

isActive

isCompleted

totalAmount

isPalindrome

isValidInput

isEven

isOdd

isNegative

productCode

orderDate

startDate

endDate

isRegistered

accountBalance

discountPercentage

numOfItems

maxValue

minValue

studentName

teacherName

companyName

employeeID

customerID

invoiceNumber

phoneNumber

emailAddress

websiteURL

fileContent

Invalid Variable Names (with reasons):

1stStudent (Cannot start with a digit)

full-name (Cannot contain a hyphen)

double (Cannot use the reserved keyword ‘double’)

if (Cannot use the reserved keyword ‘if’)

first name (Contains a space)

@address (Contains a special character)

num@ber (Contains a special character)

*totalMarks (Contains a special character)

average-score (Contains a hyphen)

1num (Cannot start with a digit)

last name (Contains a space)

enum (Cannot use reserved keyword ‘enum’)

default (Cannot use reserved keyword ‘default’)

class (Cannot use reserved keyword ‘class’)

is Active (Contains a space)

int (Cannot use reserved keyword ‘int’)

float (Cannot use reserved keyword ‘float’)

bool (Cannot use reserved keyword ‘bool’)

long (Cannot use reserved keyword ‘long’)

short (Cannot use reserved keyword ‘short’)

signed (Cannot use the reserved keyword ‘signed’)

unsigned (Cannot use the reserved keyword ‘unsigned’)

char (Cannot use reserved keyword ‘char’)

void (Cannot use reserved keyword ‘void’)

auto (Cannot use reserved keyword ‘auto’)

break (Cannot use reserved keyword ‘break’)

const (Cannot use reserved keyword ‘const’)

continue (Cannot use the reserved keyword ‘continue’)

default (Cannot use reserved keyword ‘default’)

goto (Cannot use reserved keyword ‘goto’)

for (Cannot use the reserved keyword ‘for’)

do (Cannot use the reserved keyword ‘do’)

while (Cannot use the reserved keyword ‘while’)

else (Cannot use the reserved keyword ‘else’)

switch (Cannot use reserved keyword ‘switch’)

typedef (Cannot use reserved keyword ‘typedef’)

extern (Cannot use the reserved keyword ‘extern’)

return (Cannot use reserved keyword ‘return’)

volatile (Cannot use the reserved keyword ‘volatile’)

assert (Cannot use the reserved keyword ‘assert’)

goto_home (Contains an underscore)

username@123 (Contains a special character)

123_variable (Cannot start with a digit)

!isActive (Contains a special character)

#PI (Contains a special character)

++counter (Contains a special character)

my variable (Contains a space)

is-Valid (Contains a hyphen)

is*Even (Contains a special character)

1.23 (Contains a special character)

These examples illustrate the rules for valid and invalid variable names in C++. Following these naming rules will help you write clean and error-free code