Docsity

C Programming Complete Notes for Beginners (B.Tech | Exam Ready), Study notes of C programming

Toggle sidebar
bg1
Q&A for Previous Year Questions
Subject: Programming for Problem Solving(B.Tech. I Year)
------------------------------------------------------------------------------------------------------------------------------------------
1
1.
What is a function? Why we use functions in C language? Give an example.
Ans: Function in C:
A function is a block of code that performs a specific task. It has a name and it is reusable .It
can be executed from as many different parts in a program as required, it can also return a
value to calling program.
All executable code resides within a function. It takes input, does something with it, then give
the answer. A C program consists of one or more functions.
A computer program cannot handle all the tasks by itself. It requests other program like entities
called functions in C. We pass information to the function called arguments which specified
when the function is called. A function either can return a value or returns nothing. Function is
a subprogram that helps reduce coding.
Simple Example of Function in C
#include<stdio.h>
#include <conio.h>
int adition (int, int); //Function Declaration int
addition (int a, int b) //Function Definition
{
int r;
r=a + b;
return (r);
}
int main()
UNIT–IV:
Functions: Introduction Designing structured programs, Declaring a function, Signature of a function,
Parameters and return type of a function, passing parameters to functions, call by value,Passing arrays
to functions, passing pointers to functions, idea of call by reference, Some C standard functions and
libraries and Functions,
Recursion: Simple programs, such as Finding Factorial, Fibonacci series etc., Limitations of
Recursive functions
Dynamic memory allocation: malloc, calloc, realloc, free, Allocating and freeing memory,
Allocating memory for arrays of different data types
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download C Programming Complete Notes for Beginners (B.Tech | Exam Ready) and more Study notes C programming in PDF only on Docsity!

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

1. What is a function? Why we use functions in C language? Give an example. Ans: Function in C: A function is a block of code that performs a specific task. It has a name and it is reusable .It can be executed from as many different parts in a program as required, it can also return a value to calling program. All executable code resides within a function. It takes input, does something with it, then give the answer. A C program consists of one or more functions. A computer program cannot handle all the tasks by itself. It requests other program like entities called functions in C. We pass information to the function called arguments which specified when the function is called. A function either can return a value or returns nothing. Function is a subprogram that helps reduce coding. Simple Example of Function in C #include #include int adition (int, int); //Function Declaration int addition (int a, int b) //Function Definition { int r; r=a + b; return (r); } int main()

UNIT–IV:

Functions: Introduction Designing structured programs, Declaring a function, Signature of a function, Parameters and return type of a function, passing parameters to functions, call by value,Passing arrays to functions, passing pointers to functions, idea of call by reference, Some C standard functions and libraries and Functions, Recursion: Simple programs, such as Finding Factorial, Fibonacci series etc., Limitations of Recursive functions Dynamic memory allocation: malloc, calloc, realloc, free, Allocating and freeing memory, Allocating memory for arrays of different data types

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

{ int z; z= addion(10,3); //Function Call printf ("The Result is %d", z); return 0; } Output: The Result is 13 Why use function: Basically there are two reasons because of which we use functions

1. Writing functions avoids rewriting the same code over and over. For example - if you have a section of code in a program which calculates the area of triangle. Again you want to calculate the area of different triangle then you would not want to write the same code again and again for triangle then you would prefer to jump a "section of code" which calculate the area of the triangle and then jump back to the place where you left off. That section of code is called „function'. 2. Using function it becomes easier to write a program and keep track of what they are doing. If the operation of a program can be divided into separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code into modular functions also makes the program easier to design and understand. 2. Distinguish between Library functions and User defined functions in C and Explain with examples. Ans: Types of Function in C: (i). Library Functions in C

C provides library functions for performing some operations. These functions are present in the c library and they are predefined.

For example sqrt() is a mathematical library function which is used for finding the square root of any number .The function scanf and printf() are input and output library function similarly we have strcmp() and strlen() for string manipulations. To use a library function we have to include some header file using the preprocessor directive #include. For example to use input and output function like printf() and scanf() we have to include stdio.h, for math library function we have to include math.h for string library string.h should be included.

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

logic of the overall problem is solved first while the details of each lower level functions is solved later.

  • A C programmer can use function written by others
  • Debugging is easier in function
  • It is easier to understand the logic involved in the program
  • Testing is easier 4. Explain the various categories of user defined functions in C with examples? Ans: A function depending on whether arguments are present or not and whether a value is returned or not may belong to any one of the following categories: (i ) Functions with no arguments and no return values. (ii) Functions with arguments and no return values. (iii) Functions with arguments and return values. (iv) Functions with no arguments and return values.

(i) Functions with no arguments and no return values:-

When a function has no arguments, it does not return any data from calling function. When a function does not return a value, the calling function does not receive any data from the called function. That is there is no data transfer between the calling function and the called function. Example

#include #include void printmsg() { printf ("Hello! I Am A Function ."); } int main() { printmsg(); return 0;

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

}

