Question Bank Subject:Advance Programming in C'
Question Bank Subject:Advance Programming in C'
Answer: B
Library function
User defined function
Library function
main(),printf(),scanf()
#include <stdio.h>
#include <math.h>
Void main()
{
float num,root;
printf("Enter a number to find square root.");
scanf("%f",&num);
root=sqrt(num); /* Computes the square root of num and stores in root. */
printf("Square root of %.2f=%.2f",num,root);
getch();
}
Library functions under "math.h"
Function Work of function
acos Computes arc cosine of the argument
acosh Computes hyperbolic arc cosine of the argument
asin Computes arc sine of the argument
asinh Computes hyperbolic arc sine of the argument
atan Computes arc tangent of the argument
atanh Computes hyperbolic arc tangent of the argument
atan2 Computes arc tangent and determine the quadrant using sign
cbrt Computes cube root of the argument
ceil Returns nearest integer greater than argument passed
cos Computes the cosine of the argument
cosh Computes the hyperbolic cosine of the argument
exp Computes the e raised to given power
fabs Computes absolute argument of floating point argument
floor Returns nearest integer lower than the argument passed.
Computes square root of sum of two arguments (Computes
hypot
hypotenuse)
log Computes natural logarithm
log10 Computes logarithm of base argument 10
pow Computes the number raised to given power
sin Computes sine of the argument
sinh Computes hyperbolic sine of the argument
sqrt Computes square root of the argument
tan Computes tangent of the argument
tanh Computes hyperbolic tangent of the argument
Elements of function:
1.Function call
Syntax :function_name(argument(1),....argument(n));
2.Function declaration
Function prototype(declaration):
Every function in C programming should be declared before they are used. These
type of declaration are also called function prototype. Function prototype gives
compiler information about function name, type of arguments to be passed and
return type.
3.Function Definition
Syntax :
return_type function_name(type(1) argument(1),..,type(n) argument(n))
{
//body of function
}
1. Function declarator
Syntax :
return_type function_name(type(1) argument(1),....,type(n) argument(n))
Syntax of function declaration and declarator are almost same except, there is no
semicolon at the end of declarator and function declarator is followed by
function body.
2. Function body
Return Statement
Return statement is used for returning a value from function definition to calling
function.
return (expression);
OR
return;
Syntax:
#include <stdio.h>
void function_name(){
................
................
}
int main(){
...........
...........
function_name();
...........
...........
}
Ex.1 A sample program of user-defined functions.
#include<stdio.h>
void print(void);
int main()
{
int i;
for(i=0;i<5;i++)
{
print();
printf("\n");
}
getch();
}
void print()
{
printf("Welcome to c");
}
Ex.2 Write a C program to add two integers. Make a function add to add
integers and display sum in main() function.
1.User defined functions helps to decompose the large program into small
segments which makes programmar easy to understand, maintain and debug.
2.If repeated code occurs in a program. Function can be used to include those
codes and execute when needed by calling that function.
Ans: A function that calls itself is known as recursive function and the process
of calling function itself is known as recursion in C programming.
Write a C program to find sum of first n natural numbers using recursion. Note:
Positive integers are known as natural number i.e. 1, 2, 3....n
#include <stdio.h>
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
Output
In, this program, sum() function is invoked from the same function. If n is not
equal to 0 then, the function calls itself passing argument 1 less than the previous
argument it was called with. Suppose, n is 5 initially. Then, during next function
calls, 4 is passed to function and the value of argument decreases by 1 in each
recursive call. When, n becomes equal to 0, the value of n is returned which is
the sum numbers from 5 to 1.
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
Every recursive function must be provided with a way to end the recursion. In
this example when, n is equal to 0, there is no recursive call and recursion ends.
Recursion is more elegant and requires few variables which make program clean.
Recursion can be used to replace complex nesting code by dividing the problem
into same problem of its sub-type.
Output
Q. 11 Explain how you can define the data by using ‘typedef’ statement ?Give
the suitable example.
Ans: Typedef is used to define new data type for an existing data type. It
provides an alternativename for standard data type. It is used for self
documenting the code by allowingdescriptive names. (for beltes understanding)
for the standard data type. The C programming language provides a keyword
called typedef, which you can use to give a type a new name.
The general format is:
typedef existing datatype new datatype;
For example:
typedef float real;
Now, in a program one can use datatype real instead of float.
Therefore, the following statement is valid:
real amount;
After this type definitions, the identifier BYTE can be used as an abbreviation
for the type unsigned char, for example:.
By convention, uppercase letters are used for these definitions to remind the
user that the type name is really a symbolic abbreviation, but you can use
lowercase, as follows:
Q.12 Explain in brief the use of enumerated data types with example
Ans: Enumerated Data Type
It has the following features:
It is user defined.
It works if you know in advance a finite list of values that a data type can take.
The list cannot be input by the user or output on the screen.
For example:
enum months { jan, feb, mar, apr, may};
enum days { sun, mon, tue, wed, thu };
enum toys { cycle, bicycle, scooter };
The enum specifier defines the set of all names that will be permissible values
of
the type called members which are stored internally as integer constant. The
first
name was given the integer value 0, the second value 1 and so on.
#include< stdio.h>
void main()
{
int i;
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
clrscr();
for(i=JAN;i<=DEC;i++)
printf("\n%d",i);
}
Output :
01234567891011
Q.13 Explain in brief arithmetic operations perform on pointers.OR pointer
arithmetic
The increment (++) operator increases the value of a pointer by the size of the
data object the pointer refers to. For example, if the pointer refers to the second
element in an array, the ++ makes the pointer refer to the third element in the
array.
The decrement (--) operator decreases the value of a pointer by the size of the
data object the pointer refers to. For example, if the pointer refers to the second
element in an array, the -- makes the pointer refer to the first element in the
array.
You can add an integer to a pointer but you cannot add a pointer to a pointer.
p = p + 2;
If you have two pointers that point to the same array, you can subtract one
pointer from the other. This operation yields the number of elements in the array
that separate the two addresses that the pointers refer to.
main()
{
int a, b, *p1, *p2, x, y, z;
a = 12;
b = 4;
p1 = &a;
p2 = &b;
x = *p1 * *p2 - 6;
y = 4* - *p2 / *p1 + 10;
printf("Address of a = %u\n", p1);
printf("Address of b = %u\n", p2);
printf("\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %d, y = %d\n", x, y);
*p2 = *p2 + 3;
*p1 = *p2 - 5;
z = *p1 * *p2 - 6;
printf("\na = %d, b = %d,", a, b);
printf(" z = %d\n", z);
}
Output :
Address of a = 4020
Address of b = 4016
a = 12, b = 4
x = 42, y = 9
a = 2, b = 7, z = 8
if (fp == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fp, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fp, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fp, "Salary = %.2f\n", salary);
fclose(fp);
}
Ans: Both the arrays and structures are classified as structured data types as they provide a
mechanism that enable us to access and manipulate data in a relatively easy manner. But they
differ in a number of ways listed in table below:
Arrays Structures
1. An array is a collection of 1. Structure can have elements of
related data elements of same different types
type.
2. An array is a derived data 2. A structure is a programmer-
type defined data type
3. Any array behaves like a 3. But in the case of structure,
built-in data types. All we have first we have to design and
to do is to declare an array declare a data structure before the
variable and use it. variable of that type are declared
and used.
Array allocates static memory Structures allocate dynamic
and uses index / subscript for memory and uses (.) operator for
accessing elements of the accessing the member of a
array. structure.
Array is a base pointer.. Structure is not a pointer
** it points to a particular
memory location..
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element n;
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element n;
}inner_struct_var;
}outer_struct_var;
Example :
struct stud_Res
{
int rno;
char nm[50];
char std[10];
struct stud_subj
{
char subjnm[30];
int marks;
}subj;
}result;
In above example, the structure stud_Res consists of stud_subj which itself is a
structure with two members. Structure stud_Res is called as 'outer structure' while
stud_subj is called as 'inner structure.' The members which are inside the inner
structure can be accessed as follow :
result.subj.subjnm
result.subj.marks
/* Program to demonstrate nested structures.
#include <stdio.h>
#include <conio.h>
struct stud_Result
{
int rno;
char std[10];
struct stud_Marks
{
char subj_nm[30];
int subj_mark;
}marks;
}res;
void main()
{
clrscr();
printf("\n\t Enter Roll Number : ");
scanf("%d",&res.rno);
printf("\n\t Enter Standard : ");
scanf("%s",res.std);
printf("\n\t Enter Subject Code : ");
scanf("%s",result.marks.subj_nm);
printf("\n\t Enter Marks : ");
scanf("%d",&result.marks.subj_mark);
printf("\n\n\t Roll Number : %d",res.rno);
printf("\n\n\t Standard : %s",res.std);
printf("\nSubject Code : %s",res.marks.subj_nm);
printf("\n\n\t Marks : %d",res.marks.subj_mark);
getch();
}
Output :
Enter Roll Number : 1
Enter standard : B.Sc(CS)-I
Enter subject code : CS01
Enter marks : 80
Roll Number : 1
Standard : B.Sc(CS)-I
Subject code : CS01
Q.19 What is union? Explain.
Ans. A union is a special data type available in C that enables you to store 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 multi-purpose.
Defining a Union
To define a union, you must use the union statement in very similar was as you did while
defining 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:
union <union tag>
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
Ex. union Data
{
int i;
float f;
char str[20];
} data;
Now, a variable of Data type can store an integer, a floating-point number, or a string of
characters. This means that a single variable ie. same memory location can be used to store
multiple types of data.
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
void main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
Structure Union
i. Access Members
We can access all the members of structure at
Only one member of union can be accessed at anytime.
anytime.
iii. Initialization
All members of structure can be initialized Only the first member of a union can be initialized.
iv. Keyword
'struct' keyword is used to declare structure. 'union' keyword is used to declare union.
v. Syntax
Mode Description
r Opens an existing text file for reading purpose.
Opens a text file for writing, if it does not exist then a new file is created. Here
w
your program will start writing content from the beginning of the file.
Opens a text file for writing in appending mode, if it does not exist then a new
a file is created. Here your program will start appending content in the existing
file content.
r+ Opens a text file for reading and writing both.
Opens a text file for reading and writing both. It first truncate the file to zero
w+
length if it exists otherwise create the file if it does not exist.
Opens a text file for reading and writing both. It creates the file if it does not
a+ exist. The reading will start from the beginning but writing can only be
appended.
Q.22 Explain the following functions with proper syntax and example:
i)exit()
II)gets
III)puts
IV)flushall()
V)random()
VI)fprintf()
VII)abs()
VII)pow()
Q.23. Explain the following string handling functions:
i)strlen()
II)strcpy()
III)strcat()
IV)strupr()
V)strlwr()
Q.27