Monday, 12 December 2016

C Programming Keywords and Identifiers


In this tutorial, you will learn about keywords; reserved words in C programming that are part of the syntax. Also, you will learn about identifiers and proper way to name a variable.


Character set

In C language characters are grouped into the following categories,
  1. Letters(all alphabets, a to z & A to Z).
  2. Digits (all digits 0 to 9).
  3. Special characters  such as (:(colon) ;(semicolon) .(period) _(underscore) &(ampersand) etc ).
  4. White spaces (blank space, new line, horizontal tab, etc).

Keywords:-

Keywords are preserved words that have special meaning in C language. The meaning has already been described. These meaning cannot be changed. There are total 32 keywords in C language.


int number;
Here, int is a keyword that indicates 'number' is a variable of type integer. 
As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.

Keywords in C Language
autodoubleintstruct
breakelselongswitch
caseenumregister typedef
charexternreturnunion
continueforsignedvoid
doifstatic while
defaultgotosizeofvolatile
constfloatshortunsigned

Along with these keywords, C supports other numerous keywords depending upon the compiler. We will study these keywords in details further in my next post Data Types in C.

Identifiers:-

Identifier refers to name given to entities such as variables, functions, structures etc.

Identifier must be unique. They are created to give unique name to a entity to identify it during the execution of the program. For example:

int money;
double accountBalance;

Here, money and accountBalance are identifiers.

Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.

Rules for an Identifier

  1. An Indetifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and underscore( _ ).
  2. The first character of an identifier can only contain alphabet( a-z , A-Z ) or underscore ( _ ).
  3. Identifiers are also case sensitive in C. For example name and Name are two different identifier in C.
  4. Keywords are not allowed to be used as Identifiers.
  5. No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as Identifier.

Good Programming Practice:-

You can choose any name for an identifier (excluding keywords). However, if you give meaningful name to an identifier, it will be easy to understand and work on for you and your fellow programmers.

No comments:

Post a Comment