0 ratings0% found this document useful (0 votes) 43 views24 pagesC Programming Notes Part 3
c programming notes mechanical engineering semester 1 part 3
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
1570972019
2D Aricays-
> Array having 2 sub-seiipts is called 2D-ANmay. TL is
also called madnix
int student [4] [2] sa" subscript = coumn,
Fotal v
1° subs ereipt = ¥ 0%
This indicates that no. of elements = &
jit shident Caltal-f{ Te en
| e {amt ol, 56h>
4 vows R £ 02, 303h5
r 1 03,. 80 45
rk Loa, 98 3,
3
01, 56, 02,83, 63, 80, of, 96
| U vames assignked in memory row-wise.
| each occupies 2 bytes.
ig bytes occupied.
*. total
|\
int Student Cadlad= & 01,56, 0% 3%,
03, 60, 04) FS.
even if you wreite Nake
jnt student C IC2d* >
The compukr will oft Undeystand that there are
© 4 teows.
BvT No. of coluMNs Mvs)
or else, fhe computer wil) not know where fo end.
Ve C4 ILA = EA 10, VI, 3! AAAS
U [cal
et ! ro |
R-2 2 u
R-3 5 im |
Ihea Ta
BE STATED IN THE SOBSCRIPT.‘wk main C )
Ce -
(nk shedent Pad Lads
Pe yas
fer Cizo; bdeayiaa)
i
PRIPLEC” Enten Roi mo. and marek: ")5
seanf ("Za ed", D4 eipeta LILO) & ta GI LDS
5
Hoy (d= i205 ic=35 i494)
i
| Prantf& O° Za%d \n"s sta LI Ced,
sta Li C11) 5
s
return 0|
|
I Int main C4
| t
int std Lda,
int 95
fow Ci=04 i d=25 144+)
; Eten
printf C™ Enter voll and matek: ")5
|
| scant (%d dvd", Rota Lillolle]
&std LUIEIITY
i} & ota CALA IL
|| int main ()
i
|
Int student [ie] LT;
int 35
for (iz 051 <= 925 144)
& {for ix 0534-25944)
becant Cy gy B student isp
Ad zed", B student CHICGT5~—
|] ®
picogream te picint fibonaci series
13 4 7 Me
= oe
| Findude
#indude
Tint main ()
int al3e), nol Ali 7
D5
sean¢ (*%d %ad%, nolo), & nels
preintf C* Enter any hoo nos
a Lod = nofa] 5
ja Ci) = noltts
[for Creazicios ive
jf
| als ati-srel-aly
5
|
print e (* Fibenecisereies: “ZANE
| preint® C" Fibonaci sereies fs An’
for Ciz05i It makes fhe program easy to understand and write
By Avoids the vepifitive same codes
FPA self-contained biocie
of -etemenis. statements that
T performs a panticulan tesk.
Finefon of 2 types
i) Library Fanchon (‘pieintf , scanf > these functions ane
\
Inbenent, present in headen fle stdio-h
(1m) User-defined functor.
\
Now, main focus:
\| User defned functon.
| ‘ [continued]
Any © pirogteam contains at least functon
|
[ mandatory. main funchon. othenwise i+ will nok catt the.
usen defined funchons |
4b 1f the funchon contains only one function, + must be
| main ¢)
F> If a © phogram contains more than
one of
them mus} be
one function,
main,| int main ¢)
=
Ree
| message () 5 —> function catting
printf C" Cry and stop the mountain. d;
Message C }—susen defined functon.
: °s
printEC’ Smile and the world smiles with you.
3
[netunn 0,
|
\5
main C )
i
printf ("J am in maind).")3
zusal )3
|| Braz C)3
yer?
Lo prante Cham in usa.)s 3
Brazid( )
§ promt C" lam in Brarin)s $sa functon can be catied any number ef times
main )
jt
Messagel( ) 5
Messagex );
u
|
| message) message 10)
}
j
main ()
A
Brazile 7;
pranteC Enter 3 nos "5
pseant ("Ad 7d 7d "Reale somlal & ee cate sort hl catesvm ted)
,
sum? cale sum ©) +,
| sum pref C" Sum Za", sum);
|
| eate sum om)char chs
printf ("%e", Rens
“if Coch >= 05 AK chez a)
ve turn (ch) 5
else Return (eh+34)3
5
Int a= 36
fun (n)3
printt "24" as
X
fun Ca +b)
| £ b=cos
| print? Cb)3
foreturn (bs
a~print® CP am in main.
main ¢ )
i
ValyC) 5
Brazil C95
3
italy ¢)
| printf Cl am in eaty.")5
3
Brazil ()
t
perintt CTL am in Brraail “Js
USAC)S
ay
us AC)
yi
| preintf C1 am in 05a.) 5
| Canada l)5
3
Canada ( )
3=) A Function can be called any qumben of times,
Main C) _ en
17 3
Imessage( ) 5 message 1 C) 5
Message C ) 5 message 20)5
} 3
| message ¢ )
|
message 2C) Message Oi
. i f
|| 3 a Viseaa.
Lmain ¢) 3 5
ik
[Brazil ls i
8 printf C" lam in diraetccint ab, Gsumy
3
eale sum (x, y,2)
main ©)
i
print? C" Enter 3 numbers: ”)5
seant ("Za Z%d Za", Ba &b Re)S
sum = cate sum Ca,b,c)5
printf C" sums %d", sum) 5
Int %Y,25
3
int dp
d= nryt 2)
vetutrn Cd) 5
5i 28/09/19|
int main ¢)
£
fmt main( ) } win not enter
Link i=a0; sthe funchon
display Cis [aisptay Civ] below, & vole
of + win enter
jhe funchon
be low,
display (int 3)
fink x= 355
printé ("'%d ids
| printé ("%d") kK);
\3
This is called scope
of function.
yute
=> Locatised function.
=> Scope Rule of Function
fun (ak ed (
\ arguments pead from
funCint x, int y, int z, mt w ) ight te left,
PrinlE seen from left
sprintf C"%d%d Xd “a, Ha,at+) 4p reight )
541
For Nbrahky functions, header file needed
Ly 4o detenmine what kind of Vvatiables
2 WNT Wore with HW ceturnCoddidhS > printf, scan
# inctude < stdio-h> err
what tre of Mia rr
int 1=10, 97205 of data 1 will retusn’
were work
j "“add", iad} with,
print "Za %d", iad5 Fist square Cer)
J primt€ CZ aS ids +
_
main( )
Rfloat a,b;
{leat squane (float);
printf C"\n Enter any number: "5 actral _angoment
—> the argament
sent from the calling
secant C" 7", Ba)y
pesqttatreta} b-square(a) 5 function,
peat C*\n square of «PF is xe% a,b); % 1 the
acttal ange ment
Feet) oq tee ne-Chtoata)
Float
(Frat squane (fat x) —>catted funchon, the Runchon is
t fwar yy being defhned here.
Y=XR XG
rete ye tunn(y); Float sqvane (Flvert *)
} L » float yb
yew ey
veturn (uy)
5This [Void] fonction is an example
main ©) of catt by ‘nothing? function |
t vole Baring yy a = SS
*
|| this function win not mefurrn any value.
Ie a function doesn’t neturn any value/ digits
|
Wb ts by defevlt void.
sa
vw
gt
ver
or
Int }= 3 —>value at location,
Preserve space
associate name
stone the value 3
mayn C$
2 met ie35
printf CMaddress of i= %u", 215
pring $C'An value of i= %d "515@ vatue at address operaky /
Scope Rute
RA Indinection operator. Functions-
—————— = Se ——_
. cal) by Value > a variable
main C ) is local fon the
funchon .... Carb)
{ it a=10,b=20, fanchon it 49
swapy (a,b), —s catling function. oy ee
only send
PYINEF C"\n aze%d and be %d 7s 4b)? a value
} fo another
swapv (int x, int y) > cated function. function. ©
Lint *&
t= 45
X= Yd
feb:
printf OC" \n xe %dsyeZd yds
3
| what i> the wesulk on the screen.-
X=20 , ¥=10
a=po and 6-20
> cail by refenence Aan by addiress,a
|main ¢)
Vine Q=10, br a0,
> address /R
: VC Ra, KbIS Leal by
(apy Carb}; swap
ive fereence
FPFIMEE CC” \n ex d and be ud", Rab)
a3
| swapy Cint Ta omt Fy) cee
it int t3
| b= &y;
|| tea dys
| tyeby
| PMLFC nm, Zayed”, wy);
3
(fn © the potential to change dhe Siginal value of
@ and b.
| a= 20, b=10 } Sand b ecway the varues
x? 20, b=lo of uM ond y Tespectively,main ()
a int radius,
== —
| Float area, cremference perimeters
scant (“Za & radivs)),
ae “areal radivs, &radix & arco, & & peivimetiy )
preint£C" Area= ZF") area)
print # C" Petrimeten = Z6%, perimeter) 5
aneapers Cink vr, float a, feat * p)
t
“ae Blu K BANS
APS Qa dK TS
3
no need to veturn the Value when sing an addivess
operator, i+ will return the values by defavit.
—if you declare a pointer,
main )
Line i, int #55 you musk point on the
+f other Vaniable
13> Rij —Smeane Dwi pan ah ae
printhCpdd oti
[print C"\n Value of 4
print® C\n ada of 2 44% BIDS
65524 Adddess
Z~da); 6dbay Address
printf C"\n vatue of 9= 7d", X(ea))) Cosa
primteC "An Zd"\3)5 coeay
printe C" %d", 4), 5 3
printf ("Ad") ® 4), 3
Adovalue at add vers of pointer J
Pot 1
| 3 | [esau
6b5aA4 65520.
| main ©)
mt tsi
James tes3
ouble ponte} Ponts ONGt han
1 i 4
Nyt Pointer.
d= &i5
Ke Ra) 4
PrinthC"\n 4d", &1), CH5X4Y — addvess of |
printf C"\y %4", 4); obey "
print# ("tn 2a" 24), 65524
printt C" \n xd "ay); 6520
printh (" Ad", k), co5ap
Prineh ("4d Rk) emare
printf C"79","@ i); 3
Print $C" Zg", 43), 3
Printf C" zd", tek); 3
i 1
ek ji 4
La | fobay eine] value (pointer)
SPP 65522 come addiness (address operator)
printf ("2d 9464) > points af 3
444 pon Gonay
#Y 3 Gbd20Sim
=—
chi
av ramel 1=1'P’,
|
PROGRAM
\\
|
| k
vod v
5524 65523
memory address.
J chan name L \ =" progsam
|
> main ©)
° a
v v
GHAR AL
3
chon name J>% program *;
|| int 1205
white (i <= 4)
V4 printf ("Ze") nameLil);
| it4y)
ab ee
a
nuit chanaeter
(imp. +» mention)
R n M
bo voy
a6 (718 “VR=
main)
4 char namel 1 “program”
ine
\|
septs Cname LiT4= '\o') — becavse all strings have a pull
If printe O%c', namel1T)s characte) O at the end
sey 5
iets
3
—
nem uo)
{ char name t Y=" program”,
|
char * p;
3
| name|—s base address
| P=name
| while (*1p1="\o") sohan type van
¢ Cro address)
A printhC"%e" &p)s
[eben combp) ‘omly mention base
Piety addvess Sor =dhany
\| 3
Nariable pointers.
cause We aPe poining
the Memory address,
|The memeny addness 1s
shifting to the sight
ond ¥p points the Value
at that memody address
euch hme.