two mar unit-4

Upload: suganrhithu9389

Post on 07-Aug-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/20/2019 two mar unit-4

    1/11

  • 8/20/2019 two mar unit-4

    2/11

      scanf456d7, a8  printf459esult:7, a8

    ;. )at is meant by variables# ariables are declared along wit its data type at te beginning of te program.

    Ex: datatype variable name)ere data type represents te type of data,

      variablename is te valid C identifier.

    ?. Give te 9ules of declaring variable or identifier:$e variable must start wit letter or underscore 4ex: int x, car fruit8 $evariable can ave numbers 4ex: int res28

    $e variable cannot ave 'ey word as identifier 4ex: float do8 because do is 'eyword.$e variable name must be uni*ue in teir location local or global. $at isvariable cannot ave same name in te local declaration part or in te globaldeclaration part of any data type.Give meaningful name to te variable for easy identification 4ex: int gitotal8,ere prefix g stands for global, and i for integer, and te name.$e variable cannot ave special caracters li'e 6, @, A etc. 4ex: int rf@s isinvalid8 Bo space between letters 4ex: int ap ple is invalid declaration8engt of te variable name 31 caracters.

    1D. )at is global variable#$e variables wic are declared outside of all te functions is called globalvariables. $ese variables are used any were in te program.

    Ex: int aF/main48{

    printf456d7,a8display48

    display48H

    aFaI/

    11. )at is local variable#$e variables wic are declared wit in a function is called as local variable.$ese variables cannot be accessed outside te function.

    Ex: main48

    2

  • 8/20/2019 two mar unit-4

    3/11

    {

    int aF/display48

    display48

    Hint c

    )ere a and c are local variables. >ariable a cannot be used in te functiondisplay48.

    1!. "efine constants#

  • 8/20/2019 two mar unit-4

    4/11

    • +ncrement and decrement operators

    • Conditional operator4$ernary operator8

    • Litwise operators

    • (pecial operators.

    1?.)at is $ernary operator#• $ernary operator is a conditional operator wit symbols# and:.

      Syntax: variable F exp1# exp2: exp3

    • +f te exp1 is true, variable ta'es te value of exp2. if te exp1 is false,

    variable ta'es te value of exp3.

    2D. "efine expression.1. =n expression represents data item suc as variables, constants and are

    interconnected wit operators as per te syntax of te language. =n expressionis evaluated using assignment operator.Ex : c F aIb

    21.)at is te output of te following#main48H

    int xF1DD, yF2DDNrintf456d7,4xOy8#x:y8

    Putput: 2DD

    22. "efine Nrecedence and =ssociativity:2. Nrecedence is a priority given to te operators. Eac operator as different

     priorities. +n an expression priority is given based on te operators used.$erefore care must be ta'en to avoid logical error wen using complexexpression wit operators.

    Example: x F 4sI1D8 & 4rIt8

    $e processing will be as follows(tep1: sI1D will be processed(tep2: rIt will be processed(tep3: results of step1 and step2 will be multiplied(tep!: te final result value will be assigned to x

    • Mrom te above example it is clear tat te following operators 48, &, I, and F are

    used.

    • $e first operation was 4sI1D8, te second operation was 4rIt8, and te tird

    operation was & and te last operation was F.

    !

  • 8/20/2019 two mar unit-4

    5/11

    • $erefore for te above example te igest priority was given to expression

    witin 4 8 and last priority was given to F 4assigning operator8. $is process of flow of operation among te operators is called precedence.

    • $e direction at wic te operators are processed is called assc!at!"!ty. $e

    above example sowed tat 4 8 moved from left to rigt, and operator e*ual 4F8

    from rigt to left.

    23. )at is meant by operator precedence#1. Pperator precedence describes te order in wic C evaluates

    different operators in a complex expression.

    2!. )at is associativity# )at is te associativity of unaryoperators#

    • =ssociativity can be eiter left to rigt or rigt to left.

    • =ssociativity is used only wen te operators are of same precedence.

    • $e associativity of unary operator is rigt to left.

    2/. )at do u meant by left to rigt associativity and rigt to leftassociativity#18 eft to rigt associativity evaluates te expression by starting on te left

    and moving to te rigt.28 9igt to left associativity evaluates te expression by starting on te

    rigt and moving to te left.

    20. )at is te purpose of control string in a scanf function#2. +t indicates tat wat type of data is to be read. +t is always

     preceded wit 6 sign

    2. )at are te types of +%P statements available in ‘C’#$ere are two types of +%P statements available in ‘C’

    • Mormatted +%P statements Ex: scanf4 8, printf4 8

    • Qnformatted +%P statements.

    • Ex: getcar4 8, putcar4 8, gets4 8, puts4 8

    2;. "efine single caracters input getcar48 function.1. $e getcar48 function is written in standard +%P library. +t reads a single

    caracter from a standard input device. $is function do not re*uire anyarguments, troug a pair of empty parenteses, must follow te statementgetcar48.

    2?. "efine single caracters output putcar48 function.2. $e putcar48 function is used to display one caracter at a time on te

    standard output device. $is function does te reverse operation of te singlecaracter input function.

    /

  • 8/20/2019 two mar unit-4

    6/11

    3D. )at is te difference between scanf48 and gets48 function#

    • +n scanf48 wen tere is a blan' was typed, te scanf48 assumes tat it is an end.

    • Gets48 assumes te enter 'ey as end. $at is gets48 gets a newline 4Jn8 terminated

    string of caracters from te 'eyboard and replaces te ‘Jn’ wit ‘JD’.

    31. "efine Nrintf Munction.1. Putput data can be written from te computer onto a standard output device

    using te library function Nrintf . Nrintf function moves data from tecomputer’s memory to te standard output device wereas te scanf functionenters data from te standard input device and stores it in te computer’smemory.

    32.)rite down te syntax of Nrintf Munction.2. $e printf function is written as

    pr!nt#$cntrl str!n%& ar%'& ar%(& . . . & ar%n)

    3. $e control string refers to a string tat contains formatting information andarg1, arg2, . . . , argn are arguments tat represent te individual output dataitems.

    33.)rite a simple program tat ma'es use of Nrintf Munction.Ainclude Rstdio.OAinclude Rmat.Omain48Hfloat iF 2.D, S F 3.D

     printf4 T 6 f 6f 6f 6fT, i,S, i I S, s*rt4 i I S 8 8 Putput:

    2.DDDDDD 3.DDDDDD /.DDDDDD 2.230D0;

    3!.)rite about Nrecision.4. T*e prec!s!n is an unsigned integer tat is always preceded by a decimal point.

    +f a minimum field widt is specified in addition to te precision 4as is usually tecase8 ten te precision specification follows te field widt specification.

    3/.)rite a Nrogram to read and write a line of text.Ainclude Rstdio.Omain4 8Hcar lineU 1/ V gets41ine8 puts41ine8

    0

  • 8/20/2019 two mar unit-4

    7/11

    +nput:

  • 8/20/2019 two mar unit-4

    8/11

    !2.(pecify te general form of an if statement wic includes te else clause#

    !# (expression) statement 1 else statement 2

    11. +f te expression as a non[ero value 4i.e., if expression is true8, ten statement 1

    will be executed. Pterwise 4i.e., if expression is false8, statement 2will beexecuted.

    !3.)rite about while statement.

    12. $e wile statement is used to carry out looping operations in wic a group of statements is executed repeatedly until some condition as been satisfied.

     !!.Zention te syntax of while statement.

    +*!le $ expression) statement 

    13. $e statement will be executed repeatedly as long as te expression is true 4i.e., aslong expression as a non[ero value8. $is statement can be simple or compound,toug it is usually a compound statement.

    !/.)rite down a simple program to display te integers D troug ?.Ainclude Rstdio.Omain4 8H

    int digit F Dwile 4 digit RF ?8 printf4 T6dJnT, digitII8

     }

    !0.)y tere is a need for do-while statement.

    14. )en a loop is constructed using te wile statement, te test for continuation of te loop is carried out at te beginning of eac pass.

    15. (ometimes, owever, it is desirable to ave a loop wit te test for continuation atte end of eac pass. $is can be accomplised by means of te do wilestatement.

    !.)rite down te syntax of do-while statement.o$e general form of te do - while statement is

    d statement +*!le (expression),

    o$e statement will be executed repeatedly, as long as te value of expression is true4i.e., is non[ero8. Botice tat  statement will always be executed at least once sincete test for repetition does not occur until te end of te first pass troug te loop.

    o$e  statement can be eiter simple or compound toug most applications will

    re*uire it to be a compound statement.

    !;. )rite a note on for  (tatement.

    ;

  • 8/20/2019 two mar unit-4

    9/11

    1. $e for statement is te most commonly used looping statement in C. $isstatement includes an expression tat specifies an initial value for an index anoter expression tat determines weter or not te loop is continued and a tirdexpression tat allows te index to be modified at te end of eac pass.

    !?. Give te general form for te for  statement.2. $e general form of te for statement is#r $ expression -, expression (, expression 3) statement 

    3. $ypically, e xpression 1is an assignment expression, expression 2 is a logicalexpression and expression 3 is a unary expression or an assignment expression.

    !. )en te f o r statement is executed, expression 2is evaluated and tested at tebeginning of eac pass troug te loop and expression 3 is evaluated at te end of eac pass.

    /D.)rite about Bested Control (tructures.

    -. oops li'e ifelse statements can be nested one witin anoter. $e inner andouter loops need not be generated by te same type of control structure. +t isessential tat one loop can be completely embedded witin te oter tere can be no overlap.

    (. Eac loop must be controlled by a different index. Zoreover nested controlstructures can involve bot loops and if else statements.

    . = loop can be nested witin an ifelse statement and an ifelse statement can be nested witin a loop. $e nested structures may be as complex as necessary, asdetermined by te program logic.

    /1. )at is te purpose of te switc statement#

    • $e switc statement causes a particular group of statements to be cosen from

    several available groups. $e selection is based upon te current value of anexpression wic is included witin te switc statement.

    • $e general form of te switc statement is

    s+!tc* (expression) statement 

    • were expression results in an integer value. Bote tat expression may also be of 

    type car, since individual caracters ave e*uivalent integer values.

    /2. "efine brea' statement. Give an example.

    • $e brea' statement is used to terminate loops or to do wile, or switc

    statement. $e brea' statement is written simply as/rea0,

    • witout any embedded expressions or statements.

    Eg.switc 4coice F toupper4getcarD88Hcase \ 9 ‘ : printf4 T9E"T 8

    ?

  • 8/20/2019 two mar unit-4

    10/11

     brea'case \ ) \ : printf4T)E >=QE MP9 ] T 8   continue

    /!. "efine Comma operator.

    • $e comma operator 4,8 wic is used primarily in conSunction wit te for 

    statement. $is operator permits two different expressions to appear insituations were only one expression would ordinarily be used.

    #r4 expression 7a, expression 7b; expression 2; expression 3) statement 

    • were expression la and expression b are te two expressions, separated by

    te comma operator,

    • were only one expression 4expression +8 would normally appear. $ese two

    expressions would typically initiali[e two separate indices tat would be usedsimultaneously witin te for loop.

    //. )at is palindrome#

    • = palindrome is a word, prase or sentence tat reads te same way eiter 

    forward or bac'ward. Mor example, words suc as noon, peep, and madam are palindromes.

    /0. "efine goto statement.

    1D

  • 8/20/2019 two mar unit-4

    11/11

    • $e goto statement is used to alter te normal se*uence of program execution

     by transferring control to some oter part of te program.

    • +n its general form, te goto statement is written as

      goto label;

    • were label is an identifier tat is used to label te target statement to wic

    control will be transferred.Eg.scanf 4 T6f+T , x8 wile 4x RF 1DD8 H. . . . .if 4 x R D8 goto errorcec'. . . . .scanf 4 T6f+T , x8 error detection routine &%errorcec': H

     printf4TE99P9 BEG=$+>E >=QE MP9 ] T 8

    11