Download - c programming 10mark Questions
-
8/9/2019 c programming 10mark Questions
1/42
-
8/9/2019 c programming 10mark Questions
2/42
else printf!) ot %ligible to *ote+";------
) ested if2else statementhen a series of decisions are in*ol*ed, we may ha*e to use more than one if-.elsestatements, in nested form as follows.
If( test condition "#
if !test condition /"#statement- ;$else#statement-/;$
$else#statement-0;$statement-x;
%x #rogram123electing the largest of three *alues21main!"#float 4, 5, 6;
printf!)%nter three *alues 7n+";scanf!)89f 9f 9f+,:4, :5, :6";
printf!)7n argest *alue is(+";if!4 < 5"#
if!4 < 6" printf!)9f 7n+,4";
else printf!)9f 7n+,6";
$else#
if!6 < 5" printf!)9f 7n+,6";
else
-
8/9/2019 c programming 10mark Questions
3/42
printf!)9f 7n+,5";$$
=>T?>T
%nter three *alues(@ A /Bargest *alue is /B
3) else , if 4adder
The general form isIf !condn "3tatement- ;else if !condn /"statement-/;else if !condn 0"statement-0;&&&.&&&.else if !condn n"statement-n;elsedefault statement;statement-x;
%C #rogram12>se of else if ladder21main!"#int units;float charges;
printf!)%nte units consumed 7n+";scanf!)9d +, :units ";if!units DE /FF"charges E F.@ 2 units;else if!units DE BFF"charges E FFG F.H@ 2 !units /FF"else if !units DE HFF"charges E /0F G F.A 2 !units BFF"elsecharges E 0JF G !units HFF";
printf!)7n charges E9./f 7n+, charges";$=>T?>T%nter units consumed @Fcharges E [email protected]
-
8/9/2019 c programming 10mark Questions
4/42
5) s6itch statement
3witch statement is used for complex programs when the number of alternati*es increases. Theswitch statement tests the *alue of the gi*en *ariable against the list of case *alues and when amatch is found, a block of statements associated with that case is executed.The general form of switch statement is
s6itch !expression"#case *alue- (
block-%reak'case *alue-/(
block-/%reak'--.--.default7default-block %reak'$statement-x;
%g(------switch !grade" # case L4L( printf!MNou did greatOM"; break; case L5L( case L6L( case LPL( printf!MNou passed.M";
break; case L'L( printf!MNou failedOM";
break; default( printf!) rong choice+";
break;
$
-
8/9/2019 c programming 10mark Questions
5/42
+. Explain the 4oop control statements or Iteration statements of C in detail.
4oop7 4 loop is defined as a block of statements which are repeatedly executed forcertain number of times
The 6 language pro*ides for three loop constructs for performing loop operations. They are QThe 6hile statement QThe do statement QThe for statement
89E :9I4E S8;8EME 8
The basic format of the 6hile statement is
6hile !test condition"#
body of the loop$
The 6hile is an entrycontrolled loop statement. The test-condition is e*aluated and if thecondition is true, then the body of the loop is executed. 4fter execution of the body, the test-condition is once again e*aluated and if it is true, the body is executed once again. This processof repeated execution of the body continues until the test-condition finally becomes false and thecontrol is transferred out of the loop.%g(----------------------sum E F;n E ;6hile !n DE F"#sum E sum G n2 n;n E n G ;$
printf!)sum E 9d 7n+,sum";----------------------89E
-
8/9/2019 c programming 10mark Questions
6/42
3ince the test-condition is e*aluated at the bottom of the loop, the do-..6hile construct pro*idesan exit-controlled loop and therefore the body of the loop is always executed at least once.%g(-----------do
# printf!)Input a number7n+";number E getnum!";$6hile !number < F";-----------
89E >=? S8;8EME 8Simple @forA 4oopsThe for loop is another entry-controlled loop that pro*ides a more consise loop control structure.The general form of the for loop is for !initialiRation ; test-condition ; increment"#
body of the loop$
The execution of the for statement is as follows( Q Initialization of the control variables is done first. QThe *alue of the control *ariable is tested using the test-condition. If the condition is true , the
body of the loop is executed; otherwise the loop is terminated and the execution continues withthe statement that immediately follows the loop.
Qhen the body of the loop is executed, the control is transferred back to the for statementafter e*aluating the last statement in the loop. ow, the control *ariable is either incremented ordecremented as per the condition.
%g " for(x 0' x B ' x x D 1)
!#rintf(& d&*x)'
printf(" n&)'The multiple arguments in the increment section are possible and separated by commas.
%g /"sum 0'
for(i 1' i B +0 FF sum B100' DDi)!
sum sum D i'printf(" d d n&*sum)'
-
8/9/2019 c programming 10mark Questions
7/42
esting of >or 4oops6 allows one for statement within another for statement.--------------------for!i E ; i D F; GG i"
#------------------
for! E ; O E @; GG "#
--------- ---------$
------------------$
--------------------%g(22222222222222222222for(ro6 1' ro6 B ?=:M;G' DDro6)!for(column 1' column B C=4M;G' DDcolumn)!H ro6 column'printf(" 3d&* H)'
printf(" n&)'
22222222222222222222
. Explain the unconditional control statements of C in detail.
-
8/9/2019 c programming 10mark Questions
8/42
(1) 89E J=8= S8;8EME 8
6 supports the goto statement to branch unconditionally from one point of the program toanother. The goto reSuires a label in order to identify the place where the branch is to be made. 4
la%el is any *alid *ariable name and must be followed by a colon.The general from is
goto label 2222222222 2222222222la%el7 statement;=
la%el7
statement; ---------- 22222222222goto labelote7 4 label can be anywhere in the program, either before or after the goto label;statement.
%g(main!"#double x, y;read7 scanf!)9f+,:x"; if!x D F" goto read' y E sSrt!x"; printf!)9f 9f 7n+,x, y"; goto read'$
(+) 8he " %reak & Statement7
4 break statement terminates the execution of the loop and the control is transferred tothe statement immediately following the loop.
the break statement is used to terminate loops or to exit from a switch. It can be used within a for, while, do-while, or switch statement.
Example 17------s6itch(i)
-
8/9/2019 c programming 10mark Questions
9/42
!case 17printf("SundaH&)'case +7printf("MondaH&)' %reak'default7
printf!)wrong choice+"; break;$
Uere if you select case / only Vonday is printed since break occurs and program control isterminated from switch but if case is selected you will get the output as 3unday Vonday.
Example +7------
for(i 1'iB10'iDD)! printf(" d* i)'
if(i 5) %reak'
This code will print / 0 B @ and when iE@ it breaks or exits the for loop and go to the nextstatement.4 break statement terminates the execution of the loop and the control is transferred to thestatement immediately following the loop.
The break statement is used to terminate loops or to exit from a switch. It can be used within a for, while, do-while, or switch statement.
( ) 8he " continue " Statement7 The continue statement is used to bypass the remainder of the current pass through a
loop. The loop does not terminate when a continue statement is encountered. Instead, the remaining loop statements are skipped and the computation proceeds directly
to the next pass through the loop. The continue statement can be included within a while , a do-while , a for statement.
Example 7
-
8/9/2019 c programming 10mark Questions
10/42
------for(i 1'iB/'iDD)! if(i 5)
continue' printf(" d* i)'
Uere the output is / 0 B H K. when i E@ due to continue statement it goes to next iteration it doesnot print @ and increments i.
3. :hat are storage classesK
-
8/9/2019 c programming 10mark Questions
11/42
There are four storage classes in 6(
1. ;utomatic storage class
The features of *ariables are as follows
Storage2 Vemory
-
8/9/2019 c programming 10mark Questions
12/42
-
8/9/2019 c programming 10mark Questions
13/42
Y 4 data type is 4 set of *alues 4 P 4 set of operations on those *alues
Y 4 data type is used to Identify the type of a *ariable when the *ariable is declared Identify the type of the return *alue of a function Identify the type of a parameter expected by a function
-
8/9/2019 c programming 10mark Questions
14/42
grossE [email protected];3ymbol E LaL;return !F";$ 11 %nd main
Peri*ed Pata TypesY ;rraH a finite seSuence !or table" of *ariables of the same data typeY String an array of character *ariablesY Structure a collection of related *ariables of the same and1or different data types. The
structure is called a record and the *ariables in the record are called members or fields>ses of Peri*ed Pata Typesint TableZ/F[;char 3ymbolsZ[ E MUello orldM;struct operation # double eading; int temperature; float speed; char action; $; 11 %nd structstruct operation c;
. Explain the $arious operators in C in detail.
-
8/9/2019 c programming 10mark Questions
15/42
4n operator is a symbol that tells the compiler to perform specific mathematical or logicalmanipulations. 6 language is rich in built-in operators and pro*ides the following types ofoperators(
4rithmetic =perators
elational =perators
ogical =perators
5itwise =perators
4ssignment =perators
=ther =perators
Assignment Operator
int x ;x = 20 ;
lvalue -- left hand side of an assignment operationrvalue -- right hand side of an assignment operation
Arithmetic Operators
+ - * / --- same rules as mathematics with * and / being evaluatedbefore + and -.% -- modulus / remainder operator
For Example :-int a = 5, b = 2, x ;float c = 5.0, d = 2.0, f ;
x = a / b ; // integer division, x = 2.f = c / d ; // floating point division, f = 2.5.
x = 5 % 2 ; // remainder operator, x = 1.
x = 7 + ! " / 2 # 1 ;// x=15,! and / eval$ated a ead of + and# .
ote that parentheses ma! be used to clarif! or modif! the evaluation ofexpressions of an! t!pe in " in the same wa! as in normal arithmetic.
-
8/9/2019 c programming 10mark Questions
16/42
Increment and Decrement Operators
#here are two special unar! operators in "$ ncrement ++$ and &ecrement-- $ which cause the variable the! act on to be incremented or decrementedb! ' respectivel!.
For Example :-x++ ; /! e&$ivalent to x = x + 1 ; !/
++ and -- can be used in pre(x or post(x notation. n pre(x notation thevalue of the variable is either incremented or decremented and is then readwhile in post(x notation the value of the variable is read (rst and is thenincremented or decremented.
For Example :-int i, ' = 2 ;
i = ++ ' ; /! prefix (# i as val$e , ' as val$e !/i = '++ ; /! postfix (# i as val$e , ' as val$e ) !/
Special Assignment Operators
)an! " operators can be combined with the assignment operator asshorthand notation
For Example :-x = x + 10 ;
can be replaced b!x += 10 ;
imilarl! for #=, !=, /=, %= $ etc.
#hese shorthand operators improve the speed of execution as the! re uirethe expression$ the variable x in the above example$ to be evaluated oncerather than twice.
Relational Operators
#he full set of relational operators are provided in shorthand notation
* *= = == =
For Example :-if - x == 2
printf- x is e&$al to 2 n ;
-
8/9/2019 c programming 10mark Questions
17/42
Logical Operators
,, -- ogical & -- ogical 01
2 -- ogical 0#
For Example :-if - x *= 0 x 10 printf- x is greater t an or e&$al to 3ero and less t anten. n ;
NB : #here is no 3oolean t!pe in " so #14E and F E are deemed to havethe following meanings.
F E -- value 5ero #14E -- an! non-5ero value but ' in the case of in-built
relational operations
For Example :-6 7 ' -- #14E so expression has value '6 7 8 -- F E so expression has value 9i 6 7 ' ; -- relation is #14E -- has value '$ i is assigned value
'
NB : Ever! " expression has a value. #!picall! we regard expressions li
-
8/9/2019 c programming 10mark Questions
18/42
-
8/9/2019 c programming 10mark Questions
19/42
For Example :-1011 0010 -17
0011 1100 -"= 1000 1110 -1)2
Shi t Operators, !! and ""RULE : #hese move all bits in the operand left or right b! a speci(ed numberof places.
S#nta$ : variable > number of places
For Example :-2 2 =
i.e.
9999 99'9 becomes 9999 '999NB : shift left b! one place multiplies b! 6
shift right b! one place divides b! 6
Si%eo Operator
#he si5eof operator gives the amount of storage$ in b!tes$ associated with avariable or a t!pe.
S#nta$ sizeof ( expression )
For Example :-int x , si3e ;
si3e = si3eof - x ;printf- 8 e integer x re&$ires %d b9tes on t is mac ine , si3e ;
-
8/9/2019 c programming 10mark Questions
20/42
N. Explain the structure of C program in detail.
4 6 program consists of one or more functions or code modules. These are essentially groups ofinstructions that are to be executed as a unit in a gi*en order and that can be referenced by auniSue name. %ach 6 program must contain a main() function. This is the first function calledwhen the program starts to run. ote that while MmainM is not a 6 keyword and hence notreser*ed it should be used only in this context.
4 6 program is traditionally arranged in the following order but not strictly as a rule.
Function prototypes and global datadeclarations
The main ! function
Function definitions
6onsider first a simple 6 program which simply prints a line of text to the computer screen. Thisis traditionally the first 6 program you will see and is commonly called the )Uello orld+
program for ob*ious reasons.
-
8/9/2019 c programming 10mark Questions
21/42
\include Dstdio.hT.txt+, )r+";
p/ Efopen!)=>T?>T.txt+, )w+";&&..&&..
fclose!p ";
fclose!p/";
putc(c*fp1)'
-
8/9/2019 c programming 10mark Questions
33/42
c ( a character *ariable fp ( pointer to file opened with mode 6 puts a character into file.
c getc(fp+)'
c ( a character *ariable fp/ ( pointer to file opened with mode r reads a character from file.
file pointer mo*es by one character position after e*ery getc!" and putc!"
getc!" returns end-of-file marker %=' when file end reached
C #rogram to readQ6rite using getcQputc
Rinclude Bstdio.h
*oid main!"
#
'I % 2fp ;
char c;f E fopen!)I ?>T+, )w+"; 12 open file for writing 21
while!!cEgetchar!"" OE %='" 12get char from keyboard until 6T -X21 putc!c,f "; 12write a character to I ?>T 21
fclose!f "; 12 close I ?>T 21
f Efopen!)I ?>T+, )r+"; 12 reopen file 21while!!cEgetc!f ""OE%='" 12read character from file I ?>T21
printf!)9c+, c"; 12 print character to screen 21
fclose!f ";$ 12end main 21
C #rogram to ;ppend the Content of >ile at the end of ;nother >ile\include Dstdio.h %"; $ while !!ch E fgetc!fsring "" OE %='" fputc!ch, ftemp"; while !!ch E fgetc!fsring/" " OE %='" fputc!ch, ftemp"; printf!MTwo files merged 9s successfully.7nM, file0"; fclose!fsring "; fclose!fsring/"; fclose!ftemp"; return F;$
C program to copH one file contents into another.
\include Dstdio.h "
-
8/9/2019 c programming 10mark Questions
35/42
-
8/9/2019 c programming 10mark Questions
36/42
13. :rite a program to implement multiplication of + matrices.
\include Dstdio.h<
int main!"# int m, n, p, S, i, , k, sum E F; int firstZ F[Z F[, secondZ F[Z F[, multiplyZ F[Z F[; printf!M%nter the number of rows and columns of first matrix n M";
scanf!M9d9dM, :m, :n"; printf!M%nter the elements of first matrix n M";
for ! i E F ; i D m ; iGG " for ! E F ; D n ; GG "
scanf!M9dM, :firstZi[Z ["; printf!M%nter the number of rows and columns of second matrix n M";
scanf!M9d9dM, :p, :S";
if ! n OE p " printf!MVatrices with entered orders canLt be multiplied with each other. n M"; else # printf!M%nter the elements of second matrix n M";
for ! i E F ; i D p ; iGG " for ! E F ; D S ; GG " scanf!M9dM, :secondZi[Z [";
for ! i E F ; i D m ; iGG " # for ! E F ; D S ; GG " # for ! E F ; D p ; GG " # sum E sum G firstZi[Zk[2secondZk[Z [;
$ multiplyZi[Z [ E sum;
sum E F; $ $
printf!M?roduct of entered matrices(- n M";
-
8/9/2019 c programming 10mark Questions
37/42
for ! i E F ; i D m ; iGG "
# for ! E F ; D S ; GG " printf!M9d t M, multiplyZi[Z [";
printf!Mn M"; $ $
return F;$
-
8/9/2019 c programming 10mark Questions
38/42
15. Explain the concept of structures 6ith help of examples.
6 3tructure is a collection of different data types which are grouped together and each element ina 6 structure is called member.
If you want to access structure members in 6, structure *ariable should be declared.
Vany structure *ariables can be declared for same structure and memory will beallocated for each separately.
It is a best practice to initialiRe a structure to null while declaring, if we dont assign any*alues to structure members.
Difference between C variable, C array and C structure:
4 normal 6 *ariable can hold only one data of one data type at a time.
4n array can hold group of data of same data type.
4 structure can hold group of data of different data types
Pata types can be int, char, float, double and long double etc.
SHntax7
3truct tag]name#data type *ar]name ;data type *ar]name/;data type *ar]name0;$; struct tag]name
Example7
This program is used to store and access )id, name and percentage+ for one student. e can alsostore and access these data for many students using array of structures.
\include Dstdio.h