Output : Hello! I Am A Function.

(ii) Functions with arguments and no return values:-

When a function has arguments data is transferred from calling function to called function. The called function receives data from calling function and does not send back any values to calling function. Because it doesn‟t have return value.

Example

#include #include void add(int,int);

void main() { int a, b;

printf(“enter value”);

scanf(“%d%d”,&a,&b);

add(a,b); }

void add (intx, inty) {

int z ; z=x+y;

printf ("The sum =%d",z); }

output : enter values 2 3

The sum = 5

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

Example

#include #include int add( ); main() { int c;

c=add(); printf ("The sum =%d",c); } int add () { int x,y,z;

printf(“enter value”);

scanf(“%d%d”,&a,&b);

z=x+y;

return z; }

Output: enter values 2 3

The sum = 5

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

5. Explain the Parameter Passing Mechanisms in C-Language with examples. Ans: Most programming languages have 2 strategies to pass parameters. They are (i) pass by value( Call by Value) (ii) pass by reference (Call by Reference) (i) Pass by value (or) call by value :-

In this method calling function sends a copy of actual values to called function, but the changes in called function does not reflect the original values of calling function.

Example program: #include void fun1(int, int); void main( ) { int a=10, b=15; fun1(a,b); printf(“a=%d,b=%d”, a,b); } void fun1(int x, int y) { x=x+10; y= y+20; } Output: a=10 b=

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

Output: a=20 b=

The result clearly shown that the called function reflect the original values in main function. So that it changes original values.

6. Differentiate actual parameters and formal parameters. Ans:

Actual parameters Formal parameters The list of variables in calling function is known as actual parameters.

The list of variables in called function is known as formal parameters. Actual parameters are variables that are declared in function call.

Formal parameters are variables that are declared in the header of the function definition. Actual parameters are passed without using type

Formal parameters have type preceeding with them.

main( ) { ..… function_name (actual parameters); ….. }

return_type function_name(formal parameters) { ..… function body; ….. } Formal and actual parameters must match exactly in type, order, and number. Formal and actual parameters need not match for their names.

7. Explain in detail about nesting of functions with example. Ans: Nesting of functions

The process of calling a function within another function is called nesting of function

Q&A for Previous Year Questions Subject: Programming for Problem Solving(B.Tech. I Year)

Syntax:-

main()

{

………..

Function1();

……….

}

Function1();

{

…………

Function2();

…………

}

Function2();

{

…………

Function3();

…………

}

Function3();

{

………….

}

main () can call Function 1() where Function1 calls Function2() which calls Function3() and so on

Q&A for Previous Year Questions (^) Subject: Computer Programming (B.Tech. I ----------------------- else ------------------------------------------------------------------------------------------------------------------- Year)

return 0; } main reads values a,b,c and calls ratio() to calculate a/(b-c) ratio calls another function difference to test whether (b-c) is zero or not this is called nesting of function.

  1. How to Pass Array Individual Elements to Functions? Explain with example program. Ans: Arrays with functions: To process arrays in a large program, we need to pass them to functions. We can pass arrays in two ways:
  1. pass individual elements

  2. pass the whole array.

Passing Individual Elements:

One-dimensional Arrays:

We can pass individual elements by either passing their data values or by passing their addresses. We pass data values i.e; individual array elements just like we pass any data value .As long as the array element type matches the function parameter type, it can be passed. The called function cannot tell whether the value it receives comes from an array, a variable or an expression.

Program using call by value void func1( int a); void main() { int a[5]={ 1,2,3,4,5}; func1(a[3]); } void func1( int x) { printf(“%d”,x+100); }

