Q.1. Multiple Choice of Answers.
1.1 Which of the following is the correct order of evaluation for the below expression?
a) */%+-=
b) =*/%+-
c) /*%-+=
d) *%/-+=
1.2 Which of the following is true for variable names in C
a) They can contain alphanumeric characters as well special characters.
b) It is not an error to declare a variable to be one of the keyboards (like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
1.3 What is the output of this C code ?
#include<stdio.h>
int main()
{
int x=2,y=0;
int z=y&&(y\=10);
printf(“%d\n”,z);
return 0;
}
a) 1
b) 0
c) undefined behavior due to order of
evaluation
d) 2
1.4 main()
{
int i=1;
while()
{
printf(“%d”,i++);
if(i>10)
break;
}}
a) The condition in while loop is must
b) There should be a semicolon in the while loop
c) The while loop should be replaced by for loop
d) No error
1.5 What is right way to initialize arrays ?
a) int num[6]={2,4,12,5,45,5};
b) int n{}={2,4,12,5,45,5};
c) int n{6}={2,4,12};
d) int n(6)={2,4,12,5,45,5};
1.6 What will be the output of the following program?
In C, if you pass an array as an argument to a function, what actually gets passed ?
a) Value of elements in array
b) First element of array
C) Base address of array
D) Address of the last element of array
1.7 #include<stdio.h>
void fun(int *ptr)
{
*ptr=30;
}
int main()
{
int y=20;
fun(&y);
printf(“%d”,y);
return 0;
}
The output of above program is
A. 20
B. 30
C. Compiler error
D. Runtime error
1.8 If the two strings are identical, then strcmp() function returns
a) -1
b) 1
c) 0
d) infinity
1.9 What is the maximum number of dimensions an array in C may have ?
a) 2
b) 8
c) 20
d) Theoretically no limits. the only practical limits are memory and compilers
1.10 Is the following statement declaration or definition: extern int i;
a) Declaration
b) Definition
c) Function
d) Error
Q.2. TRUE or FALSE.
2.1 The expressions *ptr++ and ++*ptr are same
FALSE
2.2 C provides the strcmp() function to compare strings
TRUE
2.3 A function cannot be defined inside another function.
TRUE
2.4 Goto can be used to jump from main function to other function.
FALSE
2.5 The default parameter passing mechanism is call by value.
TRUE
2.6 The expressions arr and &arr same for an array of 10 integers
FALSE
2.7 Memory allocation can be done by using keyword 'create'.
FALSE
2.8 The expressions int fun(int arr[]); and int fun(int arr[2]); are same.
TRUE
2.9 There can be two return statements inside a function.
FALSE
2.10 Break statement can be used to exit a loop.
TRUE
FALSE
2.2 C provides the strcmp() function to compare strings
TRUE
2.3 A function cannot be defined inside another function.
TRUE
2.4 Goto can be used to jump from main function to other function.
FALSE
2.5 The default parameter passing mechanism is call by value.
TRUE
2.6 The expressions arr and &arr same for an array of 10 integers
FALSE
2.7 Memory allocation can be done by using keyword 'create'.
FALSE
2.8 The expressions int fun(int arr[]); and int fun(int arr[2]); are same.
TRUE
2.9 There can be two return statements inside a function.
FALSE
2.10 Break statement can be used to exit a loop.
TRUE
Q.3. Match words and phrases
SR NO. | X | Y(Ans.) |
1 | For is | an entry controlled loop |
2 | Float | is another name for a variable |
3 | Reference | function all C programs must contain |
4 | # define directive | defines a macro |
5 | enum is | is a keyword |
6 | Function | cannot return array |
7 | Logical OR Operator | || |
8 | *has | more priority than + |
9 | Real | user defined data type |
10 | Comments in C | /* */ |
Q.4. Fill in the blank
4.1 An array elements are always stored in ___Sequentia_________ order.
4.2 The value obtained in the function is given back to main by using ___Return____ keyword.
4.3 By default, storage class of local variable is ____Auto____
4.4 ____Infinite_____ loop can be terminated using break statement.
4.5 ___Pointer_______ is a variable which holds the address of another variable.
4.6 Variables that are alive and active throughout the program are called ___Global____ variables.
4.7 ____do-while _____ is an exit control loop.
4.8 A pointer that does not point to any data object is ___Null______
4.9 The conditional operator (?:) is a ______Ternary_____
4.10 In ____Union _____ all elements are stored in the same memory location.
4.2 The value obtained in the function is given back to main by using ___Return____ keyword.
4.3 By default, storage class of local variable is ____Auto____
4.4 ____Infinite_____ loop can be terminated using break statement.
4.5 ___Pointer_______ is a variable which holds the address of another variable.
4.6 Variables that are alive and active throughout the program are called ___Global____ variables.
4.7 ____do-while _____ is an exit control loop.
4.8 A pointer that does not point to any data object is ___Null______
4.9 The conditional operator (?:) is a ______Ternary_____
4.10 In ____Union _____ all elements are stored in the same memory location.
Options.
A. Return
B. Global
C. Sequential
D. Infinite
E. do-while
F. Union
G. Null
H. Ternary
I. integer
J. Pointer
K. Auto
L. prototype
M. Argument
B. Global
C. Sequential
D. Infinite
E. do-while
F. Union
G. Null
H. Ternary
I. integer
J. Pointer
K. Auto
L. prototype
M. Argument
Long Questions
(Answer any four questions)
Q.5.
(A) Compare the use of switch statements with the use of nested if statements. Which is more convenient?
Ans:-
(B) Explain break, continue and goto statements with example
}
Output:
(C) Write a program to print all prime numbers from 1 to 100. Use nested loops, break or continue statement wherever necessary.
Ans:-
Difference Between Switch Case and Else If Ladder
While programming, a number conditions come and a number of decisions need to be taken by a programmer. For decision making in C programming, the statements such as if, if..else, else if, switch case etc. have defined in the standard C library. In case of multi-conditional processing, basically there come two statements in the choice of a programmer; they are switch case and else if ladder. In this post, I have presented the features and applications of the two statements, and the difference between switch case and else if ladder.
Though the function of switch case and else if ladder is same, there are a number of remarkable difference between switch case and else if ladder in many aspects such as memory consumption, speed of processing, variable requirement, comfort in programming etc. Appropriate choice between switch case and if else ladder is essential for the sake of ease, comfort, accuracy and efficient programming.
Else If Ladder:
else if statement can be defined as a control statement which controls the statement(s) to be executed on the basis of some conditions. Whenever the else if statement is used, the compiler or interpreter initially checks the condition whether it is true or false and if the condition is found to be true then, the corresponding statements are executed. If the condition is found to be false, it continues checking the next else if statement until the condition comes to be true or the control comes to the end of the else if ladder.
The syntax of else if ladder can be represented as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | if( condition-1) statement-1; else if (condition-2) statement-2; else if (condition-3) statement-3; else if (condition-4) statement-4; else if (condition-n) statement-n; else default statement; |
Flowchart for else if ladder
Features of else if ladder:
· It evaluates an expression and then, the code is selected based on the true value of evaluated expression.
· Each else if has its own expression or condition to be evaluated.
· The variable data type used in the expression of else if is either integer or character.
· The decision making of the else if is dependent on zero or non-zero basis.
Switch Case:
The switch case statement is similar to the else-if ladder as it provides multiple branching or multi-conditional processing. But, the basic difference between switch case and else if ladder is that the switch case statement tests the value of variable or expression against a series of different cases or values, until a match is found. Then, the block of code with in the match case is executed. If there are no matches found, the optional default case is executed.
The syntax of switch case can be represented as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | switch(expression) { case constant-1 block-1; break; case constant-2 block-2; break; case constant-3 block-3; break; case constant-n block-n; break; default: } statement-x; |
Flowchart for switch case
Features of switch case:
· The switch case statement evaluates the value of an expression and a block of code is selected on the basis of that evaluated expression.
· Each case refers back to the original expression.
· The data type that can be used in switch expression is integer type only.
· Each case has a break statement.
· The switch case takes decision on the basis of equality.
Difference between switch case and else if ladder:
So, after going through the two control statements in brief, here are the main difference between switch case and else if ladder:
1. In else if ladder, the control goes through the every else if statement until it finds true value of the statement or it comes to the end of the else if ladder. In case of switch case, as per the value of the switch, the control jumps to the corresponding case.
2. The switch case is more compact than lot of nested else if. So, switch is considered to be more readable.
3. The use of break statement in switch is essential but there is no need of use of break in else if ladder.
4. The variable data type that can be used in expression of switch is integer only where as in else if ladder accepts integer type as well as character.
5. Another difference between switch case and else if ladder is that the switch statement is considered to be less flexible than the else if ladder, because it allows only testing of a single expression against a list of discrete values.
6. Since the compiler is capable of optimizing the switch statement, they are generally considered to be more efficient. Each case in switch statement is independent of the previous one. In case of else if ladder, the code needs to be processed in the order determined by the programmer.
7. Switch case statement work on the basis of equality operator whereas else if ladder works on the basis of true false( zero/non-zero) basis.
codewithc.com
codewithc.com
Break, continue and goto statements
The
Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used.
break;
continue;
and goto;
statements are used to alter the normal flow of a program.Loops perform a set of repetitive task until text expression becomes false but it is sometimes desirable to skip some statement/s inside loop or terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used.
break statement
In C programming, break statement is used with conditional if statement.
The break is used in terminating the loop immediately after it is encountered.
it is also used in switch...case statement. which is explained in next topic.
The break is used in terminating the loop immediately after it is encountered.
it is also used in switch...case statement. which is explained in next topic.
Syntax:
break
;
The break statement can be used in terminating loops like
for
, while
and do...while
Example:
//Write a C Program Which use of break statement.
void main()
{
int num, sum=
0;
int i,n;
printf(
"Note: Enter Zero for break loop!\n");
printf(
"Enter Number of inputs\n");
scanf(
"%d",&n);
for(i=
1;i<=n;++i){
printf(
"Enter num%d: ",i);
scanf(
"%d",&num);
if(num==
0) {
break;
/*this breaks loop if num == 0 */
printf(
"Loop Breaked\n");
}
sum=sum+num;
}
printf(
"Total is %d",sum);
getch();
}
Output:
continue statement
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statement is used.
Syntax:
continue
;
Just like break, continue is also used with conditional if statement.
Example:
//Write a C Program Which use of continue statment.
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
printf("value of a: %d\n", a);
a++;
} while( a < 20 );
return 0;
Output:
Command Prompt
goto statement
In C programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.
Syntax:
goto
label;
.............
.............
.............
label:
statement;
In this syntax,
When, the control of program reaches to goto statement, the control of the program will jump to the
label
is an identifier.When, the control of program reaches to goto statement, the control of the program will jump to the
label:
and executes the code below it.Example:
//Write a C Program Which Print 1 To 10 Number Using goto statement.
void main()
{
int i=
1;
clrscr();
count:
//This is Label
printf(
"%d\n",i);
i++;
if(i<=
10) {
gotocount;
//This jumps to label "count:"
}
getch();
}
Output:
Command Prompt
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Note:
Though goto statement is included in ANSI standard of C, use of goto statement should be reduced as much as possible in a program.
Reasons to avoid goto statement
Though, using goto statement give power to jump to any part of program, using goto statement makes the logic of the program complex and tangled.
In modern programming, goto statement is considered a harmful construct and a bad programming practice.
The goto statement can be replaced in most of C program with the use of break and continue statements.
In fact, any program in C programming can be perfectly written without the use of goto statement.
All programmer should try to avoid goto statement as possible as they can.
Q.6.
(A) Explain the difference between = and == operator with example.
(B) What will be the output of the following program segment? main() { int x=3,y=5; if(x==3) printf(“\n%d”,x); else printf(“\n%d”,y); }
(C) Write a program to find the sum of the digits of a number
(B) What will be the output of the following program segment? main() { int x=3,y=5; if(x==3) printf(“\n%d”,x); else printf(“\n%d”,y); }
(C) Write a program to find the sum of the digits of a number
Q.7.
(a) What is call by value and call by reference ? Write a program to swap two numbers using call by
value (b) What do you understand by local, global and static variables ? Explain (c) Compare the
lifetime, Scope, Initial value and storage place of all storage classes.
value (b) What do you understand by local, global and static variables ? Explain (c) Compare the
lifetime, Scope, Initial value and storage place of all storage classes.
Q.8.
a) Write a program to add a new node to the beginning of a linked list and to the end of inked list.
b)What is a file in C? Discuss various modes in which a file can be opened. Also discuss types of files.
b)What is a file in C? Discuss various modes in which a file can be opened. Also discuss types of files.
Q.9.
a) Write a note on pointers and its uses. What do you mean by refreshing and de-refreshing of a pointer variable.?
Ans:-
किसी integer data type variable का address hold करना हैतो pointer भी उसीdata type का होगा जिसdata type का variable हो, मतलबvariable int है तो pointer भी int हीहोगा, अगर variable char होतो pointer variable भी char ही होगा| Pointer किसी की value hold करके नहींरखता, बल्कि सिर्फउसका address hold करके रखताहै और उसaddress से ही pointer के साथvariable के value को print किया जाताहै |
b) Explain any five string handling functions in detail with example.
Ans:-
हर एक Variable का एकMemory Address होता है औरMemory Address देखने के लिएProgrammer को variable से पहले'&'(address operator) लगानापड़ता है |
for eg.
int a ;
printf("Address of a : %d", &a);
Output:
Address of a : 6356748 // 6356748 is address of variable
इसीmemory address की मदद सेVariable की value को access किया जाताहै , इसी को'Pointers' कहते है |
Pointer किसीदूसरे variable का address hold करके रखताहै |
हर एक variable का एकaddress होता है | जबvariable declare होता है तभीउसका एक memory location परवो store होता है|
For Example
int a = 10;
Variable_or_Location_name | a | |
Variable_value | 10 | Location |
Memory Address | 6356748 |
Syntax for Pointer Declaration - (data_type *pointer_name;)
Example for Pointer Declaration
int *ptr1; // integer pointer variable
char *ptr2; // character pointer variable
float *ptr3; // float pointer variable
double *ptr4; // double pointer variable
किसी integer data type variable का address hold करना हैतो pointer भी उसीdata type का होगा जिसdata type का variable हो, मतलबvariable int है तो pointer भी int हीहोगा, अगर variable char होतो pointer variable भी char ही होगा| Pointer किसी की value hold करके नहींरखता, बल्कि सिर्फउसका address hold करके रखताहै और उसaddress से ही pointer के साथvariable के value को print किया जाताहै |
Full Example for Pointer
#include <stdio.h>
int main(){
int a = 10;
int *ptr; // Integer Pointer Variable
ptr = &a; //address of a stored in ptr
printf("Value of a is: %d\n", a);
printf("Address of a is: %u", ptr);
return 0;
}
Output :
Value of a is: 10
Address of a is: 6356744
Note : Pointer काaddress साधारणतः Hexadecimal Numbers होते है, परकुछ compiler अपना address integer में print करते है| Variable का address print कराने के लिए'%x' या '%u' इन Format Specifiers का इस्तेमालकरते है |
Pointers के बारे में और जानते है |
Pointers के दो Methods है |
1. Referencing Operator ( & )
2. Dereferencing Operator ( * )
1. Referencing Operator
Referencing मतलबकिसी दूसरे variable काaddress hold करके रखना होताहै |
हर variable का address hold करने केलिए जिसका address hold करनाहै और जिसPointer variable में hold करना है, तो उन दोनोंका data type same होना चाहिए|
for eg.
int a = 10;
int *ptr;
ptr = &a; //address of a stored in ptr
2. Dereferencing Operator
Dereferencing मेंasterisk ( * ) का इस्तेमाल करते परpointer में इसे Dereferencing Operator भी कहतेहै |
Dereferencing मेंpointer में store किये हुएvalue को access किया जाताहै |
int a = 10;
int *ptr;
ptr = &a;
int b = *ptr; // ptr means dereferencing
printf("Value of a : %d", b);
Output :
Value of a : 10
Ans:-
Strings characters का समूहहोता है |
Strings ये One-dimensional array होता है, जिसमे सिर्फ characters होते है |
String का आखिरी character 'NULL'(\0) होता है |
अगरपूरा string लिखना हो तो उसे double quotes ( " " ) मेंलिखा जाता है | अगर एक-एक character को लिखना हो तो उसे single quotes ( ' ' ) में लिखा जाता हैl
String का data type character (char) होता है |
Example for Single Character String
char str1[6] = {'H', 'e', 'l', 'l', 'o', ' \0'};
स्ट्रिंग हैंडलिंग क्रियाओ का ५ list,
1. strcat() - String Function
strcat ये String का एकFunction है | एक String से दूसरेString को जोड़ा जाता है |
Syntax for strcat() - strcat(destination_string, source_string);
Destination_string - ये वो parameter है जिसके साथsource का string जोड़ा जाता है| String के आखिर में null character (\0) होता है| Source string destination string के साथ जुड़ते समय उसकोremove कर देता है |
Source_string - ये वो parameter है जिसकाstring destination string के साथ आखिर में जोड़ा जाता है |
किसी integer value को एकvariable को दूसरे variable से जोड़ना हो तोarithmetic operator (+) का use नहीं कर सकते|
for e.g.
int num1 = 5 ;
int num2 = 5 ;
int num3 = num1 + num2 ;
इनका output 55 मिलना चाहिए लेकिन इनकाoutput 10 मिलेगा |
किसी char को भी एक दूसरे सेarithmetic operators के साथ नहीं जोड़ा जा सकता |
Code :
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[20] = "Welcome";
char str2[20] = " Friend";
strcat(str1, str2);
printf("Concatenation String : %s", str1);
return 0;
}
Output:
Concatenation String : Welcome Friend
[Process completed - press Enter]
2. strcmp() - String Function
दो String कोCompare किया जाता है | ये case-sensetive है|
Syntax for strcmp() - strcmp(string1, string2);
string1 -ये वोString है जिसके साथString2 को Compare किया जाता है|
string2 - ये वोString है जिसके साथString1 को Compare किया जाता है|
अगर दोनों string एक जैसे होते है तो ये'0' return करता है |अगर दोनों string अलग-अलग होते है तो'1' या '-1' return करता है|
Note : येstrcmp() function case-sensitive है | इसमें 'h' और'H' ये दोनों अलग-अलग है|
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello" ;
char str2[] = "World" ;
int a = strcmp(str1, str2) ;
printf ("%d\n", a) ;
int b = strcmp(str1, "Hello") ;
printf ("%d\n", b) ;
int c = strcmp(str1, "hello") ; // strcmp is case-sensitive
printf ("%d\n", c) ;
return 0;
}
Output:
-120
0
-128
[Process completed - press Enter]
3. strcpy() - String Function
दो String कोCompare किया जाता है | ये case-sensetive नहीं है|
Syntax for strcpy() - strcpy(destination_string, source_string);
destination_string - ये वो parameter है जिसपरsource के string कीvalue copy की जाती है | अगर destination string पर कोईvalue भी हो तो वो overwrite हो जाती है|
source_string - ये वो parameter है जिसकीvalue destination पर copy की जाती है|
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Welcome";
char str2[20] = "Friend";
printf("Value of str2 = %s\n", str2 );
strcpy(str2, str1);
printf("Copy str1 to str2 = %s", str2 );
return 0;
}
Output:
Value of str2 = Friend
Copy str1 to str2 = Welcome
[Process completed - press Enter]
4. strlen() - String Function
String की Length निकाली जाती है|
Syntax for strlen() - strlen(string);
string - ये एकnormal string है, जिसकी length निकली जाती है|
strlen से निकला हुआoutput integer value ही होती है ,ये किसी दूसरे integer variable में भीstore करके रख सकते है |
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str[30] = "Hello World";
int length ;
printf("String : %s\n", str);
length = strlen(str);
printf("Length of str is %d", length);
return 0;
}
Output:
String : Hello World
Length of str is 11
[Process completed - press Enter]
5. strupr() - String Function
Lowercase के Characters कोUppercase में convert किया जाता है|
Syntax for strupr() - strupr(string);
string - ये वोstring है जिसको Uppercase मेंconvert किया जाता है |
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char str[50] = "hello world";
int lwr ;
printf("String : %s\n", str );
printf("Uppercase of str = %s", strupr(str));
return 0;
}
Output:
String :hello world
Uppercase of str = HELLO WORLD
Post a Comment
Leave a Reply