MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 1 of 11

Block 1 · Unit 2Data Types, Operators and Expressions11 min · Section 1 of 11

2.1Character Set, Identifiers and Keywords

Covers IGNOU MCS-201 §2.2, 2.3, 2.3.1, 2.3.2

Just as English words are built from letters, C programs are built from a fixed set of characters, which form names and keywords. This section covers the character set, the rules for naming things in C, and the words C reserves for itself.

The C character set

The character set is the collection of characters that may appear in C source code:

GroupCharacters
LettersA–Z and a–z
Digits0–9
Special characters; : , . ' " ( ) [ ] { } < > = + - * / % & | ! ? ~ ^ # \ _
Whitespacespace, tab, newline (separate words; otherwise ignored by the compiler)

Identifiers

Rules for forming an identifier:

  1. It may contain only letters, digits and the underscore _.
  2. The first character must be a letter or an underscore, not a digit.
  3. Upper and lower case are different: total, Total and TOTAL are three different names.
  4. No spaces or other special characters are allowed.
  5. It must not be a keyword.
  6. Compilers must distinguish at least the first 31 characters (63 since C99) of an internal name, so very long names that differ only near the end are risky.
ValidInvalidReason
total2nd_valueStarts with a digit
tax_rateorder-noHyphen not allowed
_counterror flagSpace not allowed
X123"name"Quotes not allowed
student_1intKeyword

Keywords

A keyword is a reserved word with a fixed meaning in C. It cannot be used as an identifier. ANSI C (C89) has 32 keywords, all in lower case:

autodoubleintstruct
breakelselongswitch
caseenumregistertypedef
charexternreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultgotosizeofvolatile
doifstaticwhile

Later standards added more, such as inline, restrict and _Bool (C99).

Key takeaways

  • C source code uses letters, digits, special characters and whitespace.
  • Identifiers: letters, digits and underscore; not starting with a digit; case-sensitive; not a keyword.
  • C89 has 32 lower-case keywords; library function names are not keywords.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which of these is a valid identifier?

Show answer

Answer: C. _net_pay

_net_pay uses only underscores and letters and does not start with a digit. net-pay contains a hyphen, which C reads as minus.

Question 2 · multiple choice

Which of these is a C keyword?

Show answer

Answer: B. volatile

volatile is one of the 32 C89 keywords. printf and main are function names, and include is a preprocessor directive name, not a keyword.

Question 3 · multiple choice

How many keywords does ANSI C (C89) have?

Show answer

Answer: D. 32

C89 defines exactly 32 keywords. Later standards such as C99 added a few more.

Question 4 · multiple choice

A program declares int Count; and later uses count = 5;. What happens?

Show answer

Answer: A. Compilation fails because count is undeclared

C is case-sensitive, so count and Count are different names and count was never declared. Ignoring case is a feature of some other languages, not C.

Question 5 · multiple choice

Which name is invalid?

Show answer

Answer: C. float

float is a keyword. function looks like a special word but is not reserved in C.

Question 6 · short answer

Explain why max value, max-value and 2max are invalid identifiers, and correct each one.

Show worked answer

max value contains a space, which separates words, so C sees two names; correct it to max_value. max-value contains a hyphen, which C reads as the subtraction operator (max minus value); correct it to max_value. 2max starts with a digit, which is not allowed for the first character; correct it to max2 or max_2.

↑ ↓ move↵ openTab switch type