Top Banner

of 12

Computer 12 Ch 2

Jun 02, 2018

Download

Documents

Muhammad Atif
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 Computer 12 Ch 2

    1/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    " #$ De%ne C&nstant$ E'p(a)n )ts types$*hat are +hara+ter +&nstants, )-e e'amp(es$./01 F3SE 2/#4*hat )s a Data type, L)st )ts -ar)&5s types )n C$./01 F3SE 2/#0

    Ans$ Data is a constant value used in C language. Constants arequantities whose values do not change during program execution. Theymay be numeric or character.#$ N5mer)+ C&nstants

    Numeric constants are of two types, integer and oatingpointnumbers.

    #$#$ 3nte6er C&nstants!ntegers represent values that are counted, li"e the number ofstudents in a class. #n integer constant consists of digit that doesnot include commas or a decimal point. Negative integer constantsmust preceded by a negative sign but a plus sign is optional for

    nonnegative integers. $or example/ 7a()d

    8#49 7a()d

    204 7a()d

    ;24409/ 3n-a()d C&mma )s n&t a((&?ed )n )nte6er+&nstant

    49$< 3n-a()d C&nta)n de+)ma( p&)nt ?h)+h )sn&t a((&?ed

    #$2$ F(&at)n6p&)nt C&nstants$loatingpoint constants are also called real constants. $loatingpoint constants are used to represent values that are measured,li"e the height of a person which might have a value of %&&.'( cm.

    2$ Chara+ter C&nstants# character constant is one of the symbols in the C character set. !tusually includes the following)

    )$ digits * to +,))$ upper case letters # to , lower case letters a to -,)))$ punctuation symbols such as semicolon /0, comma ,0, period .0

    and)-$ special symbols such as 1, , 2, 3, etc.# character constant is enclosed by single quotes for example 4a5, 465etc.

    " 2$ Des+r)be the r5(es @&r ?r)t)n6 names &@ -ar)ab(es$./01 F3SE 2/##

    Ans$ 7ariable naming rules are as follows))$ # variable name may include d)6)ts /to 5pper +ase (etters

    .A t& 1> (&?er +ase (etters .a t& B1 and 5nders+&re

    .1.

    ))$ # variable name may start with (etteror 5nders+&re .1. !tcan5t start with digit.

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e # &@ #2

  • 8/10/2019 Computer 12 Ch 2

    2/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    )))$ eser-ed ?&rds are not allowed as variable name.)-$ Spe+)a( +hara+terssuch as 6, 8, 9 etc. are not allowed in

    variable name.-$ Spa+esare not allowed in variable name.

    -)$ There is no restriction on the length of a variable name. :owever,

    only the ;rst eserved words li"e )ntare not allowed asvariable name

    &n&G 3n-a()d :yphen 0 is not allowed in variable name

    #C 3n-a()d 7ariable name can5t start with digit

    T?& ?ay 3n-a()d =paces are not allowed in variable name

    ab+ 7a()d 7ariables may start with underscore 0 or a

    letter

    " 4$ De%ne -ar)ab(e and )nte6er -ar)ab(e$ H&? they +an bede+(ared, E'p(a)n )ts types$Ans$7ar)ab(e:# variable is a symbolic name used to refer a memory locationin computer. !n other words a variable is a space in the computer5smemory set a side for a certain "ind of data. 7ariables are used so thatthe same space in memory can hold di?erent values at di?erent times.3nte6er -ar)ab(es: !nteger variables are used to hold integer type

    constants li"e @(, +' etc.3nte6er -ar)ab(e de+(arat)&n:#ll the variables used in a C programmust be declared. i.e.

    )nt s5m!n the above declaration statement )nt is a type quali;er, which tells thatvariable type is integer which set a side number of bytes by the compiler0and s5m is the name of the variable. Aore than one variables of sametype can be declared in single statement. $or example,

    )nt a> b> s5m> a-6Declaration of an integer variable can be preceded by the type quali;erssh&rt, (&n6, 5ns)6nedor s)6ned. $or example,

    7ar)ab(e N& &@ an6e E'amp(e

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e 2 &@ #2

  • 8/10/2019 Computer 12 Ch 2

    3/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    De+(arat)&n ytes

    )nt 2 02>8=I J ;02>8=8 sh&rt )nt s5m

    (&n6 )nt 4 2>#48>4I0>=4I J;2>#48>4I0>=48

    (&n6 )nt s5m>a-6

    5ns)6ned )nt 2 / J =9909 5ns)6ned )nt

    +&5nt

    " 9$ *r)te a pr&6ram that +an a++ept n5mber &@ app(es andn5mber &@ +h)(dren$ D)-)de app(es am&n6 +h)(dren and+a(+5(ate n5mber &@ app(es (e@t$ )n+(5de std)&$h-&)d ma)n.-)&d1)nt app(es> +h)(dren> per+h)(d> (e@t OUTPUTpr)[email protected] Enter the n&$ &@ app(es: 1

    [email protected]> app(es1pr)[email protected] Enter the n&$ &@ +h)(dren: [email protected]> +h)(dren1per+h)(d app(es +h)(dren(e@t app(es Q +h)(drenpr)[email protected] Ea+h +h)(d 6ets Qd app(esR> per+h)(d1pr)[email protected] Qd app(es are (e@t &-erR> (e@t1" =$ De%ne V&at)n6p&)nt -ar)ab(es$ H&? they +an be de+(ared,E'p(a)n )ts types$Ans$ $loatingpoint variables are used for storing oatingpoint numbers.

    $loating point numbers are stored in memory in two parts.#$ Mant)ssa: The ;rst part is the mantissa. !t is the value of the

    number and2$ E'p&nent: The second part is the exponent. !t is the power to

    which it is raised.F(&at)n6p&)nt -ar)ab(e de+(arat)&n: Declaration of oatingpointvariable can be preceded by the type quali;ers V&at> d&5b(e V&at> &r(&n6 d&5b(e V&at i.e. oat base, height.

    7ar)ab(eDe+(arat)&n

    N& &@ytes

    an6e E'amp(e

    F(&at 4 #/0I

    J #/0I

    with & or ' digits ofprecision0

    V&at base>he)6ht

    d&5b(e V&at I#/0/IJ #/0/I

    with %( digits ofprecision0

    d&5b(e V&at a>b

    (&n6 d&5b(eV&at

    #/#/4

  • 8/10/2019 Computer 12 Ch 2

    4/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    Pre+)s)&n:!t is the number of digits after the decimal point.

    " 8$ H&? V&at)n6p&)nt n5mbers are represented )n e'p&nent)a(n&tat)&n, *hy e'p&nent)a( n&tat)&n )s 5sed,Ans$ The oatingpoint number may be represented in e'p&nent)a(

    n&tat)&n. $or example, the number 20=II would be represented as2$0=IIe4. :ere, 2$09IIis the value of the n5mber .mant)ssa1and 4isthe e'p&nent, the power to %* to which the number is raised.

    The exponent can also be negative. $or example, the number /$///9=82is represented as 9$=82eW4 in exponential notation. The exponential formtransfers the number one digit to the left of the decimal point, followed bye and the appropriate power of %*.

    Bxponential notation is used to represent very large and very smallnumbers.

    !n C, a oating point number of type oat is stored in @&5r bytes. Threebytes @&r the mant)ssaand &ne byte @&r the e'p&nentand provides= t& 8 d)6)ts &@ pre+)s)&n.

    De+)ma(N&tat)&n

    E'p&nent)a(N&tat)&n

    E'p&nent)a(N&tat)&n

    De+)ma(N&tat)&n

    29=$89 2$9=89e2 2$0e= 20/////$//

    /$/##9 #$#9eW2 9$29eW4 /$///929

    W09$#20 W0$9#20e# #$2049=e0 #204$9=

    /$////49= 4$9=eW9 W8$II40eW0 W/$//8II40

    #////// #$/e= W0$4IIe2 W04I$I

    " I$ *r)te a pr&6ram that +an a++ept (en6th and ?)dth @&r are+tan6(e and +a(+5(ate )ts area$Ans$

    )n+(5de std)&$h-&)d ma)n.-)&d1V&at (en6th> ?)dth> area OUTPUTpr)[email protected] Enter (en6th &@ the re+tan6(e: [email protected]@R> (en6th1pr)[email protected] Enter ?)dth &@ the re+tan6(e: [email protected]@R> ?)dth1area (en6th X ?)dthpr)[email protected] Area &@ the re+tan6(e: Q=$2@ > area1

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e 4 &@ #2

    Enter (en6th &@ there+tan6(e: =$2Enter ?)dth &@ there+tan6(e: 4$4Area &@ the re+tan6(e:28$2I

  • 8/10/2019 Computer 12 Ch 2

    5/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    " a1 9$=/////pr)nt@. Q=$2@ > a1 9$=/

    !n the above ;rst printf0 function, the output of the variable a with formatspeci;er Q@is 9$=/////

    !n the second printf0 function, oatingpoint @&rmat spe+)%er Q=$2 isused. This format speci;er means to print the value of a in a %e(d ?)dthof =, with 2 de+)ma( p(a+esafter the decimal point. Now the output is

    (.&*

    !n general, if the value to be printed contains fewer characters than the;eld width speci;ed, it will be padded on the left with blan"s. !f the valueto be printed contains more characters than the ;eld width speci;ed, Cwill use whatever ;eld width is necessary for printing the value.

    OUTPUT OUTPUTV&at ' 0$#4#9 '1pr)[email protected]#$#@n Y> '1pr)[email protected]#$2@ nY> '1pr)[email protected]$0@nY> '1pr)[email protected]$4@nY> '1pr)[email protected]$9@nY> '1pr)[email protected]/ '1pr)[email protected]/ '1pr)[email protected] '1pr)[email protected] '1Negative sign 0 is used to align the output to the left.ositive sign 10 is used to align the output to the right.

    " #/$*r)te a pr&6ram that +an a++ept t?& )nte6ers and pr)nt the)rs5m$Ans$ )n+(5de std)&$h-&)d ma)n.-)&d1)nt n5m#> n5m2 OUTPUTpr)[email protected] Enter the %rst n5mber: [email protected]> n5m#1pr)[email protected] Enter the se+&nd n5mber: 1

    [email protected]> n5m21pr)[email protected] The s5m )s QdR> n5m# ; n5m21

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e 9 &@ #2

    0$#4#9 '1pr)[email protected]> '1pr)[email protected]> '1pr)[email protected]> '1

    r)nt@ YQ9d nY

    #20#20

    #20#20 #20 #20 #20#20

    Enter the %rst n5mber: 9Enter the se+&nd

    n5mber: IThe s5m )s #0

  • 8/10/2019 Computer 12 Ch 2

    6/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    " ##$ *hat )s a @&rmat spe+)%er, *r)te ab&5t d)Gerent type &@@&rmat spe+)%ers 5sed )n C$Ans$ # format speci;er is used to control what format will be used by

    printf0 to print out a particular variable or by scanf0 to read a particularvariable. !n general, the format speci;er should be matched to the type ofvariable we are printing or reading. # list of format speci;ers is givenbelow.

    F&rmatSpe+)%

    erUsed F&r 7a(5e Statement

    O5tp5t

    Q+ =ingle character char ch 2 465 printfEcF,ch0/

    Z

    Qs =tring charnG&H2F!mranF

    printfEsF,n0/

    A()

    Qd =igned decimal integer int a 2 (/ printfEdF,a0/

    9

    Q@ $loatingpointdecimal notation0

    oat b 2 (.&/ printfEfF,b0/

    9$=/////

    Qe $loatingpointexponential notation0

    oat b 2 (.&/ printfEeF,b0/

    9$=e#

    Q6 $loatingpointEf or Ee, whichever isshorter0

    oat b 2 (.& printfEeF,b0/

    9$=e#

    Q5 Insigned decimal integer int c 2 &(( Q5> Q' and Q& t& spe+)@y (&n6 )nte6er @&re'amp(e Q(d

    " #2$*r)te a pr&6ram that +an a++ept ?e)6ht )n )(&6rams and

    +&n-ert )t )nt& e\5)-a(ent p&5nds$ .N&te that # )(&6ram 2$2p&5nds1Ans$ OUTPUT )n+(5de std)&$h-&)d ma)n.-&)d1V&at 6s> (bspr)nt@. Enter ?e)6ht )n )(&6rams: [email protected]@R> 6s1(bs 6s X 2$2

    pr)[email protected]=$2@ )(&6rams Q=$2@ p&5ndsR> 6s> (bs1

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e = &@ #2

    Enter ?e)6ht )n)(&6rams: 9$=9$=/ )(&6rams #2$02p&5nds

  • 8/10/2019 Computer 12 Ch 2

    7/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    J. a1 De%ne es+ape se\5en+e$ ./01F3SE 2/###$ *hat ?)(( be the &5tp5t &@ the @&((&?)n6 statement,

    pr)[email protected] n L&-e n Pa)stanR1

    " #0$*hat )s an es+ape se\5en+e, *r)te ab&5t d)Gerent types &@es+ape se\5en+es$Ans$ The combination of Kn is "nown as escape sequence. The bac"slashK0 indicates that a special e?ect is needed and the character following thebac"slash speci;es what to do.

    The following are the commonly used escape sequences.

    Es+apese\5en+e

    Chara+ter epresented

    a #larm Leep, Lell0

    n to issue a new line

    t to issue a tab character hori-ontal0- to issue a tab character vertical0

    b to bac"space

    r to issue a carriage return

    ] to print single quote

    R to print double quote

    to print bac"slash

    , to print question mar"

    @ to form feed, it is a page brea"ing to continue printing onnew page

    "$ *hat are &perat&rs, ./01F3SE 2/#4"$ L)st the types &@ &perat&rs 5sed )n C (an65a6e$ *r)te ab&5tany t?& &@ them ?)th e'amp(es$

    ./01 F3SE 2/#2" #4$De%ne e'press)&n$ L)st the types &@ &perat&rs 5sed )n C(an65a6e$Ans$ Bxpressions consist of constants and variables combine togetherwith operator. Commonly used operators are)

    #$ Ar)thmet)+ Operat&rs2$ Ass)6nment Operat&rs0$ e(at)&na( Operat&rs4$ L&6)+a( Operat&rs9$ 3n+rement and de+rement &perat&rs

    " #9$*hat are ar)thmet)+ &perat&rs,Ans$ #rithmetic operators are used in ar)thmet)+ e'press)&nto performarithmetic operations. $ollowing the arithmetic operators, their symbolsand operations with example)

    Symb&( Operat)&n E'amp(e es5(t

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e 8 &@ #2

  • 8/10/2019 Computer 12 Ch 2

    8/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!

    ; #ddition @ 1 @ M

    W =ubtraction & < untime error L$ Operat&r err&rC. =yntax error D. Uogical Brror))$ # memory location with some data that can be changed is called

    #. Constant $ 7ar)ab(e C. Named constant D. None)))$ Phat happens when the result of a calculation exceeds the capacity ofdata type[

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e ## &@ #2

  • 8/10/2019 Computer 12 Ch 2

    12/12

    Prepared by: Ahmed Hassan Khan [Chapter 2:

    FUNDAMENTALS OF C LANUAE!#. =yntax error L. Uogical Brror C. =yntax error D$ O-erV&?

    )-$ The expression < E ( has a value equal to#. < L. ( C$ / D. None

    -$ Phich operator is used in a compound condition to \oin two conditions[#. >elational Xperator $ L&6)+a( Operat&r C. >elational >esult

    D. Uogical >esults-)$ #n ampersand before the name of a variable denotes

    #. #ctual 7alue L. 7ariable Name C$ Address D. Data Type-))$ Xperators used to compare operands and decide whether the relation is

    true or false are called#. #rithmetic Xperator $ L&6)+a( Operat&rC. =yntax Xperator D.#ssignment

    -)))$ The result of %' E < isA$ 2 L. < C. M D. (

    )'$ The equivalent statement of sum 2 sum num is#. =um 2 num L. sum2num C C$ s5m Wn5m D.

    sum 2 num'$ ]]]] should be matched to the type of variable you want to print orread#. 7ariable type L. variable name C$ F&rmat spe+)%erD. Bscapesequence

    ')$ #fter evaluation of expression ( E @ the value is equal to#. * $ # C. @ D. M

    '))$ The ;rst character of a variable name cannot beA$ D)6)t L. Inderscore C. #lphabets D. #lphabeticCharacter

    ')))$ Phich format speci;er is used to assign signed decimal integer[

    #. Ec L. Es C. Ef D$Qd

    ')-$ Phich of the following escape sequences is used to erase acharacter left to the cursor position[A$ b L. Kdel C. Kf D. Kc

    '-$ Phich of the following logical operator has the highest priority[A$ ^ L. 2 C. 99 D. YY

    '-)$ Aista"es that cause a running program to produce incorrect resultare called#. =yntax error $ L&6)+a( err&r C. rogram error D.

    None'-))$ Xrder of precedence determines which operator ]]]]]]]]]]]]]]]

    #. is most important $ )s 5sed %rst

    C. is faster D. operator on the largestnumber

    '-)))$ Phich of the following has the highest precedence[A$ .1 L. 11 C. 32 D.

    ')'$ Comments are used to increase the ]]]]]]]]]]] of the program#. 7isibility L. Leauty C$ eadab)()ty D.

    Complexity

    ''$ Phich address operator indicates the address of a variable[#. E L. Ka C. ^ D$

    P5nab C&((e6e &@ C&mmer+e Cantt$ Camp5sPa6e #2 &@ #2