ctokens_chapter2
ctokens_chapter2
Chapter 2
1. Character set
2. Keywords
3. Identifiers
4. Constants
5. Operators
Character set:
Character set consists of
i) alphabet from A-Z or a-z
ii) digits from 0-9
iii) Special characters like (,), {,}, [,], <, >, &, $, #, %, ^, !, ?, :, ;, ",', .
iv) White space character: blank space
v) Escape sequences:
\b backspace
\a audible bell
\v vertical tab
\t horizontal tab
\f form feed
\r carriage return
\” double quotes
\’ single quotes
\\ back slash
#include<stdio.h>
main()
{
printf("Backspace Character\b");
printf(" Audible Bell\a\n");
printf("Vertical Tab\vDemonstration\n");
printf("Horizental Tab\tDefault 8 spaces\n");
printf("Form feed\fIt is only with Printer\n");
printf("Carriage Return\rmove the cursor at the start of the line\n");
printf("Single Quotes The \"Sun\" rises in the \'east\'\n");
printf("Backslash \\hai\n");
}
Output:
Keywords:
Keywords are reserved words. All keywords have fixed meanings. Keywords
serve as basic building blocks for program statements. In ‘C’, there are 32
keywords. All keywords must be written in lower case.
Identifiers: Identifier refers to the names of the variables, functions and arrays.
Constants:
Constants refer to fixed values that do not change during the execution of a
program.
Integer constants:
Real constants:
Example Program:
#include<stdio.h>
main()
{
char ch='a';
printf("ch=%c\n",ch);
printf("ASCII value of \'%c\'=%d\n",ch,ch);
}
Output:
ch=a
ASCII value of 'a'=97
Output:
ch=a
ASCII value of c=99
Variable is a data name which can be used to store a data and a variable may
take different values at different times during execution.
Declaration of variables:
datatype v1,v2,…,vn
Initialization of variables:
Assign a value to a variable at the time of the variable is declared.
datatype variable_name=constant;
or
Assignment of an initial value to a variable is also called initialization.
variable_name=constant;
Data type specifies which type of data that we are storing in a variable. There
are 3 types of data types.
i. Integer
ii. Floating point
iii. Character
iv. Double precision floating point
Integer:
'C' provides three different classes of integers.
They are
a) int
b) short int
c) long int
The difference between these 3 integers is the number of bytes to occupy the
range of values.
Formula to find number of bits occupied by data type when number of bytes is
known:
-2n-1 to 2n-1 - 1
Floating point or real numbers are stored in 32 bits (on all 16 bit and 32 bit
machines) with 6 bits precision.
3 classes of floating point types:
i. float
ii. double
iii. long double
Character types:
A single character can be defined as a character type data.
#include<stdio.h>
main()
{
printf("signed char size=%d\n",sizeof(char));
printf("signed short int size=%d\n",sizeof(short int));
printf("signed int size=%d\n",sizeof(int));
printf("signed long int size=%d\n",sizeof(long int));
printf("unsigned char size=%d\n",sizeof(unsigned char));
printf("unsigned int size=%d\n",sizeof(unsigned int));
General form:
where type is any existing data type, identifier is a new name given to
the data type.
Examples:
typedef int units;
typedef float marks;
units u1,u2;
marks m1, m2;
Example program:
#include<stdio.h>
int main()
{
typedef int units;
typedef float marks;
units a,b;
marks x,y;
printf("Enter 2 values\n");
scanf("%d%d",&a,&b);
printf("Enter 2 values\n");
scanf("%f%f",&x,&y);
printf("a=%d\tb=%d\n",a,b);
printf("x=%f\ty=%f\n",x,y);
}
Output:
Enter 2 values
5
6
Enter 2 values
1.9
6.7
a=5 b=6
x=1.900000 y=6.700000
enum identifier v1,v2,…,vn can only have one of the values value1, value2, …,
valuen.
Example:
Example program:
#include<stdio.h>
main()
{
enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
Sunday};
enum day weekst,weekend;
weekst=Monday;
weekend=Thursday;
printf("weekst=%d\nweekend=%d\n",weekst,weekend);
}
Output:
weekst=0
weekend=3
#include<stdio.h>
main()
{
enum day { Monday, Tuesday=5, Wednesday,Thursday, Friday, Saturday}
weekst,weekend;
weekst=Tuesday;
weekend=Thursday;
printf("weekst=%d\nweekend=%d\n",weekst,weekend);
}
Type qualifiers:
This tells the compiler that the value of the int variable classsize must not be
modified by the program.
Example program:
1. #include<stdio.h>
2. main()
3. {
4. const int classsize=60;
5. printf("classsize=%d\n",classsize);
6. classsize=classsize*3;
7. printf("classsize=%d\n",classsize);
8. }
Note: The compiler gives an error at line number 6: assignment of read-only
variable 'a'.
volatile:
ANSI standard defines qualifier volatile that could be used tell explicitly the
compiler that a variable’s value may be changed at any time by some external
sources (from outside the program).
Example:
volatile int a;
Example program:
#include<stdio.h>
main()
{
volatile int a=5;
printf("a=%d\n",a);
a=a*4;
printf("a=%d\n",a);
}
Output:
a=5
a=20
Prepared by Aravinda Kasukurthi, CSE, RVRJCCE Page 13