Identifiers are the names given to various program elements such as constants, variables, function names and arrays etc. Every element in the program has its own distinct name but one cannot select any name unless it conforms to valid name in C language. Let us study first the rules to define names or identifiers.
Rules for Forming Identifiers
Identifiers are defined according to the following rules:
For example, some valid identifiers are shown below:
X
X123
_XI
temp
tax_rate
For example, some invalid identifiers are shown below:
123 First character to be alphabet.
“X.” Not allowed.
order-no Hyphen allowed
error flag Blankspace allowed
Keywords
Keywords are reserved words which have standard, predefined meaning in C. They cannot be used as program-defined identifiers.
The lists of C keywords are as follows:
char, while, do, typedef, auto, int, if, else, switch, case, printf, double, struct, break, static, long, enum, register, extern, return, union, const, float, short, unsigned, continue, for, signed, void, default, goto, sizeof, volatile .
Note: Generally all keywords are in lower case although uppercase of same names
can be used as identifiers
When you write a program, you express C source files as text lines containing characters from the character set. When a program executes in the target environment, it uses characters from the character set. These character sets are related, but need not have the same encoding or all the same members.
Every character set contains a distinct code value for each character in the basic C character set. A character set can also contain additional characters with other code values. The C language character set has alphabets, numbers, and special characters as shown below:
Identifiers are the names given to various program elements such as constants, variables, function names and arrays etc. Every element in the program has its own distinct name but one cannot select any name unless it conforms to valid name in C language. Let us study first the rules to define names or identifiers.
Rules for Forming Identifiers
Identifiers are defined according to the following rules:
For example, some valid identifiers are shown below:
X
X123
_XI
temp
tax_rate
For example, some invalid identifiers are shown below:
123 First character to be alphabet.
“X.” Not allowed.
order-no Hyphen allowed
error flag Blankspace allowed
Keywords
Keywords are reserved words which have standard, predefined meaning in C. They cannot be used as program-defined identifiers.
The lists of C keywords are as follows:
char, while, do, typedef, auto, int, if, else, switch, case, printf, double, struct, break, static, long, enum, register, extern, return, union, const, float, short, unsigned, continue, for, signed, void, default, goto, sizeof, volatile .
Note: Generally all keywords are in lower case although uppercase of same names
can be used as identifiers
To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of data, which may be represented differently within the computer memory. The data type specifies two things:
C Language provides four basic data types viz. int, char, float and double. Using these, we can store data in simple ways as single elements or we can group them together and use different ways (to be discussed later) to store them as per requirement.
Data Type | Description | Size | Range |
---|---|---|---|
int | Integer | 2 Bytes | -32,768 to 32,767 |
char | Character | 1 Byte | -128 to 127 |
float | Floating Point Number | 4 Bytes | 3.4e-38 to 3.4e+38 |
double | Higher Precision Float | 8 Bytes | 1.7e-308 to 1.7e+308 |
Short, long, signed, unsigned are called the data type qualifiers and can be used with any data type. A short int requires less space than int and long int may require more space than int. If int and short int takes 2 bytes, then long int takes 4 bytes.
Unsigned bits use all bits for magnitude; therefore, this type of number can be larger. For example signed int ranges from –32768 to +32767 and unsigned int ranges from 0 to 65,535. Similarly, char data type of data is used to store a character. It requires 1 byte. Signed char values range from –128 to 127 and unsigned char value range from 0 to 255. These can be summarized as follows:
Data Type | Size (bytes) | Range |
---|---|---|
Short int | 2 | -32,768 to 32,767 |
int | 2 | -32,768 to 32,767 |
Signed int | 2 | -32,768 to 32,767 |
Unsigned int | 2 | 0 to 65,535 |
Long int | 4 | -2,147,483,648 to 2,147,483,647 |
Signed char | 1 | -128 to 127 |
Unsigned char | 1 | 0 to 255 |
Variable is an identifier whose value changes from time to time during execution. It is a named data storage location in your computer’s memory. By using a variable’s name in your program, you are, in effect, referring to the data stored there. A variable represents a single data item i.e. a numeric quantity or a character constant or a string constant. Note that a value must be assigned to the variables at some point of time in the program which is termed as assignment statement. The variable can then be accessed later in the program. If the variable is accessed before it is assigned a value, it may give garbage value. The data type of a variable doesn’t change whereas the value assigned to can change. All variables have three essential attributes:
For example, in the following C program a, b, c, d are the variables but variable e is not declared and is used before declaration. After compiling the source code and look what gives?
main() {
int a, b, c;
char d;
a = 3;
b = 5;
c = a + b;
d = 'a';
e = d;
..........................
..........................
}
After compiling the code, this will generate the message that variable e not defined.
Before any data can be stored in the memory, we must assign a name to these locations of memory. For this we make declarations. Declaration associates a group of identifiers with a specific data type. All of them need to be declared before they appear in program statements, else accessing the variables results in junk values or a diagnostic error. The syntax for declaring variables is as follows:
data- type variable-name(s);
For example,
int a;
short int a, b;
When variables are declared initial, values can be assigned to them in two ways:
A constant is an identifier whose value can not be changed throughout the execution of a program whereas the variable value keeps on changing. In C there are four basic types of constants. They are:
Integer and Floating Point constants are numeric constants and represent numbers.
Rules to form Integer and Floating Point Constants
Integer Constants
Further, these constant can be classified according to the base of the numbers as:
Maximum values these constants can have are as follows:
Integer Constant Type | Maximum Value |
---|---|
Decimal | 32767 |
Octal | 32767 |
Hexadecimal | 32767 |
Unsigned interger constants: Exceed the ordinary integer by magnitude of 2, they are not negative. A character U or u is prefixed to number to make it unsigned.
Long Integer constants: These are used to exceed the magnitude of ordinary integers and are appended by L.
For example,
50000U decimal unsigned.
1234567889L decimal long.
Floating Point Constants
What is a base 10 number containing decimal point or an exponent. Examples of valid floating point numbers are:
0., 1., 000.2, 5.61123456 etc
Examples of Invalid Floating Point numbers are:
1 decimal or exponent required.
1,00.0 comma not allowed
A Floating Point number taking the value of 5 x 104 can be represented as:
5000. 5e4
5e+4 5E4
5.0e+4 .5e5
The magnitude of floating point numbers range from 3.4E –38 to a maximum of 3.4E+38, through 0.0. They are taken as double precision numbers. Floating Point constants occupy 2 words = 8 bytes.
Character Constants
This constant is a single character enclosed in apostrophes ‘ ’ . For example, some of the character constants are shown below:
‘A’, ‘x’, ‘3’, ‘$’
‘\0’ is a null character having value zero.
Character constants have integer values associated depending on the character set adopted for the computer. ASCII character set is in use which uses 7-bit code with 27 = 128 different characters. The digits 0-9 are having ASCII value of 48-56 and ‘A’ have ASCII value from 65 and ‘a’ having value 97 are sequentially ordered. For example,
‘A’ has 65, blank has 32
ESCAPE SEQUENCE
There are some non-printable characters that can be printed by preceding them with ‘\’ backslash character. Within character constants and string literals, you can write a variety of escape sequences. Each escape sequence determines the code value for a single character. You can use escape sequences to represent character codes:
The following is the list of the escape sequences:
Character | Escape Sequence |
---|---|
" | " |
' | ' |
? | ? |
\ | \ |
BEL | \a |
BS | \b |
FF | \f |
NL | \n |
CR | \r |
HT | \t |
VT | \v |
String Constants
It consists of sequence of characters enclosed within double quotes. For example,
“ red ” “ Blue Sea ” “ 41213*(I+3) ”.
Symbolic Constant is a name that substitutes for a sequence of characters or a numeric constant, a character constant or a string constant. When program is compiled each occurrence of a symbolic constant is replaced by its corresponding character sequence. The syntax is as follows:
#define name text
[where name implies symbolic name in caps.
text implies value or the text. ]
For example,
#define printf print
#define MAX 100
#define TRUE 1
#define FALSE 0
#define SIZE 10
The # character is used for preprocessor commands. A preprocessor is a system program, which comes into action prior to Compiler, and it replaces the replacement text by the actual text. This will allow correct use of the statement printf.
Advantages of using Symbolic Constants are:
John Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
ReplyJohn Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Reply