C Notes Sem 1 (2nd File)
C Notes Sem 1 (2nd File)
Ans :- The enum in C is also known as the enumerated type. It is a user-defined data type
that consists of integer values, and it provides meaningful names to these values. The use
of enum in C makes the program easy to understand and maintain.
1. #include <stdio.h>
2. enum months{jan=1, feb, march, april, may, june, july, august, september, octobe
r, november, december};
3. int main()
4. {
5. Int I;
6. // printing the values of months
7. for( i=jan;i<=december;i++)
8. {
9. printf("%d, ",i);
10. }
11. return 0;
12. }
Output
Q) Discuss recursion in c with example .
Ans :- In programming languages, if a program allows us to call a function inside the
same function, then it is called a recursive call of the function.
void recurse() {
recurse(); /* function calls itself */
}
int main() {
recurse();
}
The C programming language supports recursion, i.e., a function to call itself. But while
using recursion, programmers need to be careful to define an exit condition from the
function, otherwise it will go into an infinite recursion.
Fibonacci Series
The following example generates the Fibonacci series for a given number using a
recursive function −
Live Demo
#include <stdio.h>
int fibonacci(int i) {
if(i == 0) {
return 0;
}
if(i == 1) {
return 1;
}
else
return fibonacci(i-1) + fibonacci(i-2);
}
int main() {
int i;
return 0;
}
Advantages of Recursion
1. It makes code easier to write:
2. Solves complex ploblem with ease:
In simple words, The break statement is a loop control statement which is used to
terminate the loop immediately.
Break are also used in switch cases to exit the body of a case statement .
Syntax :
Break;
1 2 3 4
continue
The continue exactly as the name suggests. Since we use this in loops, it will skip
over the remaining body of the current loop, and continue to the next iteration.
Syntax :
continue;
How continue statement works?
Working of Continue statement in C
1 2 3 5 6 7 8 9 10
Q ) Discuss operator precedence and associativity in C .
Ans :-
Operator Precedence in C
Operator precedence controls how terms in an expression are grouped and how an expression is
evaluated. Certain operators take precedence over others. The multiplication operator, for
example, takes priority over the addition operator.
For example, x = 2 + 3 * 5;
Operator Associativity in C
The direction in which an expression is evaluated is determined by the associativity of operators.
Associativity is utilized when two operators of the same precedence exist in an expression.
Associativity can be either left to right or right to left.
For example, consider x = 5 / 3 * 3;
Here, the value of x will be assigned as 3 and not 5. ‘*’ operator and ‘/’ operator have the same
precedence, but their associativity is from Left to Right. So first 5 is divided by 3 to get 1, and
then 1 is multiplied by 3, resulting in 3.
Q ) Discuss call by value and call by reference function calling techniques with
example also differentiate between them .
Ans :- There are two methods to pass the data into the function in C language, i.e., call by
value and call by reference.
Call by value in C
o In call by value method, the value of the actual parameters is copied into the formal
parameters.
o In call by value method, we can not modify the value of the actual parameter by the formal
parameter.
o In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
o The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
Example:-
1 A copy of the value is passed into the function An address of value is passed into the function
2 Changes made inside the function is limited to the Changes made inside the function validate outsid
function only. The values of the actual parameters of the function also. The values of the actu
do not change by changing the formal parameters. parameters do change by changing the form
parameters.
3 Actual and formal arguments are created at the Actual and formal arguments are created at th
different memory location same memory location
Differences between Structure and Union are as shown below in tabular format as shown
below as follows: