Professional Documents
Culture Documents
6102 C Language Mca MCQ
6102 C Language Mca MCQ
6102 C Language Mca MCQ
d) None of these.
Ans: a
Ans: a
Ans: b
5. C is a ___ language
a) High Level
pg. 1
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
b) Low Level
c) Middle Level
d) Machine Level
Ans: c
Ans: d
Ans: b
Ans: d
Ans: d
pg. 2
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Ans: d
Ans: a
12. Character constants should be enclosed between ___
a) Single quotes
b) Double quotes
c) Both a and b
d) None of these
Ans: a
Ans: b
14.The __________ consists of an equal sign and an expression, which is usually a single
constant.
a) Initializer
b)Variable
c) Constant
d) None of these
Ans: a
15. A single declaration statement can contain variables of different types. (True/False)
Ans: False
Ans: Underscore
pg. 3
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
b) 4 bytes
c) 8 bytes
d) 16 bytes
Ans: c
21.Using C language programmers can write their own library functions. (True/False)
Ans: True
Ans:True
Ans: False
pg. 4
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
28. The operator & is used for
a) Bitwise AND
b) Bitwise OR
c) Logical AND
d) Logical OR
Ans: a
31. An octal integer constant consists of any combination of digits from the set _________
through _________.
Ans: (0,7)
34. The size of the Integers in C language is same in all the machines. (True/False)
Ans: True
37. Which of the following tells the compiler the name and type of a variable you'll be using in
your program?
pg. 5
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
(a) declaration
(b) variables
(c) integers
(d) assignments
Ans: a
pg. 6
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
d) Separation
Ans: a
a) Additional
b) Subtraction
c) Multiplication
d) Modulus
Ans: d
47. The logical operators ___________ and __________ are used when we want to test more
than one condition and make decisions.
Ans: &&, ||
Ans: Incorrect
49. ____________________ are built-in functions that carry out various commonly used
operations or calculations
Ans: Library Function
50. Fill in the blanks as indicated by alphabets. The value of (a) floor(5.8),
(b) floor(-5.8), (c) ceil(5.8) and (d) ceil(-5.8) are ______ (a) _____,
____ (b) ______, ____ (c) _______ and _____ (d) ______ respectively.
pg. 7
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
51. Header files in C contain
a) Compiler commands
b) Library functions
c) Header information of C programs
d) Operators for files
Ans: b
52. Which pair of functions below are used for single character I/O.
a) Getchar() and putchar()
b) Scanf() and printf()
c) Input() and output()
d) None of these
Ans: a
53. The printf() function retunes which value when an error occurs?
a) Positive value
b) Zero
c) Negative value
d) None of these
Ans: c
a) C
b) A
c) a
d) none of these
Ans: c
56. gets() is a formatted input statement. (True/False)
Ans: False
57. The argument for a gets() and puts() functions are ____________variables.
a) char
pg. 8
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
b) String
c) Integer
d) Float
Ans: b
58. Using gets() function, you cannot include whitespace characters in the input string.
(True/False).
Ans: False
63. When the accuracy provided by a float number is not sufficient, the type long float can be
used to define the number. (True/False)
Ans: False
pg. 9
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Ans: d
65. If the operands are of different data types, the ‘lower’ type is automatically converted to the
‘higher’ type before the operation proceeds. (True/False)
Ans: True
66. During the final assignment ________ to int causes dropping of the excess higher order
bits.
a) int
b) short int
c) long int
d) all of these
Ans: c
Ans: 2.2
70. If the value of X is 35.2, then the value of A in the expression: A = (int)(X+0.5); is
____________.
a) 35
b) 35.7
c) 36
d) None of these
Ans: a
71. What is the format character to display the value of a char variable?
a) %d
b) %f
c) %c
d) %s
Ans: c
pg. 10
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
73. All keywords must be written in ____________.
a) Uppercase
b) Lowercase
c) both (a) and (b)
d) none of these
Ans: b
Ans: False
76. In order to stop reading the input character, you can use a value called___________.
Ans: EOF
77. The __________ string consists of control characters, whitespace characters, and non-
whitespace characters.
Ans: Control
81. To print an int argument in octal, you can use ____________ format string.
a) %u
b) %i
c) %x
d) %o
Ans: d
86. If the function is defined elsewhere (not in the same program where it is called), the function
prototype must be preceded by the keyword
Ans: Extern
_____________.
87. The arguments that appear in function definition are called_____________ arguments
whereas the arguments that appear when the function is called are the ___________
arguments.
Ans: Formal,actual
91. Which of the following header file is required for strcpy() function?
a) string.h
pg. 12
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
b) strings.h
c) files.h
d) strcsspy()
Ans: a
93. Closing a file simply involves calling fclose with the ___________ as its argument.
Ans: File pointer
94. If you forget to close a file, it will be closed automatically when the program exits.
(True/False)
Ans: True
95. The companion function to putchar is putc, and the file pointer argument comes first.
(True/False)
Ans: false
96. Besides the file pointers which we explicitly open by calling fopen, there are also
____________ predefined streams.
Ans: Three
100. The library function used to copy one string to another is_________________.
Ans: strcpy
101. The library function atoi can be used for any string. (True/False)
Ans: False
pg. 13
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
102. Dereference operator is also known as ___________________.
Ans: indirection operator.
105. In C, a Union is
a) memory location
b) memory store
c) memory screen
d) None of these
Ans: b
106. When the main function is called, it is called with the arguments
a) argc
b) argv
c) None of these
d) both a & b
Ans: d
109. The return data type, function name and the list of formal parameters enclosed in brackets
are separated by _______________.
Ans: Comma
pg. 14
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
111. The function prototypes are optional. (True/False)
Ans: False
112. The function prototypes of the library functions is in the corresponding__________ files.
Ans: Header
113. The function prototype for the function fun() called in main() below is________________ .
Ans: double fun(double, double);
114. ______________ is a process by which a function calls itself repeatedly, until some
specified condition is satisfied
Ans: Recursion
116. A Structure
a) can be read as a single entity
b) cannot be read as a single entity
c) can be displayed as a single entity
d) has member variables that cannot be read individually
Ans: b
118. The process of translating a source program into machine language is a function
of:
a) Compiler
b) Translator
c) Assembler
d) None of these.
Ans: a
119. _________________ let you work with the individual bits of a variable; one common use is
to treat an integer as a set of single-bit flags.
pg. 15
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
a) Bitwise operator
b) Logical Operator
c) Relational operator
d) None of these
Ans: a
122. The prefix form to increment i is _______ whereas the postfix form to increments i is
________.
Ans: ++i, i++
pg. 16
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
d) %p
Ans: a
127. The goto requires a _________ in order to identify the place where the
branch is to be made.
Ans: Label
129. The series of statements enclosed by braces after the expression in simple if statement is
itself a simple expression statement. (True/False)
Ans: False
130. In the cascaded if/else/if/else/... chain, each else clause is another _______ statement.
Ans: If
pg. 17
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
138. C supports the ______ statement to branch unconditionally from one point to another in
the program.
a) continue
b) goto
c) break
d) for
Ans: b
140. On using the ________, the body of the loop is always executed at least once irrespective
of the expression.
Ans: Do….while
143. The “control variable'' of a for loop does not have to be an integer. (True/False)
Ans: True
#include<stdio.h>
main()
{
int n=5;
int fun(int n);
printf(“%d\n”, fun(n));
}
int fun(int n)
{
if(n==0)
return 0;
else
return (n+fun(n-1));
pg. 18
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
}
Ans: 15
146. Which one of the following is a loop construct that will always be executed once?
a. for
b. while
c. switch
d. do while
Ans d
a. Before main
b. After main
c. Anywhere, but starting on a new line.
d. None of the these.
Ans c
148. What is the result after execution of the following code if a is 10, b is 5, and c is 10?
If ((a > b) && (a <= c))
a = a + 1;
else
c = c+1;
a. a = 10, c = 10
b. a = 11, c = 10
c. a = 10, c = 11
d. a = 11, c = 11
Ans: b
149. How many characters can a string hold when declared as follows?
char name[20]:
a. 18
pg. 19
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
b. 19
c. 20
d. None of the these
Ans: c
150. What is the maximum number of characters that can be held in the string variable char
address line [40]?
a. 38
b. 39
c. 40
d. 41
Ans: b
152. Does an automatic variable retain its value once control is transferred out of its defining
function? (Yes/No)
Ans: No
153. The key word auto is must in the declaration of automatic variables. (True/False)
Ans: False
154. The variables declared in the main() function are the global variables.(True/False)
Ans: False
155. The global variables are more secured than the automatic variables in a program.
(True/False)
Ans: False
156. The scope of static variables and automatic variables is the same. (True/False)
Ans: True
157. ____________ variables retain their values throughout the life of the program. As a result,
if a function is exited and then reentered later, the static variables defined within that function
will retain their previous values.
Ans: Static
pg. 20
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
159. What will the result of len variable after execution of the following statements?
int len;
char str1[] = {"39 march road"};
len = strlen(str1);
a. 11
b. 12
c. 13
d. 14
Ans c
Ans b
1. x - = y + 1;
a. x = x - y + 1
b. x = -x - y - 1
c. x = x + y - 1
d. x = x - y - 1
Ans d
main()
{
char *s = "Hello,"
"World!";
printf("%s", s);
pg. 21
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
}
a. Hello, World!
b. Hello,
World!
c. Hello
d. Compile error
Ans B
163. By modularizing the problem into different sub problems. Each sub problem can be
implemented as a __________.
Ans: Function
Ans: False
165. The visibility of a variable determines how much of the rest of the program can access that
variable. (True/False)
Ans:True
166. __________ class refers to the persistence of a variable and its scope within the program,
that is, the portion of the program over which the variable is recognized.
Ans: Storage
167. Visibility provides security for your data used in a program. (True/False)
Ans:True
168. The main purpose of using external variables is to access the same variable in different
______________ files.
Ans: Source
169. Compiler doesn’t allocate memory for an external variable where it is accessed.
(True/False)
Ans:True
170. Global variables and external variables have the same scope. (True/False)
Ans: False
pg. 22
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
172. An array name is a pointer. (True/False)
Ans: True
174. Suppose that cPtr is a character pointer, and its current content is 300. What will be the
new value in cPtr after the following assignment?
cPtr = cPtr + 5;
a. 305
b. 310
c. 320
d. 340
Ans a
a. Non-linear
b. Primary
c. Linear
d. Data type
Ans c
a. In the array, users can only allocate the memory at the run time.
b. In the array, users can only allocate the memory at the compile time.
c. The array is a primitive and non-linear data structure that only stores a similar data type.
pg. 23
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
d. All of the these
Ans: b
177. In a two-dimensional matrix, the first subscript in the declaration specifies number of
__________.
Ans: Rows
199. Will there be a compilation error for the following program? (Yes/No).
char str1[10];
str1=”Hello, world”;
printf(“%s”, str1);
200. The contents-of operator * does not merely fetch values through pointers; it can also set
values through pointers. (True/False)
Ans: True
201. You can perform any type of arithmetic operation on pointers. (True or False)
Ans: False
202. For an int array a[10], If you declare a pointer variable ip then you can set it to point to the
beginning of an array by assigning: int *ip =____________ .
Ans: &a[0];
203. One common use of pointer comparisons is when copying arrays using pointers.
(True/False)
Ans: True
204. To increment what p points to, you can use the expression __________.
Ans: (*p)++.)
pg. 24
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Ans: c
a. 1.8
b. 1.0
c. 2.0
d. None of the these
Ans: d
Ans: a
a. Compiler
b. Computer
c. Compiler library
d. Users
Ans: d
pg. 25
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
d. None of the these
Ans: d
210. Which of the following function is used to write the integer in a file?
a. getw()
b. putw()
c. int value
d. f_int()
Ans: b
211. A ______ is a special pointer value that is known not to point anywhere.
Ans: Null pointer
212. A function that returns ____________ values can return a null pointer when it is unable to
perform its task.
Ans: Pointer
213. In general, C does not initialize pointers to null for you, and it never tests pointers to see if
they are null before using them. (True/False)
Ans: True
215. You can represent an array of strings using pointers by using an array of __________ to
character.
Ans: Pointers
Ans: Null
217. Which of the following statement is correct about the ftell() function?
pg. 26
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Ans: a
218. In which of the following modes, the user can read and write the file?
a. r
b. w
c. r+
d. b+
Ans: c
219. The enum keyword is used to assign names to the ________ constants.
a. Integer
b. String
c. Character
d. All of the these
Ans: a
220. Which of the following operator's precedence order is correct (from highest to lowest)?
a. %, *, /, +, -
b. %, +, /, *, -
c. +, -, %, *, /
d. %, +, -, *, /
Ans: a
221. We can define a two dimensional array as a pointer to a group of contiguous _________
dimensional arrays.
Ans: One
222. A two-dimensional array can also be expressed in terms of an_________ of pointers rather
than as a pointer to a group of contiguous arrays.
Ans: Array
pg. 27
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
223. A ___________ is a convenient tool for handling a group of logically related data items.
Ans: structure
224. We can declare structure variables using the tag name anywhere in the program.
(True/False)
Ans: True
226. If person1 and person2 are variables of the same type structure then the expression
person1>person2 is valid. (True/False)
Ans: False
228. The link between a member and a variable is established using the member operator
_________.
Ans: Dot(.)
a. x * = 65;
b. x / = 42;
c. x % = 2;
d. x ! = 56;
Ans: d
a. ==
b. ++
c. ||
d. &&
Ans: a
pg. 28
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
a. un
b. unt
c. ion
d. union
Ans: d
a. For
b. for
c. Basic salary
d. hello.
Ans: a
234. We can modify a member of the structure by passing the structure as a_____________.
Ans: argument
235. We can use structures to describe the format of a number of related variables.
(True/False)
Ans: True
236. You can declare an array of structures where each element is defined to be of the type
_______.
Ans: struct
237. The parentheses around the de referenced pointer is necessary to enforce the correct
_______.
Ans: precedence
238. An alternative notation other than dot, permits simpler pointer access to structure members
is ____________.
Ans: ->
240. The compiler allocates storage for the smallest member of the union. (True/False)
pg. 29
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Ans: false
241. Which of the following header files is used for character type function in C language?
a. <assert.h>
b. <ctype.h>
c. <iostream.h>
d. <locale.h>
Ans: b
a. *
b. #
c. &
d. &&
Ans: a
247. Defining instances of global variables is not recommended in the header files. (True/False)
Ans: True
pg. 30
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
248. The role of preprocessor in macro substitution is ______substitution according to the
macro definition.
Ans: Text
253. You can use _______________ directive which compiles code depending on whether a
compile-time expression is true or false.
Ans: #if
Ans: d
pg. 31
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
B) Auto Function
C) Recursive Function
D) Static Function
Ans: c
void show()
{
printf("AFRICA ");
}
A) ARGENTINA AFRICA
B) AFRICA ARGENTINA
C) ARGENTINA
D) Compiler error
Ans: b
262. The function that is used to alter the size of a block previously allocated is
______________.
Ans: realloc()
263. A linked list is a data structure that is used to model a dynamic list of data items and is a
collection of ___________.
Ans: nodes.
pg. 32
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
264. Linked list make use of ____________ memory allocation technique.
Ans: dynamic
return 0;
}
A) 25 1234
B) 25 0
C) 25 25
D) Compiler error
Ans: c
269. We may add a “+’’ character to the mode string in the fopen function to indicate that we
want to both read and write. (True/False)
Ans: True
pg. 33
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
270. __________ returns non-zero if the stream's error indicator is set, zero otherwise.
Ans: ferror
271. ____________ function returns the current value (measured in characters) of the file
position indicator if stream refers to a binary file.
Ans: ftell
272. fseek allows the file position indicator for stream to be set to an arbitrary value.
(True/False)
Ans: True
273. __________ stores the current file position indicator for stream in the object pointed to by
pos.
Ans: fgetpos
274. Command line argument is a parameter supplied to a program when the program is
invoked. (True/False)
Ans: true
275. In the command line arguments of main(), argv is an array-of-pointers to-___________.
Ans: char
276. The ____________ is a parameter supplied to a program when the program is invoked.
Ans: Command line argument
pg. 34
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
280. Choose a correct statement about C break; statement.?
A) break; statement can be used inside switch block
B) break; statement can be used with loops like for, while and do while.
C) break; statement causes only the same or inner loop where break; is present to quit
suddenly.
D) All the above.
Ans: d
a) 8
b) 16
c) 64
d) 80
Ans: d
Ans: True
a) -128 to 127
b) 128 to 127
c) 1 to 127
d) none of these
Ans: a
pg. 35
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
285. putchar() function is an input function.(True/False)
Ans: false
Ans: false
287. getchar(),putchar(), scanf(), printf(), gets() and puts(). These functions can be
a) stdio.h
b) math.h
c) conio.h
d) ctype.h
Ans: a
a) Qualifier
c) data type
Ans: b
Ans: d
pg. 36
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
290. Which of the data types has the size that is variable?
a) struct
b) int
c) float
d) double
Ans: a
a) Char str
b) char *str;
c) String str;
d) float I;
Ans: c
292. No of keywords in C ?
a) 35 Only
b) 36 Only
c) 34 Only
d) 32 Only
Ans: d
a) Ture.
b) False.
Ans: b
a) number
b) Both A and C
pg. 37
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
c) Any special symbol except underscore
Ans: b
a) &
b) _
c) #
d) @
Ans: b
a) True
b) False
Ans: b
a) a / = 10;
b) a * = 10;
c) a ! = 10;
d) a % = 10;
Ans: c
a) double
b) long double
c) long int
d) float
Ans:a
pg. 38
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
299. getch() belongs to which header file .
a) math.h
b) stdio.h
c) iostream.h
d) conio.h
Ans: d
a) Yes
b) No
Ans: b
Q1 Write a program to accept a number and check the number is even or odd?
Ans: #include<stdio.h>
int main()
int number;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
pg. 39
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf("%d is even number",number);
else
return 0;
Q2. Write a program to accept three number and find out the largest of three number.
int main()
int a, b, c;
scanf("%d %d %d",&a,&b,&c);
printf("%d is largest",a);
printf("%d is largest",b);
pg. 40
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf("%d is largest",c);
Q3. Write a program to accept the marks and check the grade depends of a condition
(marks >85 display grade A,marks >60 display grade B+, marks>40 display grade B,
marks>30 display grade C, other wise display fail).
int main()
int marks;
scanf("%d",&marks);
pg. 41
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf("You scored grade B ...");
else
Ans: Constants in C refer to fixed values that do not change during the execution of a program. C
supports several types of constants:
1. Integer constants
An integer constant refers to a sequence of digits. There are three types of integers, namely decimal,
octal and hexadecimal.
Decimal integers consist of a set of digits, 0 through 9, preceded by an optional – or +.
Examples: 12, -546, 0, 354647, +56
2. Real constants
The numbers containing fractional parts like 67.45 are called real (or floating point) constants.
Examples: 0.0045, -8.5, +345.678
3. Character constants
A single character constant (or simple character constant) contains a single character enclosed within a
pair of single quote marks.
Examples: ‘6’, ‘X’, ‘;’
4. String constants
A string constant is a sequence of characters enclosed within double quotes.The characters may be
letters, numbers, special characters and blank space.
Examples: “Hello!”, “1947”, “5+3”
Q5. Write a program to accept a year and check the year is leap year or not a leap year.
Ans:
pg. 42
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
#include<stdio.h>
#include<conio.h>
int main()
{
int year;
printf("Enter Year: ");
scanf("%d", &year);
if((year%4==0) && (year%100!=0))
printf("\nIt's a Leap Year");
else if(year%400==0)
printf("\nIt's a Leap Year");
else
printf("\nIt's not a Leap Year");
getch();
return 0;
}
Ans: #include<stdio.h>
#include<conio.h>
int main()
{
float fahrenheit, celsius;
printf("Enter Temperature Value (in Fahrenheit): ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit-32)/1.8;
printf("\nEquivalent Temperature (in Celsius) = %0.2f", celsius);
getch();
return 0;
}
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations.
Operators are used in programs to manipulate data and variables. There are very types of operator:
1. Arithmetic operators
2. Unary operator
3. Relational operators
4. Logical operators
5. Conditional operator
6. Bitwise operators
7. Increment and Decrement operators
pg. 43
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
1. Arithmetic Operators
The basic operators for performing arithmetic are the same in many computer languages:
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder)
2. Unary Operator
A unary operator acts upon a single operand to produce a new value. Example (++,--)
3. Relational
The relational operators such as <, <=, >, and >= are in fact operators, just like +, -, *, and /. The
relational operators take two values, look at them, and “return'' a value of 1 or 0 depending on whether
the tested relation was true or false. The complete set of relational operators in C is:
< less than
<= less than or equal
> greater than
>= greater than or equal
== equal
!= not equal
4. Logical operators
You can also combine true/false values by using the Boolean operators(also called the logical
operators), which take true/false values as operands and compute new true/false values. The three
Logical operators are:
&& AND
|| OR
! NOT (takes one operand, “unary'')
Q8. Write a program to accept a number and find out a factorial number.
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i, fact=1;
printf("Enter any number: ");
scanf("%d", &num);
for(i=num; i>0; i--)
fact = fact*i;
printf("\nFactorial of %d = %d", num, fact);
getch();
return 0;
}
pg. 44
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Q9. What is Library Function? write any five library function?
Ans:
The C language is accompanied by a number of library functions or built in functions that carry
out various commonly used operations or calculations. There are library functions that carry out
standard input/output operations, functions that perform operations on characters, functions that
perform
operations on strings and functions that carry out various mathematical calculations.
A library function is accessed simply by writing the function name, followed by a list of
arguments that represent information being passed to the function. A typical set of library
functions will include a large number of functions that are common to most C compilers.
pow(d1,d2) Return d1 raised to the power d2
Q10. Write a program to accept a number and find out a factorial number using function.
Ans:
#include<stdio.h>
#include<conio.h>
int findFact(int);
int main()
{
int num, fact;
printf("Enter any number: ");
scanf("%d", &num);
fact = findFact(num);
printf("\nFactorial of %d = %d", num, fact);
getch();
return 0;
}
pg. 45
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
int findFact(int n)
{
int i=n, f=1;
while(i>0)
{
f = f*i;
i--;
}
return f;
}
Q11. write a program that will calculate the factorial of any given number using recursive
function?
Ans: #include<stdio.h>
#include<conio.h>
int findFact(int);
int main()
{
int num, fact;
printf("Enter the number: ");
scanf("%d", &num);
fact = findFact(num);
printf("Factorial = %d", fact);
getch();
return 0;
}
int findFact(int val)
{
if(val==1)
return val;
else
return val*findFact(val-1);
}
Q12. Write a program to accept a character value and check the character value is vowel
or consonant. (using switch statement)
Ans:
#include <stdio.h>
int main()
char input;
pg. 46
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf( "Enter a character " );
switch ( input )
case 'a':
break;
case 'e':
break;
case 'i':
break;
case 'o':
break;
case 'u':
break;
case 'A':
break;
case 'E':
break;
pg. 47
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
case 'I':
break;
case 'O':
break;
case 'U':
break;
default:
printf( "Consonant" );
break;
return 0;
Q13. Write a program to accept a character value and check the character value is vowel
or consonant. (using if statement)
Ans:
#include <stdio.h>
int main()
char input;
pg. 48
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
if(input == ‘a’ || input == ‘A’ || input == ‘e’ || input == ‘E’ || input == ‘i’ || input == ‘I’ || input == ‘o’
|| input == ‘O’ || input == ‘u’ || input == ‘U’ )
else
printf( "Consonant" );
return 0;
Ans:
Void main()
int count;
sum = 0;
count = 0;
while (count<N)
Printf(“enter a number”);
scanf(“%f”, &number);
pg. 49
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
count = count + 1;
average = sum / N;
Q15. write a program to accept any number and check number is prime or not.
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{
int num, i, count=0;
printf("Enter a number: ");
scanf("%d", &num);
for(i=2; i<num; i++)
{
if(num%i == 0)
{
count++;
break;
}
}
if(count==0)
printf("\nIt's a prime number");
else
printf("\nIt's not a prime number");
getch();
return 0;
}
Ans:
#include<stdio.h>
#include<conio.h>
pg. 50
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
void main()
clrscr();
scanf(“%d”,&n);
large=0;
i=0;
while(i<n)
scanf(“%d”, &num);
if(large<num)
large=num;
i++;
Ans:
#include <stdio.h>
Void main()
int i, j, m, n;
pg. 51
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf(“Enter the order of the matrices:”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”, &a[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”, &b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j] = a[i][j]+b[i][j];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf(“%d\t”, c[i][j]);
printf(“\n”);
Q18. Write a program to accept two number and swap the two number.
Ans: #include<stdio.h>
#include<conio.h>
pg. 52
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
int main()
{
int num1, num2, temp;
printf("Enter Two Numbers:-\n");
printf("First Number: ");
scanf("%d", &num1);
printf("Second Number: ");
scanf("%d", &num2);
printf("\nBefore Swap:\n");
printf("First Number = %d\tSecond Number = %d", num1, num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("\n\nAfter Swap:\n");
printf("First Number = %d\tSecond Number = %d", num1, num2);
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int num, rev=0, rem, temp;
pg. 53
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf("Enter a Number: ");
scanf("%d", &num);
temp = num;
while(temp>0)
{
rem = temp%10;
rev = (rev*10)+rem;
temp = temp/10;
}
if(rev==num)
printf("\nIt's a Palindrome Number");
else
printf("\nIt's not a Palindrome Number");
getch();
return 0;
}
Ans:
#include<stdio.h>
main()
int a, b;
pg. 54
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
{
int temp;
temp=*x;
*x=*y;
*y=temp;
return;
Ans: #include<stdio.h>
#include<conio.h>
int main()
{
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)
printf("* ");
printf("\n");
}
getch();
return 0;
}
Output
Q23. write a program to accept the array and finding the largest number from given list
(or array)
pg. 55
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10], i, large;
printf("Enter 10 Array Elements: ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
i=0;
large = arr[i];
while(i<10)
{
if(large<arr[i])
large = arr[i];
i++;
}
printf("\nLargest Number = %d", large);
getch();
return 0;
}
Q24. Write a program to accept name and find out of length of name.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[20];
int len;
printf("Enter the string: ");
gets(str);
len = strlen(str);
printf("\nLength of the string = %d", len);
getch();
return 0;
}
Q25. write a program to accept any two string and display full string using concatenate
function in string.
Ans:
#include<stdio.h>
pg. 56
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
#include<conio.h>
#include<string.h>
int main()
{
char str1[50], str2[50];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
strcat(str1, str2);
printf("\nString after concatenation is:\n%s", str1);
getch();
return 0;
}
Ans:
#include<stdio.h>
int main()
int n1=0,n2=1,n3,i,number;
scanf("%d",&number);
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
pg. 57
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
return 0;
Q27. To print the Name, Salary and date of joining of a person.(using Structures)
Ans:
#include<conio.h>
#include<stdio.h>
struct personal
char name[30];
int day;
int month;
int year;
float salary;
};
void main()
struct personal p;
gets(p.name);
scanf(“%d”,&p.day);
scanf(“%d”,&p.month);
scanf(“%d”,&p.year);
pg. 58
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf(“Enter the salary:\n)";
printf(“\nName:",p.name);
printf(“Salary:",p.salary);
getch();
1
12
123
123 4
Ans:
#include <stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
printf("\n");
return 0;
pg. 59
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
Q29. print a pattern like
1
22
333
4444
Ans:
#include <stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
printf("\n");
return 0;
Ans:
Structure
A structure is a user-defined data type available in C that allows to combining data items of
different kinds. Structures are used to represent a record.
Defining a structure: To define a structure, you must use the struct statement. The struct
statement defines a new data type, with more than one member. The format of the struct
statement is as follows:
pg. 60
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
struct [structure name]
{
member definition;
member definition;
...
member definition;
};
union
A union is a special data type available in C that allows storing different data types in the same
memory location. You can define a union with many members, but only one member can
contain a value at any given time. Unions provide an efficient way of using the same memory
location for multiple purposes.
Defining a Union: To define a union, you must use the union statement in the same way as
you did while defining a structure. The union statement defines a new data type with more than
one member for your program. The format of the union statement is as follows:
Q31. Write a program to accept n number and calculate the sum of odd number and sum
of even number using loop?
Ans:
#include <stdio.h>
#include <stdlib.h>
int main()
pg. 61
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
{
int oddSum=0,evenSum=0;
scanf("%d",&num);
evenSum=evenSum+i;
else
oddSum=oddSum+i;
getch();
return 0;
Q32. Write a program to accept n number and count the odd number and even number
in an array?
Ans:
#include<stdio.h>
int main()
int n;
pg. 62
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
scanf(“%d”,&n);
int arr[n];
scanf(“%d”,&arr[i]);
if(arr[i] % 2 == 1)
count_odd++;
else
count_even++;
printf(“Odd: %d”,count_odd);
printf(“\nEven: %d”,count_even);
return 0;
Ans:
#include<stdio.h>
int main()
pg. 63
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
int a[10], Size, i, Largest, Position;
scanf("%d",&Size);
scanf("%d",&a[i]);
Largest = a[0];
if(Largest<a[i])
Largest=a[i];
Position = i;
return 0;
pg. 64
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
}
Q34. Write a program to accept a string and count Alphabets Digits and Special Characters
in a String?
Ans:
#include <stdio.h>
int main()
char str[100];
gets(str);
if( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') )
alphabets++;
pg. 65
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
digits++;
else
special++;
i++;
return 0;
Ans:
#include <stdio.h>
int main()
pg. 66
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
for (c = 0; c < m; c++)
scanf("%d", &matrix[c][d]);
transpose[d][c] = matrix[c][d];
printf("%d\t", transpose[c][d]);
printf("\n");
return 0;
Q36. Write a program to create simple calculator using switch case and functions?
Ans:
#include <stdio.h>
pg. 67
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
float sub(float num1, float num2);
int main()
char op;
printf("----------------------------\n");
switch(op)
case '+':
break;
case '-':
pg. 68
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
break;
case '*':
break;
case '/':
break;
default:
printf("Invalid operator");
return 0;
pg. 69
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
return num1 - num2;
Q37. Write a program to display Fibonacci series in c within a range using a function?
Ans:
#include<stdio.h>
while (a<=range)
printf("%d\t", a);
c = a+b;
pg. 70
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
a = b;
b = c;
int main()
int range;
scanf("%d", &range);
fibonacciSeries(range);
return 0;
Ans:
#include <stdio.h>
int main()
pg. 71
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
scanf("%d", &number);
octalNumber[i] = number % 8;
number = number / 8;
printf("%d", octalNumber[j]);
return 0;
Ans:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system ("cls");
scanf("%d",&n);
for(i=0;n>0;i++)
pg. 72
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
{
a[i]=n%2;
n=n/2;
for(i=i-1;i>=0;i--)
printf("%d",a[i]);
return 0;
Ans:
pg. 73
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
variable must be initialized first to check Initialized in the loop also.
the test condition in the loop.
In a while loop, at the end of the In this, at the end of the condition, there is a
condition, there is no semicolon. semicolon.
Syntax: Syntax:
while (condition) while (condition);
While loop is not used for creating It is mostly used for creating menu-driven
menu-driven programs. programs because at least one time; the loop is
executed whether the condition is true or false.
In a while loop, the number of In a do-while loop, irrespective of the condition
executions depends on the condition mentioned, a minimum of 1 execution occurs.
defined in the while block.
Syntax of while loop: Syntax of do-while loop:
while (condition) do
{ {
Block of statements; statements;
} }
Statement-x; while (condition);
Statement-x;
C Basic Structure
C is characterized by the ability to write very concise source programs, due in part to the large
number of operators included within the language. Thus, the features and capabilities of the
language can easily be manipulated.
1 documentation section
2.link section
3. definition section
pg. 74
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
5.main() functions
6.subprogramme section
The documentation section consists of a set of comment (remarks) linesgiving the name of the
program, the author and other details which the programmer would like to use later.
ANS- Constants in C refer to fixed values that do not change during the execution
2. Real constant
Similarly character constatant devided into two types 1 . string constant 2. Single character
constant
Integer constants
An integer constant refers to a sequence of digits. There are three types ofintegers, namely
decimal, octal and hexadecimal.
optional – or +.
An octal integer constant consists of any combination of digits from the set 0through 7, with a
leading 0.
integer. They may also include alphabets A through F or a through f. The letters A through F
represent numbers 10 through 15.
pg. 75
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
The largest integer value that can be stored is machine-dependent. It is
Real constants
The numbers containing fractional parts like 67.45 are called real (or floating point) constants.
A real number may also be expressed in exponential (scientific) notation.The general form is:
mantissa e exponent
The mantissa is either a real number expressed in decimal notation or an integer. The exponent
is an integer number with an optional plus or minus sign. The letter e separating the mantissa
and the exponent can be written in either lowercase or uppercase.
Floating point constants are normally represented as double-precision quantities. However, the
suffixes f or F may be used to force single precision and l or L to extend double-precision
further.
Character constants
A single character constant (or simple character constant) contains a singlecharacter enclosed
within a pair of single quote marks.
Character constants have integer values known as ASCII values. Forexample, the statement
printf(“%d”, ‘a’);
would print the number 97, the ASCII value of the letter a. Similarly, thestatement
printf(“%c”, 97);
String constants
pg. 76
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
A string constant is a sequence of characters enclosed within double quotes.
space.
ANS
The bitwise operators &, |, ^, and ~ operate on integers thought of as binarynumbers or strings
of bits. The & operator is bitwise AND, the | operator is bitwise OR, the ^ operator is bitwise
exclusive-OR (XOR), and the ~operator is a bitwise negation or complement. (&, |, and ^ are
“binary'' in thatthey take two operands; ~ is unary.) These operators let you work with the
individual bits of a variable; one common use is to treat an integer as a set of single-bit flags.
You might define the 3rd bit as the “verbose'' flag bit by
defining
#define VERBOSE 4
Then you can “turn the verbose bit on'' in an integer variable flags by executing
The left-shift and right-shift operators << and >> let you shift an integer left
or right by some number of bit positions; for example, value << 2 shifts value
The comma operator can be used to link the related expressions together.
The expressions are executed one after the other. The most common use
for comma operators is when you want multiple variables controlling a for
pg. 77
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
loop, for example:
Q2.(B) What do you understand by precedence and associativity of an operator. Explain with
example.
Precedence:-
The precedence of C operators dictates the order of evaluation within anexpression. The
precedence of the operators introduced here is summarised in the highest precedence
operators are given first.
Operators
Associativity
( ) -> .
left to right
! ~ + - ++ -- & *
right to left
pg. 78
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
*/%
left to right
+-
left to right
left to right
== !=
left to right
&
left to right
left to right
&&
left to right
||
right to left
= *= /= %= += -=
pg. 79
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
right to left
Where the same operator appears twice (for example *) the first one is the unary version.
Example:
#include<stdio.h>
Int main()
/* Evaluation of expressions */
float a, b, c, x, y, z;
a=20;
b=2;
c=-23;
x = a + b / ( 3 + c * 4 - 1);
y = a – b / (3 + c) * ( 4 – 1);
z= a – ( b / ( 3 + c ) * 2 ) – 1;
printf(“y=%f\n”, y);
printf(“z=%f\n”, z);
return 0;
ASSOCIATIVITY OF AN OPERATOR:-
pg. 80
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
C supports a rich set of operators. An operator is a symbol that tells thecomputer to perform
certain mathematical or logical manipulations.Operators are used in programs to manipulate
data and variables. A binaryoperator acts on two operands. A unary operator acts upon a single
operandto produce a new value. Multiplication, division, and modulus all have higher recedence
than addition and subtraction.
TYPE CASTING
C performs type conversions automatically. However, there are instances when we want to
force a type conversion in a way that is different from the automatic conversion. Consider,
Since doctor _number and engineer_number are declared as integers in the program, the
decimal part of the result of the division would be lost and Ratio would represent a wrong figure.
This problem can be solved by converting locally one of the variables to the floating point as
shown below:
(type-name) expression
where type-name is one of the standard C data types. The expression may be a constant,
variable or an expression.
Example
Action
X=(int) 8.5
pg. 81
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
A=(int) 21.3 / (int) 4.5
B=(double) sum/n
Y= (int) (a+b)
Z= (int) a+b
P=cos(( double)x)
b) Explain unformatted and formatted input and output functions with example.
Input data can be entered into the computer from a standard input device by means of the
standard C library function scanf(). This function can be used to enter any combination of
numerical values, single character and strings.
pg. 82
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
The function returns the number of data items th at have been entered successfully.
where control string refers to a string containing certain required formatting information, and
arg1, arg2,…, argn are arguments that represent the individual input data items. The arguments
represent pointers that indicate addresses of the data items within the computer’s memory.
Control Character
Explanation
%c
a single character
%d
a decimal integer
%i
an integer
%e, %f, %g
a floating-point number
%o
an octal number
pg. 83
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
%s
a string
%x
a hexadecimal number
%p
a pointer
%n
%u
an unsigned integer
%[]
a set of characters
%%
a percent sign
scanf() reads the input, matching the characters from format. When acontrol character is read, it
puts the value in the next variable. Whitespaces(tabs, spaces, etc) are skipped. Non-
whitespace characters are matched tothe input, then discarded. If a number comes between the
% sign and thecontrol character, then only that many characters will be entered into the
pg. 84
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
variable. If scanf() encounters a set of characters, denoted by the %[] control character, then
any characters found within the brackets are read into the variable. The return value of scanf()
is the number of variables that were successfully assigned values, or EOF if there is an error
Formatted Output
Output data can be written from the computer onto a standard output deviceusing the library
function printf(). This function can be used to output any combination of numerical values, single
characters and strings. It is similar
to the input function scanf(), except that its purpose is to display data rather than enter into the
computer.
where control string refers to a string that contains formatting information,and arg1, arg2, …,
argn are arguments that represent the individual output data items. The arguments can be
written as constants, single variable or array names, or more complex expressions.
Examples:
printf("Hello, world!\n");
printf(“%d”, 10);
printf(“%d”, i+j);
The first statement simply displays the string given as argument to the printf() function. In the
second statement, printf() function replaces the two characters %d with the value of the variable
i. In the third statement the argument to be printed is a constant and in the fourth, the argument
is an expression.
Ans:
pg. 85
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
C supports the goto statement to branch unconditionally from one point to another in the program. Although it
may not be essential to use the goto statement in a highly structured language like C, there may be occasions
when the use of goto might be desirable.
The label can be anywhere in the program either before the goto or after the goto label; statement. During
execution of the program when a statement like
goto first;
is met, the flow of control will jump to the statement immediately following the label first. This happens
unconditionally.
Note that a goto breaks the normal sequential execution of the program. If the label is before the statement goto
label; a loop will be formed and some statements will be executed repeatedly. Such a jump is known as a
backward jump. On the other hand, if the label is placed after the goto label; some statements will be skipped
and the jump is known as the forward jump.
A goto is often used at the end of a program to direct the control to go to the input statement, to read further
data. Consider the following example:
#include<stdio.h>
main()
double a, b;
read:
pg. 86
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
printf(“enter the value of a\n”);
scanf(“%f”, &a);
b=sqrt(a);
goto read;
Ans:
Nesting of if statements
It's also possible to nest one if statement inside another. (For that matter, it's in general possible to nest any kind
of statement or control flow construct within another.) For example, here is a little piece of code which decides
roughly which quadrant of the compass you're walking into, based on an x value which is positive if you're walking
east, and a y value which is positive if you're walking north:
Example
#include<stdio.h>
main()
pg. 87
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
int a,b,c,big;
else if (b>c) // if the condition (a>b) fails check whether b is greater than c
Q45 (a) What is a function? How can you declare a function? Explain with example.
Ans:
Function
A function is a self-contained program segment that carries out some specific, well-defined task. Every C program
contains one or more functions. One of these functions must be called main. Program execution will always begin
by carrying out the instructions in main. Additional functions will be subordinate to main, and perhaps to one
another.
So what defines a function? It has a name that you call it by, and a list of zero or more arguments or parameters.
Parameters (also called formal parameters) or arguments are the special identifiers through which information
can be passed to the function. A function has a body containing the actual instructions (statements) for carrying
out the task the function is supposed to perform; and it may give you back a return value, of a particular type.
Example : Here is a very simple function, which accepts one argument, multiplies it by 4, and hands that value
back.
int multbyfour(int x)
pg. 88
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
{
int retval;
retval = x * 4;
return retval;
On the first line we see the return type of the function (int), the name of the function (multbyfour), and a list of
the function's arguments, enclosed in parentheses. Each argument has both a name and a type; multbyfour
accepts one argument, of type int, named x. The name x is arbitrary, and is used only within the definition of
multbyfour. The caller of this function only needs to know that a single argument of type int is expected; the
caller does not need to know what name the function will use internally to refer to that argument.
Function Prototypes
it is considered good practice to use prototype declarations for all functions that you call. As we mentioned, these
prototypes help to ensure that the compiler can generate correct code for calling the functions, as well as
allowing the compiler to catch certain mistakes you might make.
Examples:
If you write the function definition after the definition of its caller function, then the prototype is required in the
caller, but the prototype is optional if you write the definition of the function before the definition of the caller
function. But it is good programming practice to include the function prototype wherever it is defined.
Ans:
Recursion
Recursion is a process by which a function calls itself repeatedly, until some specified condition has been met. The
process is used for repetitive computations in which each action is stated in terms of a previous result. Many
repetitive problems can be written in this form.
pg. 89
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
In order to solve a problem recursively, two conditions must be satisfied. First, the problem must be written in a
recursive form, and the second, the problem statement must include a stopping condition.
Example :
Factorial of a number. Suppose we wish to calculate the factorial of a positive integer, n. We would normally
express this problem as n!=1 x 2 x 3 x … x n.
This can also be written as n!=n x (n-1)!. This is the recursive statement of the problem in which the desired
action(the calculation of n!) is expressed in terms of a previous result (the value of (n-1)! which is assumed to be
known). Also, we know that 0!=1 by definition. This expression provides stopping condition for the recursion.
Thus the recursive definition for finding factorial of positive integer n can be written as:
fact(n)={ 1 if n=0
n x fact(n-1) otherwise}
Example:
#include <stdio.h>
void main()
{ int n;
scanf(“%d”, &n);
printf(“n!=%ld\n”, fact(n));
if(n==0)
return(1);
else
pg. 90
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
return (n*fact(n-1));
Ans:
Storage Classes
There are two ways to categorize variables: by data type, and by storage class. Data type refers to the type of
information represented by a variable, for example, integer number, floating-point number, character etc.
Storage class refers to the persistence of a variable and its scope within the program, that is, the portion of the
program over which the variable is recognized.
The following types of storage-class specifications in C are discussed in this unit: global, automatic or local, static,
and extern. The exact procedure for establishing a storage class for a variable depends upon the particular
storage class, and the manner in which the program is organized, (i.e. single file vs. multiple file).
Why would you want to limit the visibility of a variable? For maximum flexibility, wouldn't it be handy if all
variables were potentially visible everywhere? As it happens, that arrangement would be too flexible: everywhere
in a program, you would have to keep track of the names of all the variables declared anywhere else in the
program, so that you didn't accidentally re-use one. Whenever a variable had the wrong value by mistake, you'd
have to search the entire program for the bug, because any statement in the entire program could potentially
have modified that variable. You would constantly be stepping all over yourself by using a common variable name
like i in two parts of your program, and having one snippet of code accidentally overwrite the values being used
by another part of the code.
Q46 (b) What is static storage class? Write a program to generate Fibonacci numbers using static variables.
Ans:
The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be
restricted to the file in which it is declared.
pg. 91
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
In C programming, when static is used on a global variable, it causes only one copy of that member to be shared
by all the objects of its class. Static variables are defined within individual functions and therefore have the same
scope as automatic variables, i.e. they are local to the functions in which they are declared. Unlike automatic
variables, however, static variables retain their values throughout the life of the program. As a result, if a function
is exited and then reentered later, the static variables defined within that function will retain their previous
values. This feature allows functions to retain information permanently throughout the execution of a program.
Static variables can be utilized within the function in the same manner as other variables. They cannot be
accessed outside of their defining function. In order to declare a static variable the keyword static is used as
shown below:
You can define automatic or static variables having the same name as global variables. In such situations the local
variables will take precedence over the global variables, though the values of global variables will be unaffected
by any manipulation of the local variables.
Initial values can be included in static variable declaration. The rules associated with the initialization remain
same as the initialization of automatic or global variables. They are:
2. The initial values are assigned to their respective variables at the beginning of the program execution. The
variables retain these values throughout the life of the program, unless different values are assigned during the
course of computation.
3. Zeros will be assigned to all static variables whose declarations do not include explicit initial values.
Example:
#include<stdio.h>
main()
int count, n;
scanf(“%d\n”, &n);
for(count=1;count<=n;count++)
pg. 92
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com
{
long int f;
if (count==1)
f=0;
else if (count==2)
f=1;
else
f=f1+f2;
f2=f1;
function*/
return f;
pg. 93
Solve Assignment Mob No. 7290886986 Email Id - solveassignmentuniversity@gmail.com