Skip to study material
← Question index

QUESTION 41 · C fundamentals

State two practical rules for naming identifiers in C.

2 marks

Answer

Use a letter as the first character of a beginner-style name, followed by letters, digits or underscores; do not use a keyword.

  • total_marks is valid; 2marks and total-marks are not valid single identifiers.
  • Names are case-sensitive: total and Total are different. Avoid reserved underscore patterns.

QUESTION 42 · C fundamentals

What are tokens and identifiers? Explain identifier naming rules in C.

5 marks

Answer in brief

Tokens are the small building blocks of C source code. An identifier is a name given to a variable, function or another program item.

Common token types

  1. Keywords: Words with a fixed language meaning, such as int and while. They cannot be used as ordinary variable names.
  2. Identifiers: Names selected by the programmer, such as total and calculate_sum.
  3. Constants: Values written in the source, such as 25 or 3.5.
  4. String literals: Text written in double quotes, such as "Hello".
  5. Operators and punctuation: Symbols such as +, =, ; and braces help form expressions and statements. C calls these symbols punctuators.

Identifier naming rules

  1. First character: For the simple portable names used here, begin with a letter.
  2. Remaining characters: Use letters, digits and underscores. Spaces and hyphens cannot be part of one identifier.
  3. Case matters: total and Total are different names.
  4. Keywords are not allowed: Do not name a variable int or while.
  5. Avoid reserved names: Do not use leading-underscore patterns in beginner application names.

Examples

total_marks and marks2 are valid names. 2marks is invalid because it begins with a digit. total-marks is read as subtraction, not one name. student count contains two separate names rather than one identifier.

Meaningful names

Choose names that explain the data, such as student_count or total_marks. The source int total = 25; contains the keyword int, identifier total, integer constant 25 and symbols = and ;. Do not memorise an old rule that all C names must have at most eight characters; implementations have limits, but useful names can be longer.

Reference tables

↔ Scroll horizontally on a small screen
Tokens in a declaration
Source fragmentCategoryMeaning
intKeywordA built-in type specifier.
totalIdentifierA name chosen for the object.
=Punctuator/operatorAssignment or initialisation notation in context.
25Integer constantA numeric value written in the source.
;PunctuatorEnds the declaration.
↔ Scroll horizontally on a small screen
Check the name
NameAssessment
total_marksValid beginner-style identifier.
marks2Valid; a digit may follow the first character.
2marksInvalid; begins with a digit.
total-marksParsed as subtraction, not one identifier.
whileA keyword; cannot be your variable name.

Key points

  • Keywords and identifiers are different token categories.
  • Uppercase and lowercase letters are distinct.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • March/April 2023 · Q1(a) · 1 marks

    Give an example of an identifier.

    Paper P05 · page 1
View all matching exam references →