0 ratings 0% found this document useful (0 votes) 40 views 9 pages Assignment 1 PDF
The document outlines the fundamentals of variables and constants in the C programming language, detailing their types, declaration, and scope. It explains the different types of operators available, including arithmetic, relational, logical, and bitwise operators, as well as the concept of expressions. Additionally, it covers the rules for defining constants and the significance of variable lifetime in programming.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save Assignment 1 .pdf For Later Name * k.Vishva Ranj
yor sampelley
Enwollment no. + AS6I7S1AYO6A
ASSIGNNENT- 4
Vlaviables in Ge
© @ vaviable is cefined as a meaningfel name given 10 tho
data stosage location in cormputes memooy
actually wefes te addvess oF
ie Gosed.C language
¥ Shen Using a vasiable , Wwe
Ihe memo ‘whee the eater
Supports 4wo basic hinds of vavictbles
* Numeric vasiables can be used to slove eithes integes
values oF floating point valies
€ (hile an integer value is a vehole Numbess without
a Fsactien past os decimal peint,a Floating peint
numbes can have a decimal point in ther
yurnesic values map also be agcocitted witin
medifiess tike shost long signed and unsigned .
e BY default ,¢ automatically « numesic veviable
aignet
ee chasactes vavials|
alprobet os fem the ’
anal ae pul betreen Single quotes .
es can include any lebtes fren the
Asc chast and mambess 0-7
© scanned with OKEN Scanner= To declare a vasiable specify dakt type of the
Vatiable followed by its name
* Vatiables names should always be meaningful and
Must welled tre puspese of theis Usage in ine
Pregvam :
* Vasiables dedlersatien always ends vith semicolon
| =p Example = int emp —nuem ;
float scllecoy 5
chas gvacte ;
= Types in Vavviables :
‘) Numevic Variable,
a) Charactes Variable
$$
© scanned with OKEN ScannerCONSTANTS
Constants ase identifiers vohose values does not
Change .
*Censtants ave uced to define Fined values like Pi 09
the charge en an election so that thers vetlue
| does not get changed in the psegyam even by
| mistake -
* To declaxe a constant , poecede the nosmal vasighle
declayation sity constant Keeproond and assign it a
value
—» Fox example s- const Float pi= Belly;
» Anothes way to designate a conctamt is to use the
pse- processor cemmand, define
=r tedefine Pi B.lyIst
When the pve poocessoe xefoamals the pregsa™ to
be cermpiled by the compies, it veplaces each defined
name with its corsespending value wheseves tt is
feend in the sousce poogram . Hence , it just wooks
like the Find and sepiace command available in &
tert ectitow
=) TYPES oF CONSTANTS:
) Integer constcnts
a) Floating point constants
3) Charactes censtanls
ue) atoing constants
© scanned with OKEN Scannere
ae
Rules that needs to be plied! to a 4define
Stafernent rahich defines constants.
Constanls names ase Usually rwsitten in capital letteos
to visually distinguish thern foe othe vavialle Name)
rohidh ase novially rwoitten in lowes case chasacless
Note that this is just a convention and not a ule.
No blank spaces ane pewmittect in befween the tr
Syrnbol and define Kexpsoxcl
Blank space must be used between the #H#define
and censtant name and belween constant name
and censtent value
tidefine & @ poe~prOcEssor compiles dinective and not
a Steaternent. Thevefore , it does not end With a semi-colon
© scanned with OKEN ScannerSCOPE AND LIFETIME OF VARIABLES
exe @ variable
~* Scepe 3 The vegion of pregsam uch ee
accessible
¥ Local Seepe * Vaviailes declazed inside function 0” blocs
of cede axe enly accessible wilwr thot
fandtien
¥ Global seepe + Vasialstey declared eulside of funcfian ase
accessible fem anyehese In the pseqvan.
|
~¥ Lifetime % ‘The dusation fos which a’ variable exists
' and holds its value in memowy
¥ Loca! vasiable - Gust ony dasing the execetion of functen
| in which they axe declased and ase
| destreyed vshen the function ox block
| euts
| & Global vavialsle Euisle fern the sterst of the pregram |
| until the psreqram tesminates.
=~ Local Vasialble 3
© Declared ieithin a fanctien ow block of cede
sible fom vaithin the fandion vehese they ase,
o Only acces
de clayed
= Global Variable 3-
y » function
e Declared oulside of @ny 14 ; :
@ Accessible fem anywhere 19 pseqsomn, including all
feenctions,
© scanned with OKEN Scanner-
OPERATOR IN G
= C language suppors @ lot of opesaloss to be
tesed IN cxpzessinns. These opevalovs can be
Categorized into following major groups *
1) Arithmetic Opesalors $+ Muettiphy Operator («JP ae
¥ Divide —eOperalos (1) - C/b
= Addition» Operator (+) ath
© Sudstrcaction -» Operator C)*a-b
© Modeolues © Opesalor (7.) Fb
a) Relaticnal epescfors S ABO KnewN as a CEMpasison
Opevatos, it 1& an opevaatos that Compares tex0 values.
Relatienal cpesalovs rveluon tue oy false value, depending
en vehethes the Condlitional velationship belween the
+00 eperands holds os net.
Ope satoss + = Less than (<]
Gueacter than (7)
Less than oF equal to (<=]
ggacles than ov equal to (> =]
= € Sup thsee
ical opesaloss 2 C language supposts
° Neal os sates. Thesy ase lf teal AND (Ak) , Logical oa()
eg! S es 7
and Legical Nor (1).
© scanned with OKEN Scanner“ Bittwise Operator =
: Ceara pc PzaIES, pestosm operations af bit (evel These.
ae . a bitwise AND, brtwice OR, bitwise YOR
*® The bit pe
itwise AND opesaios (2) is a smatl version of
He beclean AND (48) a3 it perfooms epewation on bils
" ‘stead of byles, chars , integers ,ete
the bitaise OR opesatos (1) & a Srnall vewsion of boolean
OR (1) as it pestorms epesation on bits instead of byter
chars, integess etc
The bilwise NOT (~),08 Complement is a unary epesation
that pesfowss legicl negation on each bit of the
Cpesand .By performing negatbien of each bit, it actectl ley
preduces the enes complemené of the given binaay veluc.
ie The bitwise XOR cpesatos (A) pesfowms epevation on |
| individuead bits of the opesands Tre sestelt ER epesation
8) "Basignment cpesalors > This is vesponsible fer assigning
values to the variables. while the equal sign(=) & the
feonclamentel assignment operalos , C ctlgo supports ctho-y
epese! axignment cperatoss that provide shosthand ways
+0 repesent cermrnon vasiable assignments.
vopesatarsss el /=]
e(\= ]
os
*(-=]
ef t=)
e( &J
of *=]
[ee]
ef?>F]
© scanned with OKEN Scanner6) Conditional Operators =
© The cenditional cpevallos (?+) is jest like an if. else
Statement that can be written within expressions
+ The syntant of the condittonal operatos is erp1 2 exp 2 seips
+ Herve erp S cvalwatedd fost. TF it is true then evp@
| is evaluated and becomes the vesult of the expsexien,
athesidise exp@ is evaluated & becomes te result of
the expression . fox enaunple 8 lange = (7b) 2a 2b
| * Cendlitional cperaisys make the pregyam cede morse
| C@empact, more veadlable, & safer to use ay tris &
| both to check and guarantee that the axqurnenfs that
are cwsed fox evaluation
| Cenditiona! cperator is alto knewn as temany operaioy as
it is nethes @ wmasy nor a bincrsy eperator 5 it takes |
tnvee cperands
9) Misc epevators’- Miscelleneus Epereetoxe) %
| Shift cpetador in€ & « leFt shift operator (<<)
* Right shift opereefor @)
* Incsernent and Decsernenl Opesafoss +
@ +tq (4 incseas? the initial volue of 4. bY ')
© qtt (intial value of qi wed in cede first .& then it is
increased by 1)
e@ ~p. (he value of q is fisst deceased by 1 fivst)
eae (the vakie af q rould be used as is in the :
progoam fisct G then decrease fhe vale oF
q by 4).
© scanned with OKEN Scanneree eee
EXPRESSIONS
*in C Plogzumming expsesiens are cembinadions oF
values, variables anc qperators that evaluate 10 single
Value. We can cateqosize exnpyessions based on type
Of operation they pesfosm + asithmetic , velatenal , legical
and cenditenal.
|
) Anithmetic Expressions s
* These expressions peroxm mathematical calcelations,
* Operators + Include addition (+),subtsaction (-),
* Example xt pia ie ee
: YF2, a/b
2) Relational Expressien }-
# These Expressions compene two values & evaluate to etthes
toue oy false.
© Operators & Include equal to (==) not equal to(1=),qseatey
than(*), leas then (<), qoenter than ov equal @=)
18.8 than 08 equal to (4-)
e Evample = k¥==S, yl=z2 arb
3) Legical Expressions *.
+ These expression Combine ox modify boolean vaalegos (Ince fetse)
Opescators * Tncleede AND (4k) logical oR(iI) & logical tor C1).
# Example > (x5) &A(yelo), (ob) IG led) (wey)
) Condibfomt exyessiens s-
» These crpsessions also known a3 fenesy epescilon jevaluate a
condition & xekrin ene of Koo values based en the result,
4 Synter: condition 7 value if true + value if-feelge,
© Excunple * (xy) 2 034 Cus cupyession velwang the
lasges value between 1 ancl y)
Ha
© scanned with OKEN Scanner