Q&A for Previous Year Questions (^) Subject: Computer Programming (B.Tech. I ------------------------------------------------------------------------------------------------------------------------------------------ Year)

Two-dimensional Arrays:

The individual elements of a 2-D array can be passed in the same way as the 1-D array. We can pass 2-D array elements either by value or by address.

Ex: Program using call by value

void fun1(int a)

main()

{

int a[2][2]={1,2,3,4};

fun1(a[0][1]);

}

void fun1(int x)

{

printf(“%d”,x+10);

}

Q&A for Previous Year Questions (^) Subject: Computer Programming (B.Tech. I ----------------------- void main() ------------------------------------------------------------------------------------------------------------------- Year)

{ int a[2][2]={1,2,3,4}; fun1(a); } void fun1(int x[][2]) { int i,j; for(i=0;i<2;i++) for(j=0;j<2;j++) printf(“%d”,x[i][j]);

}

10. Explain how pointers are used as function arguments. Ans: When we pass addresses to a function, the parameter receiving the addresses should be pointers. The process of calling a function using pointers to pass the address of variables is known as “call by reference”. The function which is called by reference can change the value of the variable used in the call. Example :- void main() { int x; x=50; change(&x); /* call by reference or address */ printf(“%d\n”,x); } change(int *p) {

Q&A for Previous Year Questions (^) Subject: Computer Programming (B.Tech. I ----------------------- p=p+10; ------------------------------------------------------------------------------------------------------------------- Year)

} When the function change () is called, the address of the variable x, not its value, is passed into the function change (). Inside change (), the variable p is declared as a pointer and therefore p is the address of the variable x. The statement, *p = *p + 10; Means ― add 10 to the value stored at the address p‖. Since p represents the address of x , the value of x is changed from 20 to 30. Thus the output of the program will be 30, not 20. Thus, call by reference provides a mechanism by which the function can change the stored values in the calling function.

11. Write a ‗C‘ function using pointers to exchange the values stored in two locations in the memory. (Or) Write a C program for exchanging of two numbers using call by reference mechanism. Ans: Using pointers to exchange the values  Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.  Passing only the copy of values to the called function is known as "call by value".  Instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference", since we are referencing the variables.  Here the addresses of actual arguments in the calling function are copied into formal arguments of the called function. Here the formal parameters should be declared as pointer variables to store the address. program #include void swap (int, int); void main( ) {

int a=10, b=15;

Q&A for Previous Year Questions (^) Subject: Computer Programming (B.Tech. I

12. ----------------------- What is recursive function? Write syntax for recursive functions. -------------------------------------------------------------------------------------------------------------------^ Year) Ans: Recursion Recursion is a process by which a function calls itself repeatedly, until some specified condition has been satisfied. When a function calls itself, a new set of local variables and parameters are allocated storage on the stack, and the function code is executed from the top with these new variables. A recursive call does not make a new copy of the function. Only the values being operated upon are new. As each recursive call returns, the old local variables and parameters are removed from the stack, and execution resumes immediately after the recursive call inside the function. The main advantage of recursive functions is that we can use them to create clearer and simpler versions of several programs. Syntax:- A function is recursive if it can call itself; either directly: void f( ) { f( ); } (or) indirectly: void f( ) { g( ); } void g( ) { f( ); } Recursion rule 1: Every recursive method must have a base case -- a condition under which no recursive call is made -- to prevent infinite recursion. Recursion rule 2: Every recursive method must make progress toward the base case to prevent infinite recursion

Q&A for Previous Year Questions (^) Subject: Computer Programming (B.Tech. I

13. ----------------------- Explain about different types of recursive functions with examples -------------------------------------------------------------------------------------------------------------------^ Year) Ans: Types Of Recursion

Any recursive function can be characterized based on: whether the function calls itself directly or (direct or indirect ). Whether any operation is pending at each recursive call (tail-recursive or not). the structure of the calling pattern (linear or tree-recursive). Direct Recursion A function is said to be directly recursive if it explicitly calls itself. Example int Func( int n) { if(n==0) retrun n; return (Func(n-1)); } Indirect Recursion A function is said to be indirectly recursive if it contains a call to another function which ultimately calls it. Example int Func1(int n) { if(n==0) return n; return Func2(n); } int Func2(int x) { return Func1(x-1); } These two functions are indirectly recursive as they both call each other.