Chapter-2
Chapter-2
Chapter 2
By Karna Bahadur Shrestha
Special Characters
Both uppercase and lowercase letters are permitted. The underscore character is also
permitted in identifiers.
The identifiers must conform to the following rules.
Constants/Literals
A constant value is the one which does not change during the execution of a program. C supports
several types of constants.
1. Integer Constants
2. Real Constants
3. Single Character Constants
4. String Constants
O26
O
O347
O676
Hexadecimal integer: constant is preceded by OX or Ox, they may contain alphabets from A to F
or a to f. The alphabets A to F refer to 10 to 15 in decimal digits. Examples of valid hexadecimal
integers are:
OX2
OX8C
OXbcd
Ox
Real Numbers can also be represented by exponential notation. The general form for
exponential notation is mantissa exponent. The mantissa is either a real number expressed in
decimal notation or an integer. The exponent is an integer number with an optional plus or
minus sign.
+3.2e5 [+3.2x105]
4.1e8 [4.1x108]
A Single Character constant represent a single character which is enclosed in a pair of quotation
symbols.
Example for character constants are:
'5'
'x'
';'
''
All character constants have an equivalent integer value which is called ASCII Values.
Constant Meaning
'\a' Audible Alert (Bell)
'\b' Backspace
'\f' Formfeed
'\n' New Line
'\r' Carriage Return
'\t' Horizontal tab
'\v' Vertical Tab
'\'' Single Quote
'\"' Double Quote
'\?' Question Mark
'\\' Back Slash
'\0' Null
Variables
A variable is a value that can change any time. It is a memory location used to store a data
value. A variable name should be carefully chosen by the programmer so that its use is reflected
in a useful way in the entire program. Variable names are case sensitive. Examples of variable
names are
Sun
number
Salary
Emp_name
average1
Data Types in C
A C language programmer has to tell the system before-hand, the type of numbers or characters
he is using in his program. These are data types. There are many data types in C language. A
C programmer has to use appropriate data type as per his requirement in the program he is
going to do.
There are basic three categories:
o Primary Data types
o User defined Data types
o Derived Data types
1. Integer int
2. Character char
3. Floating Point float
4. Double precision floating point double
5. Void void
Integer Type
Integers are whole numbers with a machine dependent range of values. A good programming
language as to support the programmer by giving a control on a range of numbers and storage
space. C has 3 classes of integer storage namely short int, int and long int. All of these data types
have signed and unsigned forms. A short int requires half the space than normal integer values.
Unsigned numbers are always positive and consume all the bits for the magnitude of the number.
The long and unsigned integers are used to declare a longer range of values.
Void Type
Using void data type, we can specify the type of a function. It is a good practice to avoid
functions that does not return any values to the calling function.
Character Type
A single character can be defined as a defined as a character type of data. Characters are usually
stored in 8 bits of internal storage. The qualifier signed or unsigned can be explicitly applied
to char. While unsigned characters have values between 0 and 255, signed characters have
values from –128 to 127.
Declaration of Variables
Every variable used in the program should be declared to the compiler. The declaration does two
things.
Example:
int sum;
int number, salary;
double average, mean;
Signed Short Integer signed short int (or) short int (or) short
Signed Long Integer signed long int (or) long int (or) long
Here type represents existing data type and „identifier‟ refers to the „row‟ name given to the
data type.
Derived datatypes
There are some datatypes which are derived from the existing primary data types.
struct st{
int a;
float b;
char c;
}
Delimeters
A delimiter is one or more characters that separate text strings. Common delimiters are commas
(,), semicolon (;), quotes ( ", ' ), braces ({}), pipes (|), or slashes ( / \ ).
Expressions
An expression in C is defined as 2 or more operands are connected by one operator and which
can also be said to a formula to perform any operation. An operand is a function reference, an
array element, a variable, or any constant. An operator is symbols like “+”, “-“, “/”, “*” etc.
Let’s observe this example:
A*B
In the above expression multiplication symbol (*) is said to be an operator and A and B are said
to be 2 operands.
Comments
Comments in C language are used to provide information about lines of code. It is widely used
for documenting code. There are 2 types of comments in the C language.
Single line comments are represented by double slash //. Let's see an example of a single line
comment in C.
/*
code
to be commented
*/
C tokens
The basic elements recognized by the C compiler are the tokens. A token in source program text
that the compiler does not breakdown into component elements. The keywords, identifiers,
Tokens in C is the most important element to be used in creating a program in C. We can define the token as the
smallest individual element in C. For `example, we cannot create a sentence without using words; similarly, we
cannot create a program in C without using tokens in C. Therefore, we can say that tokens in C is the building
block or the basic component for creating a program in C language.
Classification of tokens in C
https://www.javatpoint.com/tokens-in-c