Basics of C
Basics of C
Basics.of.C
T2.1. INTRODUCTION
such as numbers,
language should be designed to support certain kind of data,
A programming is a set of statements to
A
gs etc. to get useful output known as information. program statements/instructions
characters, strings
a
specific itask, which will be executed in a Juential form. These
perform
keywords and allowed in that language.
eformed using certain characters, symbols to write programs in C.
In this chapter, we will study the basic elements used
22 C CHARACTER SET used
set means a set allowed in C. These characters are
of all the characters that are
Character included in Character set of
words, numbers and expressions and statements. The characters
to form
in four categories as detailed below.
Care classified and lowercase because
A to Z and a to z. These English alphabets written in upper
Letters.
1.
written using these letters.
C programs are
2. Digits. All 10 decimal digits from 0 to9.
area between words.
White spaces. White space or whitespace refers to the blank (white)
3. Horizontal tab, Vertical tab, New
line
one or more blank spaces given using space bar,
This includes
etc.
and Form feed some special specific
This
characters. category includes some characters used for
4. Special marks and some other special
urposes. This includes different types of operators, punctuation
haracters.
", , !, 1,/,,-, $ , ?, &, A, *, -,+, <,>, (G). L1. {. }. %, #,5 @,,etc.
For example., ;,
Table 2.11
Name
pecial character Name Special character
Ampersand
Comma &
Caret
Period
Semicolon
Asterisk
Minus sign
Colon
Plus sign
Question mark
Opening angel bracket or less
than sign
Apostrophe than sign
Quotation mark Closing angel bracket or greater
Left (opening) parenthesis
Exclamation mark| Contd.
iiowiw.www.wwwwwww.ww.wawwwww.www.ww
ww vw e w w w w w w w w w w w w . w .
17
Basics of C
u t e l i u i A A O A R G i n i K a w w w w w . 3 O A K A K W U N W A w
nnAinkitiNiKin ik
AMIOMGHIAW0RRMOR
Name
R i g h t ( c l o s i n g ) p a r e n t h e s i s
square
Name (opening)
bracket
Left square
Right curly
Forwand Slash (opening) brace
Left curly
(closing)
Back slash
Right Per cent sign
Tilde
rate
sign
Under s c o e the
At
Dollar sign
Number (hash) sign|
Equality
machine
machine
it into
into
translate
t ranslate
npiler
compiler
to
in each
sstatement
tat
statemem
2.3. T O K E N S
to the involved
involved
the s a m e o rtokens.
C and give
tocs,
tokens
program in and identifies
a g r o u p of
group
write a is
is
When we
the compiler,
first of all,
separates
statements
andaa
statement
s t a t e m e n t
r e the
are
basic bu
basic building building
is a group of
a
level language, Tokens
T o k e n s
prepares
A program unit.
u n d e r s t o o d as a compiler
written in the The
program.
is
sequence of
characters that the C.compiler. called Symbol Table.
1abie
token is a elements recognized by called Symbor
A
and are the basic This table
table is is
blocks ofa Cprograminvolved in all the statements of approgram.
rogram.
if(a> b)
big=a;
statement areif, (, a,>, b, ), }, {, big,
=, i
Tokens involved in this
below.
five categories listed
C tokens fall roughly into the
1. ldentifiers
2. Keywords (e.g. main, for, int, float)
character constants and string constants)
3. Literals (numeric constants,
4. Operators (e.g. -
+, %, >, <, && )
S. Delimiters or punctuation marks e-g. comma()
2.3.1. ldentifñers
ldentifiers are user-defined names and are consecutive string of letters, digits, and underscore
characters, the first of which must be a letter or an underscore but should not be started with a dioit
In C programs,identifiersarethose words used by programmers which idenüty sonmething within the
program, like names of the variable, names of functions, names of arays, names of symbolic constants
ts
are allidentifiers. Sometimes they are called user-defined identifiers to distinguish them from othe
types of identifiers called system-defined identifiers (called reserved words, which are dioo
in this chapter). (The tem "user" here implies "programmer"
who is writing propra Sedlater
Some rules for defining identifiers
1. The first character must be an alphabet (or an underscore)
and underscore.
2. Identifier names must consist of only letters, digits But it must
not
a digit. begin with
JRD D
3. An dentifier name should have
dard C
4. Any standa less than 31
language
5. An ident ier should keyword cannot becharacter
ters.
not contain used
d
Since Cis case sensitive, sum, blank whitea
or
as a an
identifier name.
7. Symb bolic constant created Sum and
SUM
space.
are three
He is also identifier using preprocessor directive for
an different identifiers.
Example of valid
identifiers are example
ple #define PIE 3.14 Here,
sum, avg3, s,K1, INT,
the following table 2.2 Float, roll_no,
shows invalid _sum, sqrt, printf, (double underscore).
identifiers and the reason
Table 2.2 why they are invalid
Invalid identifiers
Reason for being Invalid
float
It is reserved
lavg word of C (used as data type)
First character is a
student marks digit
pace not allowed
salary$
roll-no Special symbol $ is not allowed
Hyphen or minus (-) not allowed
"niyati" Double quotes are not allowed
2.3.2. Keywords
Certain identifiers have predefined and specific meanings and cannot be redefined. Such
identifiers
are called system-defined identifiers or keywords. These keywords cannot be given any new meaning by
Drogrammers and cannot be used as variable names. These system identifiers, called keywords, are listed
2.3.3. Literals
Literal means constant values. A "constant"is a fixed value, which means that its value cannot
be changed throughout the program. It can be a number or text (string) that can be used as a value in a
program.
In C, there are 3 different types of constants. These are-
(a) Numeric constants
(b) Character constants
(c) String constants
(a) Numeric Constants. Numeric constants can de wnote numbers (integers) or
real numhers.
Integer constants can be represented using decimal numoer
system, octal number svstem aand
hexadecimal number system. By default, a numDer boog
umber system.
are represented with the number preceded by a zero character. A hexadecimo numberOctal numhers
i numbers
prefixing the characters Ox.
() Whole numbers or integer constants.
Example. 77 represents integer number written in decimal number svste
integer constant written in octal number system. Ox 180 represents an i
hexadecimal number system.
u or U. The long integer
The unsignedinteger constant is written with an appended character
is ritten appended with character t or L.
number system. 12Lor 12trepresents
onstant
Symbol Description
Escape sequence
name
Makes an alert, such as a beep
Alert la
Moves the cursor back one spaçe
b
Backspace Moves the cursor to next logical page
Formfeed f
n Moves the cursor tonextline
Newline Moves the cursor to beginning of line
Carriage return Prints a horizontal tab
Horizontal tab Prints a vertical tab
Vertical tab v
Prints a single quote
Single quote Prints a double quote
Double quote Prints a backslash
Backslash Prints a question mark
Question mark 1?
Translates into char represented
Octalhex numher (number)
(ü) Trigraph Sequence. Various reasons exist for using trigraphs. One reason is that
keyboards may not have keys to cover the entire character set of the C language, and therefore SOm
of special characters may be difficult. Using concept of Trigraph sequence we can use these chara
Each Tigraph sequence comes in three combinations of characters i.e. two question marks
Tolowed by a special character. Suppose, if a non-English keyboard does not support # (hash si
then we can use them in a C program using the trigraph character ?? and=. sigm,
Example.
printf("Serial ??=7 ??/n is here")
Output.
Serial#7
is here
Explanation. Trigraph sequence ?7= is replaced with # and ??/ with L ??/n becomesin
an
escape sequence character and acts as New line character which is
Table 2.5
Trigraph sequence Replaced with
# (hash) sign
[ left (opening) parenthesis
Iright (closing) parenthesis
{ left (opening) curly brace
right (closing) curly brace
?2! I vertical bar
back slash
22 caret
22-
tilde
(c) String Constant. A
double quotes (only) whereasstring constant is a sequence of zero or
more characters
enclosed within
constant. For
a
single character enclosed within single
example, *a" a
is quotes are known as
character
examples of String constants are:string constant, whereas *a' is' a character constant. Some valid
Valid String
"Vasu" Description
String constant of length 4.
BBK DAV College"
String constant can include spaces also
123 Numbers written inside double
"Agam-Sood" quotes, is also a string constant
Special characters like can be part of a
-
2.3.4.Operators
Operators are the symbols that instruct C to
perform some operation. For example, atb
expression. Here, a,b are operands and + is an operator is an
which is used to add values of a
and b.
Chas a large collection of
<2
operators.
&&, ! etc. Operators will be
Some of the symbols used for
operators are+,*,,,,
discussed in details in the
chapter Operators and Expressions.
2.3.5.Delimiters or Punctuators
A punctuation character or a group of characters is that which
of data, or is used to denotes the separates two names or two units
beginning or end of a programming construct. Delimiters are used in
almost every computer lanugage. Therefore, in C
and working are known as delimiters. These symbols language, such symbols that have a special meaning
are as follows:
Table 2.6
Delimiter Symbol Description Example
Symbol) name
case 2:
Keywords Delemeters
Identifies Operatorss
Literals
Flg. 2.1