C Notes
C Notes
Q#ncudetoio.h ud
LHS RHS
b
(3i mDiAC)
Lad to pufpom zyraboud alue alhtnitwill complete am ereudiou Typiealy avetiys value o
Oibxoyi Calleeton o pre tu tode hat Cam be usd to
pezlaim comng task aodped up dualopmed.
9psiakf CHellauenld')
ist mai
RAM
pisdf ([Link] "); he willitn Ar ello waulokautput
Oonpile
)bsembler Couesuon yecifahom
(inken
Hilude<stdio,h Gnveon charecb:
jot nain()
evel
-/'
ALGOLashi
[Link] Cowiter
)ltonpil timtinibiolyation a úoi able
[Link], losigued, uslgned Hiselude<stale.h>
Lintn 3
i t n=2
int nn = 8
ntns =
int Aug =(nLt n34 y4 us)/5;
teeisaddueu ol apvata
#uinclue<stde.h
Couneclo wbtuewer flouchard
ptsd we we Connectar
Atart
Temival
lestt sep)
decneneut perahr
dttienl
less ta
gezla)
ee
anesed when e t toeut mene perator
0ical opeataA Ranling
Lelt to Rigld
falss false
falie false
false
falie
fale alse b20
false
Dd nat evaluate
fale
Iycituimaut Deetmt spumala
Iucremeut wnes eHeynnt taevalie waniable by
IacKemet()
wiU uant,eualuat
Qne-ine pstdee
oeted by
1
Mens poand
Lvalue
Rvalue
Lvalue s, alwoyys a otable ,
Frepresutr slany
tocatlbm
Rualue ds aslgn tu Lvalue.
decnendthe valu.
6
pnecioete++
-batth
sdubstlutoy 13
Ealntiom
Aignanet
pest-ie post-de
a (p(a=19)
ElQ4Xa 19
YOR (A)
ooooo100
(1oin3)
Bisue XOR^
at ist Conbtut
binany form.
(oooo) ( 8421)
Lwe
se tae famla
Davable kita to be slwftog
Q=15 b=6 c =0
32 16 8
Tue
Sole
1aleplaoo 3
UNIT
eicutedaeuesealy
sblemnln
staleea
>pt stotemet
Bue
folse
Sytax
tonditon)
->incluae < stdo.
itain l) iata
else
iut pite,te, bill j
elebeck
bill>500)
Wj jner else Bock
t a
eley(aso)
Callesslwe
break
hlocle
bHeak'
blok-n
oxeak
deoul Salewd
bneake,.
jnttaicej
choieR)";
Ylaiee);
ASOTE he bueak stattut iswwecd to end
Casci
statemetE
AOT E() wthaut bueak staeneut the praskaua
Case
Lcoutinue to the Met tse, exeeuting the cases Latil
bxeak'
breake
Caset:
puitf ('vauwet") ;
pant
hveale
pidt ("buwel")jtake
bneak'
byeak
Case0
break,
Case 1:
Hepeaf a block c Code undil thé xpeihed condition
nt ni, y claice' M. A logp atote ment alow proxammex to egeeute tae
attemut th gxoup c statem sut muliple fusnd wtlaut
Cocde.
Laaitl (plaica)
bxeak'
Case
oe 3
program using the trigraphs ??( and ?2). ?? tilde
C ToKENS
Ina passage of text, individual words and punctuation
smallest individual units are known as C tokens. C hasmarks
six
are called tokens. Similarly, in a C program the
are written using these tokens and the syntax types of tokens as shown in Fig. 3.1. C programs
of the language.
CTOKENS
Note c99 adds some more keywords. See the Appendix "C99 Features".
Identifiers refer to the names of variables, functions and arrays.)These are user-defined names and consist
of a sequence of letters and digits, with a letter as afirst character. Both uppercase and lowercase letters
are permitted, although lowercase letters are commonly used. The underscore character is also permitted in
identifiers. It is usually used as a link between two words in long identifiers.
Programming in ANSI C
49 struct
ANSI C Keyword int
Table 3.3 double switch
auto long typedef
else
break register
enum union
case return
extern unsigned
char short
float void
const signed
for volatile
Continue
sizeof
goto while
default static
if
do
CONSTANTS
change during the execution
Constants in C refer to fixed values that do not LO 3.2
of constants as illustrated in Fig. 3.2. Describe constants and
of a program. C supports several types
variables
Integer Constants
three types of integers, namely, decimal integer,
An integer constant refers to a sequence of digits. There are
octal integer and hexadecimal integer.
+ sign. Valid examples
Decimal integers consist of a set of digits, 0 through 9, preceded by an optional - or
of decimal integer constants are:
123 321 0 654321 +78
CONSTANTS
float Note that some versions of C automatically convert all floating-point operands to
float The final result of an expression is converted to the type of the variable on the leftdouble precision.
of the assignment sign
double before assigning the value to it. However, the following changes are introduced during the final assignment.
double
int
1. float to int causes truncation of the fractional part.
2. double to float causes rounding of digits.
Fig. 4.8 Process of implicit type conversion 3. long int to int causes dropping of the excess higher order bits.
evaluating expressions.
Given below is the sequence of rules that are applied while
Allshort and char are automatically converted to int; then Explicit Conversion
converted to long double and the result will be
1. if one of the operands is long double, the other will be We have just discussed how C performs type conversion automatically. However. there are
instances when
iong double; we want to force a type conversion in a way that is different from the automatic conversion.) Consider, for
be converted to double and the result will be example, the calculation of ratio of females to males in a town.
2. else, if one of the operands is double, the other will
double; ratio = female number/male number
to float and the result will be float Since female number and male number are declared as integers in the program, the decimal part of the
3. else, if one of the operands is float, the other will be converted
unsigned long int and result of the division would be lost and ratio would represent a wrong figure. This problem can be solved
4. else, if one of the operands is unsigned long int, the other will be converted to by
the result will be unsigned long int; converting locally one of the variables to the floating point as shown below:
5. else, if one of the operands is long int and the other is unsigned int, then ratio = (float) female number/male_number
The operator (float) converts the female _number to floating point for the purpose of evaluation of the
(a) if unsigned int can be converted to long int, the unsigned int operand will be converted as such
and the result will be long int;
expression. Then using the rule of automatic conversion, the division is performed in floating point mode, thus
retaining the fractional part of result.
(b) else, both operands wil be converted to unsigned long int and the result will be unsigned long (Note that in no way does the operator (float) affect the value of the variable female number. And also, the
int; type of female number remains as int in the other parts of the program.
6. else, if one of the operands is long int, the other willbe converted to long int and the result will be long (The process of such a local conversion is known as explicit conversion or casting a value> The general
int: form of a cast is:
4.9 Use ota cast
Inequality
& Bitwise AND Left to right
Bitwise XOR Left to right
Bitwise OR Left to right 10
&& Logical AND Left to right 11
Logical OR Left to right 12
2: Conditional expression Right to left 13
Assignment operators Right to left 14
*=|%=
+== &=
<<=>>=
the order of decreasing precedence. Rank 1 indicates the highest precedence level and 15 the lowest.
also includes those operators,which we have not yet been discussed.
It is very important to note carefully, the order of precedence and associativity of operators. Consi
following conditional statement:
if (x == 10+15 && y<10)
The precedence rules say that the addition operator has a higher priority than the logical operator (&
the relational operators (== and<). Therefore, the addition of 10and 15 is executed first. This is egu
to:
if (x==25 && y < 10)
The next step is to determine whether x is equal to 25 and y is less than 10. If we assume a valu
for x and 5 for y, then
x == 25 is FALSE (0)
y<10 is TRUE (1)
Note that since the operator <enjoys a higher priority compared to = y<10 is tested first and the
25 is tested,
Finally we get:
if (FALSE && TRUE)
Because one of the conditions is FALSE, the complex condition is FALSE.
In the case of &&, it is guaranteed that the second operand will not be evaluated if the first is Ze
the case of |l, the second operand will not be evaluated if the first is non-zero.
Rules of Precedence and Associativity
" Precedence rules decides the order in which different operators are applied operator
area
" Associativity rule decides the order in which multiple occurrences of the same level