Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - Constants and Identifiers

Page 1 of 5
Author: Exforsys Inc.     Published on: 2nd Mar 2006    |   Last Updated on: 6th Jul 2011

This tutorial will cover constants and identifiers in C. Constants, as the name implies, are values that never change. In the previous tutorial on data types you have seen how a variable can be declared constant by making use of the const keyword. You can also declare a constant by directly entering its value in the source code. For example, in this code:

Sample Code
  1. double pi = 3.14159;
  2. char c = 'A';
  3. char hello[] = "Hello World!";
Copyright exforsys.com


The number 3.14159, the letter A and the string Hello World! are all constants. You have already seen these types of constants used in the examples of previous tutorials. In this tutorial we will go into more detail and cover how to declare different types of constants and the rules that apply to those constants.

Ads

In addition we will look at the rules that are applicable for identifier names. An identifier is a name of a variable, function, structure, union, typedef, etc. - basically anything that can be given a name. Now, let us start with characters.

Character Constants

A character is a single letter from an alphabet. C defines two basic alphabets; one in which the source code is written and the one which is used when the program is run. They are usually the same, but they do not have to be. If they are different, it is the compiler's job to translate character constants from the source code alphabet to the runtime alphabet.

The basic C alphabet contains the following characters:

  • upper and lower case A-Z,
  • decimal digits 0-9,
  • the space,
  • horizontal tab,
  • vertical tab,
  • newline,
  • backspace,
  • carriage return,
  • alert and form feed characters

and the following symbol characters:

    ! " # % & ' ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~

Each character can be represented by a number. The numbers representing the digits 0-9 are 10 continuous integers. If for example the digit '0' was represented by the number 100, the '1' would be 101, '2' would be 102 and so on.

Note that the C standard itself does not give the actual mappings from the characters to the numbers representing them. In practice most implementations will use the ASCII standard to map characters to numbers and back; some systems from the IBM mainframe and minicomputer world use the EBCDIC standard.

The characters discussed so far are the basic characters of the alphabets. The basic characters are guaranteed to fit inside a char type. In addition to the basic characters there are extended characters that are locale specific (they are language, culture and nationality specific).

Each implementation will define what extended characters are supported. These extended characters may not fit inside a single char. There is a "wide character" type called wchar_t defined in stddef.h that is used to hold these characters which may potentially be more than one byte.

Using Character Constants

There are numerous ways to indicate a character constant in your source code. The simplest way is the way you have seen before in previous examples – to put single quotes around the character, like this:

Sample Code
  1. const char upper_h = 'H';
  2.    if ( c == 'Y' || c == 'y' )
  3.       return;
Copyright exforsys.com


A character constant inside single quotes actually has the type int, but if assigned or cast to a char the value will not overflow the char.

Another way to enter a character constant is to specify the number that represents the character you want. The way to do this involves using an "escape sequence" to specify the character. Escape sequences allow you to designate a character in character constants and string constants using a backslash (the '\' character) followed by some letters or numbers, all inside single quotes (or double quotes for string constants).

C allows you to specify a character's number using octal or hexadecimal numbers. For octal, you use a backslash followed by one, two or three digits between 0 and 7 inside single quotes. For hexadecimal use a backslash followed by a lowercase 'x' and then one or more digits between 0 and 9 and letters between 'a' and 'f' inside single quotes.

For example, in the ASCII character set a capital M is assigned the number 77 which is 115 in octal or 4D in hexadecimal. You can enter a capital M using this syntax:

Sample Code
  1. char a1 = '\115'; // octal
  2. char a2 = '\x4d'; // hexadecimal
  3. char a3 = '\x4D'; // hexadecimal
Copyright exforsys.com


The last two show that when specifying hexadecimal digits, both upper and lower case are allowed.

If you want to use one of the extended characters you need to prefix the single quote with the letter 'L' to indicate it is a large character. An extended character may not fit inside a regular char but you can hold it in a wchar_t. Of course a wchar_t can hold all the basic characters too.

You can enter extended characters in the same way as the basic characters. Let us see an example with the character Ă which is represented by the two-byte hexadecimal number 0x0102 (which is 258 in decimal and 402 in octal):

Sample Code
  1.    #include <stdio.h>
  2.    #include <stddef.h>
  3.    #include <wchar.h>
  4.    #include <locale.h>
  5.    
  6.    void main()
  7.    {
  8.            wchar_t a1 = L'Ă';
  9.            wchar_t a2 = L'\x102';
  10.            wchar_t a3 = L'\402';
  11.  
  12.           setlocale( LC_ALL, "" );
  13.  
  14.           printf("a1 = %lc\n", a1);
  15.           printf("a2 = %lc\n", a2);
  16.           printf("a3 = %lc\n", a3);
  17.   }
Copyright exforsys.com


Ads

The output of this program is:

Sample Code
  1. a1 = 'Ă'
  2. a2 = 'Ă'
  3. a3 = 'Ă'
Copyright exforsys.com


As you can see all the characters are the same, just specified in different ways. The setlocale() in line 12 is needed to initialize C's locale information. Without it, a C program can only handle the basic characters. Also note that the printf() format specifier for a wide character is %lc instead of %c.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments