0% found this document useful (0 votes)
46 views14 pages

C Notes Sem 1 (2nd File)

Uploaded by

sujaymandalslg09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views14 pages

C Notes Sem 1 (2nd File)

Uploaded by

sujaymandalslg09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Q) Discuss enum data type in C with example .

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.

The following is the way to define the enum in C:

1. enum variable{integer_const1, integer_const2,.....integter_constN};

Let's create a simple program of enum.

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;

for (i = 0; i < 10; i++) {


printf("%d\t\n", fibonacci(i));
}

return 0;
}

Advantages of Recursion
1. It makes code easier to write:
2. Solves complex ploblem with ease:

3. Reduce time complexity:


Disadvantages of Recursion
• More use of Memory:
• Difficult to analyze code:
Q ) Discuss break and continue statements with example .

Ans :- break statement


In all the C loops we have a way to break out of a loop at any point in time,
immediately, regardless of the conditions set for the loop.

This is done using the break keyword.

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;

How break statement works?


Example : break statement
// program to use break statement inside for loop
#include <stdio.h>
int main() {
int i;
for ( i = 1; i <= 10; i++) {
// break condition
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output

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

Example : continue statement


// loop to print numbers 1 to 10 except 4
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
// if i is equal to 4 , continue to next iteration without printing 4.
if (i == 4) {
continue;
}
else{
// otherwise print the value of i.
printf("%d ", i);
}
}
return 0;
}
Output

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:-

Call by Value Example: Swapping the values of the two variables


1. #include <stdio.h>
2. void swap(int , int); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing t
he value of a and b in main
8. swap(a,b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of ac
tual parameters do not change by changing the formal parameters in call by valu
e, a = 10, b = 20
10. }
11. void swap (int a, int b)
12. {
13. int temp;
14. temp = a;
15. a=b;
16. b=temp;
17. printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal para
meters, a = 20, b = 10
18. }
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Call by reference in C
o In call by reference, the address of the variable is passed into the function call as the actual
parameter.
o The value of the actual parameters can be modified by changing the formal parameters
since the address of the actual parameters is passed.
o In call by reference, the memory allocation is similar for both formal parameters and actual
parameters. All the operations in the function are performed on the value stored at the
address of the actual parameters, and the modified value gets stored at the same address.

Call by reference Example: Swapping the values of the two variables


1. #include <stdio.h>
2. void swap(int *, int *); //prototype of the function
3. int main()
4. {
5. int a = 10;
6. int b = 20;
7. printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing t
he value of a and b in main
8. swap(&a,&b);
9. printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of a
ctual parameters do change in call by reference, a = 10, b = 20
10. }
11. void swap (int *a, int *b)
12. {
13. int temp;
14. temp = *a;
15. *a=*b;
16. *b=temp;
17. printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal pa
rameters, a = 20, b = 10
18. }
Output
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Difference between call by value and call by
reference in c
No. Call by value Call by reference

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

Q ) differences between structure and union .


Ans :- Structures in C is a user-defined data type available in C that allows to combining
of data items of different kinds. Structures are used to represent a record.
Defining a structure: To define a structure, we must use the struct statement. The struct
statement defines a new data type, with more than or equal to one member. The format of
the struct statement is as follows:
struct [structure name]
{
member definition;
member definition;
...
member definition;
}
Union in C 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: The format of the union statement is as follows:
union [union name]
{
member definition;
member definition;
...
member definition;
};
Put example of struct and union from the book of reema thareja

Differences between Structure and Union are as shown below in tabular format as shown
below as follows:

You might also like