0% found this document useful (0 votes)
5 views

Basics of C

Uploaded by

sahilsalaria2617
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basics of C

Uploaded by

sahilsalaria2617
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

2

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

Special character bracket

square

Name (opening)
bracket

Left square

Special character brace


Vertical bar (closing)

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.

a table of all tokens statement:


consider the following
For example, owwww.ewem

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

below. Notalllanguageshave thesame numbersofkeywords.Thenumber of keywordsinalanguageha


little to do with how "powerful" a language is. C has only 32 keywords that are described below :
Table 2.3
Sr. No. Keyword Meaning or description
1. auto declare automatic variable

2. break break out of a loop


a block of code in a switch
statement
3. case
4. char declare character variable
a
that do not change data
const declare immutable data or functions

6. continue bypass iterations of a loop


default handler in a case statement
7. default
looping construct
8. do variable
declare a double precision floating-point
9. double
alternate case for an if statement
10. else create enumeration types
11. enum variables defined elsewhere
tells the compiler about
12. extern variable
declare a floating-point
13 float looping construct

14 for different part


of the program
jump to a
15. goto
Meaning o r description

Sr. No. Keyword the result of a t e t


code based off of
execute
variable
16. if integer
declare an
int variable
17. declare a long integer
18. long a variable
be optimized for speed
request that
register retumfroma function
9.
retum
20. declare a short integer variable
short
21
signed
modify vaniable type declarations
22. retun the size of a variable or type
sizeof
23. create permanent storage for a variable
static
24. define a new structure
struct
25. variabie
switch execute code based off of different possible values for a
26.
ypedef create a new type name from an existing type
27 a structure that assigns multiple variables to the same memory
Ocauon
union
28.
unsigned declare an unsigned integer variable
29.
30. void declare functions or data with no associated data type
volatile wan the compiler about variables that can be modified unexpectedly
31.
32. while looping construct
In addition to these standard keywords, there are some more keywords like huge, interrup, nearec
Reserved word Vs keyword. In some languages, a reserved word is a word that is reserved for
the
special use by the language. Reserved words are system identifiers and cannot be used by
programmer as an user identifier. Technically, a keyword is a reserved word that is currently used by
the language with a special purpose. Therefore all keywords are reserved words, however some
reserved words are not keywords yet, though they may become so later on or in future versiOns.
For example, main is reserved word, not keyword of C.

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

Example 77u represents unsigned int written in decimal


int ritten in
decimal number system.
long numbers in C, and are of float,
Real Numbers. Real numbers are called floating point double precision.
on whether these are single precision or
dotble or long double data types depending
to double data type and is allocated 8 bytes of memory.
Butit
By default, any real number belongs
be made
float (single precision) by appending "f' with it.
can
and/or exponent.
Floating-point constants are numbers with decimal point
are given valid floating point constants. For e.g. %f
rample. Below
of single precision and is stored in 4 bytes of memory
2.3f is a floating point constant
constant of double precision and is stored in 8 bytes of memory
2.3 is floating point
notation
9.01e14 is a floating point constant written in exponent
has been made long double is given
1.234556L is floating point number of double precision and
10 bytes of memory values of single character
)Character constants. Character constants are constant (fixed)
different types of
byte of memory to store in memory. There are three
length and C allocates
one
in C. These are
character constants
Single character constant written inside single quotes.
are character constants. In C, we have
char data type for such constants.
For example, 'a', 'm,P "a can
used for purposes. For example, 'm' can be used to represent gender Male,
These can be many
and °P can be used to represents Pass.
be used to representsAgrade
or backslash characters. C has some special
characters called Escape
() Escape sequence
with al (backslash), and are followed
ofC and hasa special meaning. These characters start
sequence
are mnostly used inside printf )
and within double
character. These characters
by single number or character constants not string constants.
quotes. Still these are be used to embed a new line in a string
of
is \n', which can
The most common escape sequence
text. The following table 2.4 shows all
of the escape sequences-
Table 2.4

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
-

"dollar$" string constant


Special characters like $ is also allowed
!@#$%^&" String constant may includes combination of
99
special characters
6699
Space, inside double quotes is also a string constant
Empty string constant.
int" A string constant may includes
any xeyword
n This will act as a new line character.
Some examples of Invalid string constants
Invalid String Description
a Single character inside single
quotes is a character constant, not string constant
Abc Double quotes missing
Vasu Double quotes started but
not closed

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

Comma Variable separator is declarations int a, b, sum;


Semicolon For terminating a statement C atb
Colon For label in goto statement and
start
after case keyword in switch statement case 1:

case 2:

Parenthesis For writing expressions (a+b)*(a-b)


Square brackets For declaring arrays int array[100)
Curly braces For defining compound statement(s)

2.3.6. Operators Vs Punctuators


Punctuators do not specify any operation as operators do, rather they have special meaning that
elps the compiler to understand the program.
For example, colon (: ) is a punctuator and not an operator. It is used after labels given to
mentswhile using goto statement and also in switch statement after case keyword.
Basics of C 23
Certain operators also work as punctuators, like (O. As a punctuator, it is used to group an expression
ike in ab+c) and as an operator it is used as function call operator For example fun (a, b);
Another similar example is comma (.) that can be used to separate expression and is an operator
also.
by following figure 2.1:
C Tokens can be respresented
Tokens

Keywords Delemeters
Identifies Operatorss
Literals
Flg. 2.1

You might also like