How2Lab Logo
tech guide & how tos..


Elements of the C Language - Identifiers, Keywords, Data types and Data objects


This article deals with basic elements, which are used to create a C program. These elements are - the valid character set, identifiers, keywords, basic data types and their representation, constants and variables.


The C Character Set

C uses the uppercase English alphabets A to Z, the lowercase letters a to z, the digits 0 to 9, and certain special characters as building blocks to form basic program elements viz. constants, variables, operators, expressions and statements.

The special characters are listed below:

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

Most versions of the language also allow some other characters, such as @ and $, to be included within strings and comments.

In addition certain combinations of these characters, such as '\b', '\n' and '\t', are used to represent special condition such as backspace, newline and horizontal tab, respectively. These character combinations are known as escape sequences.


Identifiers and Keywords

Identifiers are names given to various items in the program, such as variables, functions and arrays. An identifier consists of letters and digits, in any order, except that the first character must be a letter. Both upper and lowercase letters are permitted. Upper and lowercase letters are however not interchangeable (i.e., an uppercase letter is not equivalent to the corresponding lowercase letter). The underscore character (_) can also be included, and it is treated as a letter. Keywords like if, else, int, float, etc., have special meaning and they cannot be used as identifier names.

The following are examples of valid identifier names: A, ab123, velocity, stud_name, circumference, Average, TOTAL

The following names are not valid identifiers:

1st - the first character must be a letter
"Jamshedpur" - illegal characters (")
stud-name - illegal character (-)
stud name - illegal character (blank space)

Although an identifier can be arbitrarily long, most implementations recognize typically 31 characters. There are some implementations, which recognize only eight characters. The ANSI standard recognizes 31 characters. If a system recognizes only 8 characters and if you use a variable name with more than 8 characters, only the first 8 characters will be taken, the rest will be ignored. Thus, average_of_numbers and average_ will both be recognized as average_. It is therefore safer to try and use short meaningful names for your identifiers.


Exercise:

Identify which of the following are valid identifiers.

(a)stud 1(d)switch(g) Average of Numbers(j) 123-45-678
(b)1stud(e)%calc(h) Average_of_Numbers
(c)stud_1(f)_x(i) Average-of-Numbers

Data Types and Sizes

There are only few basic data types in C. These are listed in the table below:

Data typeDescriptionSizeRange
charsingle character1 byte0 - 255
intinteger number4 bytes-2147483648 to 2147483647
floatsingle precision floating point number (number containing fraction & or an exponent)4 bytes3.4E-38 to 3.4E+38
doubledouble precision floating point number8 bytes1.7E-308 to 1.7E+308

The list of data types can be increased by using the data type qualifiers such as - short, long, and unsigned. For example, an integer quantity can be defined as long, short, signed or unsigned integer. The memory requirement of an integer data varies depending on the compilers used. The qualified basic data types and their sizes are shown in table below.

Data typeSizeRange
short int2 bytes-32768 to 32767
long int4 bytes-2147483648 to 2147483647
unsigned short int2 bytes0 to 65535
unsigned int4 bytes0 to 4294967295
unsigned long int4 bytes0 to 4294967295
long double (extended precision)8 bytes1.7E-308 to1.7E+308

Note that the qualifier unsigned can be used along with the qualifiers short and long. The unsigned int occupies the same memory space as an ordinary int but differs on the possible content of the left-most bit. In case of an ordinary int it is reserved for sign (sign bit), but all the bits in an unsigned int are used for determining the magnitude of the integer quantity.

The char type is used to represent individual characters, and occupies one byte of memory. Each character has an equivalent integer representation (since each stores internally the ASCII value for the corresponding character). So char variables or constants can be used as integer data in arithmetic expressions.

The data objects to be manipulated in a C program are classified as variables and constants. The type of all the variables to be used in the program must be declared before they can be used. The operations that can be performed on the data objects are specified by a set of operators. Expressions used in a program combine the variables, constants and operators to produce new values.


Constants

The constants in C can be classified into four categories namely integer constants, floating point constants, character constants and string constants.

A character constant is written as for example - 'A' (always enclosed in single quotes).

Examples of string constants are - "Jamshedpur", "A", etc. Note that a string constant is always enclosed within double quotes.

A normal integer constant is written as 1234.

A long int is recognized by the presence of L (uppercase or lowercase) at the end of the constant, e.g. 2748723L.

The suffix u or U signifies the int to be an unsigned one.

The UL or ul at the end indicates the int quantity is of unsigned long type

Floating point constants contain a decimal point (167.903) or an exponent (1e-2) or both. Their type is double unless suffixed. The suffix of f or F indicates float; 1 or L indicates long double.

C also supports octal and hexadecimal data. The value of an integer data can be specified in either octal or hexadecimal form. A hexadecimal constant must begin with 0x or 0X, a leading 0 indicates the octal representation. Octal and hexadecimal constants may also be followed by U to indicate unsigned or L to determine long.

The number 0x2A5 is an example of a hexadecimal number. Internally the number is represented by the following bit patterns,

0x2A5 = 0010 1010 0101 = 2 * 162 + 10 * 161 + 5 * 160 = 677
        ---- ---- ----
          2    A   5

The number 677 is the decimal equivalent of the number 0x2A5.


Example of an octal number can be 0347. To represent each digit of an octal number in the form of binary, we need maximum of three bits since the last digit in the octal number system is 7.

0347 = 011 100 111 = 3 * 82 + 4 * 81 + 7 * 80 = 231 (in decimal)
       --- --- ---
        3   4   7

In numeric constants e.g. integer or floating point constants, blanks and any non-numeric characters cannot be included. The range of these constants will be limited by the maximum and minimum bounds (usually machine dependent).

A character constant is a single character enclosed in apostrophes such as 'A'. Such a constant is internally treated as an integer e.g. 'A' corresponds to the integer 65 (also known as its ASCII value). The ASCII value of 'a' (small) is 97. Hence character constants can participate in a numeric calculation just like any other integer, moreover, they also can be used for comparing with other character constants. Some character constants are of non-printing type which can be expressed by a combination of a back-slash (\) and a character. They are known as escape sequences. Each of them represents a single character even though they are written in terms of two or more characters.

Commonly used escape sequences are listed below:

CharacterEscape SequenceASCII Value
Bell\a007
Backspace\b008
Null\0000
Newline\n010
Carriage return\r013
Vertical tab\v011
Horizontal tab\t009
Form feed\f012

Exercises:

Identify which of the following numerical values are valid constants. If a constant is valid, specify whether it is integer or real. Also, specify the base for each valid integer constant.

(a)0.7b)39,634c)16.3e18d)16.3e-18
(e)123456789f)123456789lg)0.3E+0.4h)0.3E4
(i)0412j)018ACFk)0xABCDEl)0x97e334

