C Programming - Question Bank.pdf

download C Programming - Question Bank.pdf

of 21

Transcript of C Programming - Question Bank.pdf

  • 7/27/2019 C Programming - Question Bank.pdf

    1/21

    1

    ARVIND SIRS NOTES IN C PROGRAMMINGQUESTION BANK ANSWERS

    Explain C tokens (2 marks)

    Tokens are basic building blocks of a C program. A token is the smallest element of a C

    program that is meaningful to the compiler. A token is source-program text that thecompiler does not break down

    The C compiler recognizes the following kinds of tokens:

    keywords : these are reserved identifiers having predefined meanings identifiers : these tokens name functions, variables, constants, and types literals : these tokens that specify values operators : these tokens are used to combine values in expressions punctuation : these tokens separate or terminate complex constructions special : these tokens have special meaning to the preprocessor or compilerA token can be a reserved word (such as int or while), an identifier (such as bor sum), aconstant (such as 3.14or "Arvind Kumar"), a delimiter (such as {or ;) or an operator(such as +or =).

    Example: Consider the following program:

    mai n( ){

    i nt r =10, ar ea;ar ea = 3. 14 * r * r ;pr i nt f ( "area of ci r cl e = %d\ n", ar ea) ;

    }

    The tokens in this program are:

    main - identifier

    ( - left bracket, delimiter) - right bracket, delimiter

    { - left brace, delimiterint - reserved wordr - identifier= - equals sign, operator

    10 - constant

    , - comma, delimiterarea - identifier; - semicolon, delimiter3.14 - constant* - asterisk, operator

    and so on. Thus a C program is a stream of tokens

  • 7/27/2019 C Programming - Question Bank.pdf

    2/21

    2

    Explain C Keywords What is a keyword? State two keywords of C

    Keywords are identifiers having predefined meanings in C programming language. Thelist of keywords used in standard C are :

    aut o doubl e i nt st r uctbr eak el se l ong swi t chcase enum r egi st er t ypedefchar ext er n r et ur n uni onconst f l oat shor t unsi gnedcont i nue f or si gned voi ddef aul t got o si zeof vol at i l edo i f stat i c whi l e

    All 32 keywords are in lower case letters. These keywords are reserved and cannot be

    redefined. Following rules must be kept in mind when using keywords.

    Keywords are case sensitive. For example, return is a keyword and it must be used asis. So it cannot be used as Return, or RETURN.

    The keywords have special meaning in the language and cannot be used for any otherpurpose such as constant name or variable name. State the use of %d and %f . Write a print statement in C using above

    mentioned symbols

    %d and %f are conversion specifiers. They are used with the control string in output

    functionprintf() and input function scanf()

    Consider the following statement

    pr i nt f ( r adi us=%d , ar ea= %f , r , a) ;

    The cont rol - st r i nguses %d conversion specifier. It describes how the value of n willbe printed.

    The following example shows use of %d. In this example %d is used in scanf() statement

    to accept an integervalue. Again %d is used in the printf() statement to print the integervalue.

    #i ncl udemai n( ){

    i nt a;pr i nt f ( Pl ease ent er an integer ) ;scanf ( %d, &a)pr i nt f ( you ent er ed t he val ue %d, a) ;

    }

    The next example shows use of %f . In this example %f is used in scanf() statement toaccept a float value. Again %f is used in the printf() statement to print the float value.

  • 7/27/2019 C Programming - Question Bank.pdf

    3/21

    3

    #i ncl udemai n( ){

    i nt b;pr i nt f ( Pl ease ent er a f l oat number ) ;scanf ( %f , &b)pr i nt f ( you ent er ed t he val ue %f , b) ;

    }

    Define expressionsExpressions: Expression is a single value or a combination of values and variableswhich are connected by operators. Following are examples of expression:

    a + b - c ;x = a ;x > 6 ;

    The first expression uses arithmetic operators + and , the second expression uses theassignment operator =, and the third expression uses relational operator >.

    Expression can also be a logical condition that is true or false. In C the true and falsecondition are represented by integervalue 1 and 0 respectively.

    What are operators?

    Operators: Operators are tokens used to combine values in an expression. Thefollowing are examples of operators used in an expression:

    a + b - c ;x = a ;x > 6 ;

    The first expression uses arithmetic operators + and , the second expression uses theassignment operator =, and the third expression uses relational operator >.

    Some operators perform operation on two operands, while others perform operation ononly one operand.

    Explain the various operators used in C Explain the bit wise operators in C Explain the logical operators used in C What is an operator? Explain unary and binary operators. Explain the increment and decrement operators. State four arithmetic operators and four logical operators of C

    The operators used in C are divided into following types:

    Arithmetic Operators Relation Operator Logical Operator

  • 7/27/2019 C Programming - Question Bank.pdf

    4/21

    4

    Arithmetic Operators:The operators used for basic arithmetic are =, +, - , * , and / .

    Operator Meaning

    subtraction

    + addition

    * multiplication

    / division

    % modulus

    The operator % is used in integer arithmetic. It is called modulus operator. It gives theremainder when the integer to its left is divided by the integer to its right.

    Example: 13 % 5 i s r ead as "13 modul o 5" and i t has t he val ue 3

    The modulus operation gives remainder of an integer division. Hence % cannot be usedin type float or double.

    Unary Operators:These operators perform an operation on a single variable and give anew value. The unary operators are:

    Operator

    Meaning

    negative value

    + positive value

    ++ increment

    decrement

    sizeof size of variable in memory

    Unary Minus:This operator is used to negate a numerical constant, variable or aexpression.

    Examples:

    35 2. 5 ( a * b) a + 35

    Increment operator:The operator ++ will increase the value of its operand by 1.. Decrement operator:The operator will decrease the value of its operand by 1. Sizeof operator:This operator will return the size of the operand. The operand can

    be an expression.

    Example:

    #i ncl udemai n( ){

    i nt p=10;f l oat q = 2. 5 ;pr i nt f ( the si ze of p i n byt e i s %d, s i zeof ( p) ) ;pr i nt f ( the si ze of q i n byt e i s %d, s i zeof ( q) ) ;

    }

  • 7/27/2019 C Programming - Question Bank.pdf

    5/21

    5

    The output of this will be

    t he si ze of p i n byt e i s 2t he si ze of q i n byt e i s 4

    Bitwise Boolean Operators: The six bit wise operators used in C are given below. Thebit wise operators work bit by bit on operands. The operands must be of integral type.

    & AND

    | OR

    ^ XOR (exclusive OR)

    ~ NOT ( changes 1 to 0 and 0 to 1)

    > Shift right

    Logical Operators: The logical operators used in C are given below.

    && logical AND if any one operand is false, the result is false

    || logical OR if any one operand is true, the result is true

    ! NOT if the operand is true, the result is false and vice versa

    The operators && and || are binary operators i.e. the logical operators require two

    operands. The ! operator is a unary operator.

    Consider operands A and B. The truth table for && and || operators are given below

    A B A && B A B A ||B

    false false false false false falsefalse true false false true true

    true false false true false true

    true true true true true true

    These logical operators are used to combine or negate expression containing relationaloperators. Here's an example of a logical expression.

    r = ( ( a&&b) | | ( c>d) ) ;

    In the example: r is set equal to 1 if a and b are nonzero, or if c is greater than d. In allother cases, r is set to 0.

    Define/Explain Data Types

    Every variables used in a C program is defined with a specific type. In Standard C thereare four basic data types. They are int, char, float, and double.

    char - char act er si nt - i nt eger s ( whol e numbers)f l oat - r eal number sdoubl e - hi gher pr eci si on r eal number s

  • 7/27/2019 C Programming - Question Bank.pdf

    6/21

    6

    In addition, C provides structured types:

    ar r ay - gr oups of var i abl es of i dent i cal t ypes,accessed usi ng i nt eger i ndi ces

    st r uct s - gr oups of var i abl es of mi xed t ypes,accessed by usi ng named f i el d sel ect or s

    uni ons - var i abl es t hat can cont ai n val ues of di f f er ent t ypes,dependi ng on a f i el d sel ect or

    C also allows enumerated types - variables that can take on a small number of differentnamed values

    Explain the break statement

    The break statement is used in loop statements (for, while, and do-while) . It is possible

    to force an immediate exit from a loop by using the break statement. A break statementis formed with the keyword break followed by a semicolon.

    Example :

    #i ncl ude mai n( ){

    i nt x;f or ( x=1; x

  • 7/27/2019 C Programming - Question Bank.pdf

    7/21

    7

    br eak;case 3 : pr i nt f ( you ent er ed 3) ;

    br eak;def aul t : pr i nt f ( wr ong choi ce) ;

    }

    Explain the continue statement

    It is possible to bypass the loops normal control structure and force an early iteration ofa loop and. This is accomplished by using continue.

    The continue statement is used in loop statements (for, while, and do-while) to terminateiteration of the loop. The next iteration of loop to takes place, skipping code betweenitself and conditional expression that controls the loop.

    A continue statement is formed with the keyword continue followed by a semicolon.

    Example:The following program prints even numbers between 0 and 100

    #i ncl ude mai n( ){

    i nt x;f or ( x=0; x

  • 7/27/2019 C Programming - Question Bank.pdf

    8/21

    8

    case const ant 2 :st atement - bl ock2;br eak;

    .

    .

    .case const ant N :

    st atement - bl ockN;br eak;

    case def aul t :st atement - bl ock3;

    }

    where expression is of simple type such as int, char, or enum. It cannot have float ordouble type.

    When switch statement is executed, the expression is evaluated. The resulting value is

    compared to values of constants 1 through N in order until a matching value is found. Ifa match is found in constant ithen statements-block ithrough Nand the defaultstatements will be executed. Normally, last statement in each statement-block is a break

    statement so that only one statement-block is executed.The default clause is optional. If it is present then the default-statements are executedwhenever value of expression does not match the constant values.

    Example:The program defines variable v of char type. The program takes input andstores it in variable v. The program will check the value of v and match it with theseveral cases. If the case matches the corresponding statement is executed.

    #i ncl udemai n( ){

    char v;

    pr i nt f ( \ nEnt er a vowel : ) ;scanf ( %d, %c) ;swi t ch ( v)case a : pr i nt f ( you ent er ed a) ;

    br eak;case e : pr i nt f ( you ent er ed e) ;

    br eak;case i : pr i nt f ( you ent er ed i ) ;

    br eak;case o : pr i nt f ( you ent er ed o) ;

    br eak;case u : pr i nt f ( you ent er ed u) ;

    br eak;def aul t : pr i nt f ( you di d not ent er a vowel )}

    Example : In this example, the switch statement will allows execution of differentstatements depending on the value of n entered by the user.

    #i ncl udemai n( ){

    i nt n;

  • 7/27/2019 C Programming - Question Bank.pdf

    9/21

    9

    pr i nt f ( \ nEnt er a number bet ween 1 t o 3) ;scanf ( %d, %n) ;swi t ch ( n)case 1 : pr i nt f ( you ent er ed 1) ;

    br eak;case 2 : pr i nt f ( you ent er ed 2) ;

    br eak;case 3 : pr i nt f ( you ent er ed 3) ;

    br eak;

    def aul t : pr i nt f ( wr ong choi ce)}

    Explain the for-statement. Give the syntax of for-statement

    The for statement is a looping construction. It has the following form:

    f or ( i ni t i al i zat i on; condi t i on; i ncrement ){

    st at ement s}

    where

    initialization is a statement that is executed once at beginning of for loop. condition is an expression that can be true (nonzero) or false (zero). This expression

    is tested prior to each iteration of the loop. The loop terminates when it is false.

    increment is a statement that is executed after statements. statements is a sequence of statements. If there is only one statement then the

    braces may be omitted.

    Example:The following program will print the numbers 1 to 10 in reverse order

    #i ncl udemai n( ){

    i nt i ;f or ( i =10 ; i >=0 ; i - - )pr i nt f ( %d\ n, i ) ;

    }

    Example:The following program will print the sum of numbers from 1 to 10

    #i ncl ude

    mai n( ){

    i nt i , sum = 0 ;f or ( i =1 ; i

  • 7/27/2019 C Programming - Question Bank.pdf

    10/21

    10

    #i ncl udemai n( ){

    i nt i , sum = 0 ;f or ( i =2 ; i

  • 7/27/2019 C Programming - Question Bank.pdf

    11/21

    11

    do{

    st atement bl ock ;}whi l e ( l oop condi t i on) ;

    where

    statements is a sequence of statements. If there is only one statement then thebraces may be omitted.

    condition is an expression that can be true (nonzero) or false (zero). This expressionis tested after each iteration of the loop. The loop terminates when it is false.

    Example: The following program will print the first 10 natural numbers. The loop isexecuted 10 times.#i ncl udemai n( ){

    i nt i =1 ;do

    {pri nt f ( %d\ n, i ) ;i ++;} whi l e ( i

  • 7/27/2019 C Programming - Question Bank.pdf

    12/21

    12

    The if statement has one of two forms:

    i f ( condi t i on){t r ue- st atement s}

    ori f ( condi t i on){t r ue- st atement s}el se{f al se- st at ement s

    }

    where

    condition is an expression that can be true (nonzero) or false (zero).

    true-statements and false-statements are sequences of statements. If there is onlyone statement in a sequence then the surrounding braces may be omitted.

    The second form includes the else clause For both forms, true-statements are executed only if condition is true. For second

    form the false-statements are executed if condition is false.

    Example :The following statement is used to test whether a number entered by the useris positive or negative

    #i ncl ude voi d mai n ( ){

    i nt n ;pr i nt f ( "Ent er a non- zer o number ") ;scanf ( "%d", &n) ;i f ( n < 0)

    pr i nt f ( "number i s negat i ve") ;el sepr i nt f ( "The number i s posi t i ve") ;

    }

    Example:Finding the greater of two numbers

    #i ncl ude

    voi d mai n ( ){

    i nt a, b ;pr i nt f ( "Ent er a number ") ;scanf ( "%d" , &a) ;

    pr i nt f ( "Ent er anot her number ") ;scanf ( "%d" , &b) ;

    i f ( a>b)

  • 7/27/2019 C Programming - Question Bank.pdf

    13/21

    13

    pr i nt f ( "%d i s gr eat er t han %d", a, b) ;el sepr i nt f ( "%d i s gr eat er t han %d", a, b) ;

    }

    State four forms of the if statement and explain any two

    The simplest form of if statement is as follows:

    i f ( condi t i on)st atement ;

    The gener al f or m of t he i f st at ement i ncl udes t he el se cl ause. Thi s hast he f ol l owi ng f or m:

    i f (condi t i on)st atement ;

    el sest atement ;

    If the programmer wants to execute more than one statement at this point, they may begrouped using curly braces. Such statement group is called compound statement.

    i f ( condi t i on){

    st atement ;st atement ;

    }

    In most general form of the if statement is :

    i f ( condi t i on){st atement ;st atement ;

    }el se{

    st atement ;st atement ;

    }

    Example: The f ol l owi ng exampl e uses i f st at ement t o f i nd t he greatest of

    t hr ee number s ent ered by t he user .

    #i ncl ude mai n ( ){

    i nt a, b, c, bi g ;pr i nt f ( "Ent er t hr ee number s" ) ;scanf ( "%d %d %d", &a, &b, &c) ;

    i f ( a > b) && ( a > c)bi g = a ;

  • 7/27/2019 C Programming - Question Bank.pdf

    14/21

    14

    el se{

    i f ( b > c)bi g = b ;el sebi g = c ;

    }pr i nt f ( "l ar gest number i s %d", bi g)

    }

    Explain the nesting of if-statement

    The if statement can itself contain another if statement. This is known as nesting of if

    statement. The nested if may appear in a program as :

    i f ( condi t i on 1){i f ( condi t i on 2)st at ement 1;el sest at ement 2;}

    el se{i f ( condi t i on 3)st at ement 3;el sest at ement 4;}

    The main thing to remember about nested if is that the else statement always refers tothe nearest ifstatement within the same block.

    Example : This program uses the nested if to find greatest of three numbers

    #i ncl ude mai n ( ){

    i nt a, b, c, bi g ;pr i nt f ( "Ent er t hr ee number s" ) ;scanf ( "%d %d %d", &a, &b, &c) ;

    i f ( a > b){ i f ( a > c)

    bi g = a ;el sebi g = c;

    }el se{ i f ( b > c)

    bi g = b ;el sebi g = c ;

    }pr i nt f ( " l ar gest of %d, %d & %d = %d" , a, b, c, bi g) ;

  • 7/27/2019 C Programming - Question Bank.pdf

    15/21

    15

    }

    Explain the else-if ladder with example

    The construct of else-if ladderis shown below :

    i f ( condi t i on1)st at ement1 ;

    el se i f ( condi t i on1)st at ement1 ;

    el se i f ( condi t i on2)st at ement2 ;

    .

    .

    .el se i f ( condi t i onN)

    st at ement N ;el se st atement ;

    The conditional expressions are evaluated from top downward. When a true condition is

    found, the associated statement is executed, and rest of ladder is bypassed. If none ofconditions is true, then final else statement is executed.

    The final else acts as a default condition i.e.if all other conditions tests fail, then the last

    else statement is performed. If there is no final else and all other conditions are falsethen no action will take place.

    Example : Following example illustrates the of if-else-if ladder

    #i ncl ude mai n ( )

    { i nt n;pr i nt f ( "Ent er an i nt eger bet ween 1 and 5" ) ;scanf ( %d, n) ;i f ( n==1) pr i nt f ( "number i s one\ n" ) ;el se i f ( n==2) pr i nt f ( "number i s t wo\ n") ;el se i f ( n==3) pr i nt f ( "number i s t hr ee\ n") ;el se i f ( n==4) pr i nt f ( "number i s f our \ n") ;el se i f ( n==5) pr i nt f ( "number i s f i ve\ n") ;el se pr i nt f ( "You di dn' t f ol l ow t he r ul es") ;

    }

    Explain the ? : operator (Conditional Operator)C language uses a combination of ? and : for making two way decisions. This operator iscalled the conditional operator and it takes three operands.

    The general form of the conditional operator is :

    conditional expression ? expression1 : expression2

  • 7/27/2019 C Programming - Question Bank.pdf

    16/21

    16

    The conditional expression is first evaluated. If result is non-zero, then expression1 isevaluated and its value is returned. If result is zero, then expression2 is evaluatedand its value is returned.

    Example : Consider the following code :

    i f ( x>3)a = 5;el sea = 1;

    The same can be written as :

    a = ( x>3) ? 5 : 1 ;

    The conditional operator can be nested for more complex assignments. For example

    consider the following

    Salary = 4R+20 for R < 20= 150 for R = 20

    = 5R+90 for R > 20

    This can be written as:

    sal ary = ( R=20) ? 150: ( ( R

  • 7/27/2019 C Programming - Question Bank.pdf

    17/21

    17

    C PROGRAMS

    Write a C program to accept marks of three subjects, and then print the totaland average marks

    / * Progr am: Add t hr ee mar ks and pr i nt t ot al and aver age */#i ncl udemai n( ){

    i nt m1, m2, m3, t otal ;f l oat aver age;pr i nt f ( Ent er mar ks of t hr ee subj ect s) ;scanf ( %d %d %d, &m1, &m2, &m3) ;

    t ot al = m1 + m2 + m3;pr i nt f ( "t ot al mar ks i s %d", t ot al ) ;

    average = t ot al / 3;pr i nt f ( "aver age mar ks i s %d" , t ot al ) ;

    }

    Write a C program to accept a number and display it in octal and hexadecimalform

    / * Pr ogr am t o pr i nt oct al and hexadeci mal number s */#i ncl udemai n( ){

    i nt n;pr i nt f ( \ nEnt er a deci mal number : ) ;scanf ( %d, &n) ;

    pr i nt f ( \ nNumber i s %d i n deci mal , %o i n oct al , %x i n hexadeci mal ) ;}

    Write C program using conditional operator to determine whether year enteredthrough at the keyboard is a leap year or not

    / * Pr ogr am : To t est f or a l eap year */#i ncl udemai n( ){

    i nt year ;pr i nt f ( Ent er an year : ) ;

    scanf ( %d, &year) ;i f ( year %4 == 0 && year %100 ! = 0 | | year %400 == 0)pr i nt f ( year i s a l eap year ) ;el sepr i nt f ( year i s not a l eap year ) ;

    }

    Write a C program to find whether a number is odd or even/ * Progr am f or odd- even t est */#i ncl udemai n( )

  • 7/27/2019 C Programming - Question Bank.pdf

    18/21

    18

    {i nt n;pr i nt f ( Ent er an i nt eger : );scanf ( %d, &n) ;i f ( n%2 == 0)pr i nt f ( number i s even) ;el sepr i nt f ( number i s odd) ;

    }

    Write a C program to find whether number is divisible by 7 or not/ * Progr am : To t est di vi s i bi l i t y by 7 * /#i ncl udemai n( ){

    i nt n;pr i nt f ( Ent er an i nt eger : );scanf ( %d, &n) ;i f ( n%7 == 0)pr i nt f ( number i s di vi si bl e by 7) ;el se

    pr i nt f ( number i s not di vi si bl e by 7) ;}

    Write a C program to find the factorial of a number/ * Pr ogr am : To f i nd t he f act or i al of a number */#i ncl udemai n( )

    {i nt n, f act =1;pr i nt f ( Ent er a posi t i ve i nt eger : ) ;scanf ( %d, &n) ;f or ( i =1; i

  • 7/27/2019 C Programming - Question Bank.pdf

    19/21

    19

    el sepr i nt f ( "number i s not pr i me" ) ;

    }

    / * Progr am : Pr i me number t est */#i ncl ude mai n( ){

    i nt n, count ;

    pr i nt f ( Ent er a number ) ;scanf ( "%d" , &n) ;f or ( i =2; i 1)pr i nt f ( "not pr i me no. ") ;el sepr i nt f ( "pr i me") ;

    }

    Write a C program to print sum of digits in 3 digit numbers

    / * Progr am : Add di gi t s of number */#i ncl ude mai n( ){

    i nt n, u, t , h;pr i nt f ( Ent er a 3 di gi t i nt eger: ) ;scanf ( %d, &n) ;u = n%10;n = n/ 10;t = n%10;n = n/ 10;h = n%10;pr i nt f ( \ n Sum of di gi t s : %d, u+t +h) ;

    }

    Write a C program to reverse the three digit number entered as input throughkeyboard

    / * Progr am : Rever se 3 di gi t number */

    #i ncl ude mai n( ){

    i nt n, u, t , h;pr i nt f ( Ent er a 3 di gi t i nt eger: ) ;scanf ( %d, &n) ;u = n%10;n = n/ 10;t = n%10;n = n/ 10;

  • 7/27/2019 C Programming - Question Bank.pdf

    20/21

    20

    h = n%10;pr i nt f ( \ nRever sed number : %d%d%d, u, t , h) ;

    }

    Write a C program to reverse digits of a number entered as input throughkeyboard

    / * Progr am : Rever se di gi t s of any number */#i ncl ude

    mai n( ){

    i nt n, di gi t ;pr i nt f ( Ent er a 3 di gi t i nt eger: ) ;scanf ( %d, &n) ;pr i nt f ( \ nThe r ever sed di gi t s ar e: ) ;do{

    di gi t =n%10;pr i nt f ( %d, di gi t ) ;n = n/ 10;

    }whi l e ( n>0) ;pr i nt f ( \ n) ;

    }

    Write a C program using if else ladder to grade student according to followingrules.

    Marks Grade

    70 to 100 Distinction

    60 to 69 I class

    50 to 59 II class

    40 to 49 pass class0 to 39 fail

    / * Progr am : Gr adi ng of st udent s */#i ncl ude mai n ( ){

    i nt mar ks ;pr i nt f ( "Ent er mar ks\ n") ;scanf ( "%d", &marks) ;

    i f ( mar ks=70) pr i nt f ( " \ n Di st i nct i on" ) ;el se i f ( mar ks>=60) pr i nt f ( "\ n Fi r st cl ass") ;el se i f ( mar ks>=50) pr i nt f ( "\ n second cl ass" ) ;el se i f ( mar ks>=35) pr i nt f ( "\ n pass cl ass") ;el se pr i nt f ( "Fai l " ) ;

    }

    Write a C program to find sum of series : 1+3+5+7++N

    / * Pr ogr am : Sum of ser i es */

  • 7/27/2019 C Programming - Question Bank.pdf

    21/21

    #i ncl ude mai n ( ){

    i nt i , n, sum = 0 ;pr i nt f ( "Ent er val ue of n: ") ;scanf ( "%d", &n) ;

    f or ( i =0; i