Which of the following are valid character constants?

(a)'a'(b)'/n'(c)'F'(d)'\0492'
(e)'x'(f)'\\'(g)'\0'(h)'\n'
(i)'\b'(j)'\x-y-'

Which of the following are valid string constants?

(a)'9:25 A.M.'
(b)"Blue, Green and Indigo"
(c)"Name:
(d)"Section 4 (Next \'d')"
(e)"1.6e-18"
(f)"JAMSHEDPUR BR 831 001"
(g)"The Station master announced, "Down Geetanjali express is running late"

Symbolic Constants

Constants which play crucial roles in a program can be made more meaningful by assigning them appropriate names to make the program more readable and easily changeable. These constants are called symbolic constants and are defined as follows.

Examples:

#define PI 3.141593

#define TRUE 1

#define PROMPT "Enter Your Name :"

PI, TRUE and PROMPT are symbolic constants, so they do not appear in the declaration. #define is a preprocessor directive like #include. The salient feature of a symbolic constant is that the value assigned to a symbolic constant cannot be changed (unlike variables) subsequently in the program.

Exercise:

Write an appropriate definition for each of the following symbolic constants, as it would appear within a C program:

ConstantText
(a)AMOUNT-75
(b)EPSILON0.00001
(c)BIGIN{
END}
(d)FIRSTNAME"Sanjay"
(e)ENDOLIN'\n'
(f)COST"Rs. 125.75"

Variable Declaration in C

In a C program all variables must be declared before they are used. A declaration determines the type of the data, and contains a list of one or more variables having the same type.

Example:
intcount, index;
charflag, text[80];
short inta,b;
unsigned intp;
doubled;

A variable can be initialized with values at the time of declaration.

Example:
intc = 5;
charreply = 'Y';
doubled = 4.64386237445675;
charstate[] = "ANDHRA PRADESH";
floateps = 1.0e-5;

Exercises:

Write appropriate declaration for each group of variables and character array (strings):

(a)Integer variables: x, y
(b)Integer variable: count
Unsigned integer variable: employee no
Double-precision variables: net sales, tax, profit
(c)Character variables: first name, last name
(d)70 element character array: message

Write appropriate declaration and assign the given initial values to each group of variables and array:

(a)Floating-point variables: x = -9.5, y = 1.0004
Integer variables: a = 734, b = 49, c = -63
Character variables: c1 = 'a', c2 = '$'
(b)Double-precision variable: d1 = 3.94 * 10-12, d2 = -9.89 * 107
Integer variable: i = 437 (octal), h = 6AFF (hexadecimal)
(c)Long integer variable: large = 123456789
Double-precision variables: d = 0.6666666
Character variable: eol = newline character
(d)One-dimensional character array: message = "OVERFLOW"

LAB WORK

Enter compile and run the following sample programs in C and try to understand how 
Variables are declared, initialized and manipulated. 
(Comment entries are given for your Understanding and need not to be typed).

a. Watch out for the compilation errors,
	#include <stdio.h>
	main()
	{
		int a number = 123;
		double 2d;
		character my_name = 'I';
		char name = "satyajit";
	}

b.	#include<stdio.h>
	main()
	{
		/* declaring two char type variable and assigning initial values */
		char cap_alpha = 'A', low_alpha = 'B';

		/*display the content of the variables on the screen */
		printf("%c %c\n",cap_alpha,low_alpha);

		/* adding one to internal integer value of the two char type variable*/
		cap_alpha = cap_alpha + 1;
		low_alpha = low_alpha + 1;
		printf("%c %c\n", cap_alpha,low_alpha);
		cap_alpha = low_alpha + 32;
		low_alpha = low_alpha – 32;
		printf("%c %c\n",cap_alpha,low_alpha);
	}

c.	#include <stdio.h>
	#define PI  	3.141593
	#define	RADIUS	10
	main()
	{
		float area = 0.00000000;
		area = 2 * PI * RADIUS * RADIUS;
		printf("\nArea of the circle: %f", area);
	}

d.	#include <stdio.h>
	main()
	{
		int num, NUM;
		num = 1000;
		NUM = 2000;
		printf("%d %d\n", NUM, num);
	}



Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide | Hostinger
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.

Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.