C PROGRAMMING final notes
C PROGRAMMING final notes
NNCE I / 02 C / QB
CS3251- PROGRAMMING IN C
I YEAR / II SEMESTER
PREPARED BY
VERIFIED BY
1
Dr.NNCE I / 02 C / QB
PART- A 2 Marks
1. Write down the steps involved in writing a program to solve a problem.
To design a program, a programmer must determine three basic steps:
a. The instruction to be performed.
b. The sequence in which those instructions are to be performed.
c. The data required to perform those instructions.
2
Dr.NNCE I / 02 C / QB
3
Dr.NNCE I / 02 C / QB
11. What are Keywords? What is the importance of keywords in C? (May 2015) Keywords
are pre-defined / reserved words in a C compiler. Each keyword is meant to can be used only for a
specific function in a C program. Since keywords are referred names for compiler, they can‟t be
used as variable name. Ex. auto, break, const, else
4
Dr.NNCE I / 02 C / QB
16. List out the rules to be followed in forming in identifier. (June 2010)
a. First letter should be alphabets.
b. Both numeric and alphabets are permitted.
c. Both lower case and upper case are allowed.
d. No special symbols are permitted except underscore ( _ ).
e. Keywords are not permitted, blank space is not allowed.
17. What is identifier? Give any two examples for an identifier. (Jan 2009)
Identifiers are the names given to variable program elements such as variables, functions and
arrays. Ex. STDNAME, sub, TOT_MARKS,sub123
20. Give two examples for logical and relational expression. (Jan 2011)
Relational Expression Logical Expression
(a>b) if((a>b)&&(a>c))
(a==b) if((a>b)||(a>c))
21. Write the following conditions using ternary operator. (Jan 2009)
4x+100 for x<40
Salary= 300 for x=40
Salary= x<40?4*x+100:x>40?4.5*x+150:300;
5
Dr.NNCE I / 02 C / QB
Operator Meaning
& Bitwise AND
! Bitwise OR
^ Bitwise XOR
<< Shift Left
>> Shift Right
~ One‟s complement
24. What do you meant by conditional or ternary operator? Give an example for Ternary
operator. (Nov 2014)
Conditional or ternary operator‟s returns one value if condition is true and returns another
value is condition is false. Using ?: reduce the number of line codes and improve the performance
of application.
Syntax: (Condition? true_value: false_value);
Ex: a<b ? printf("a is less") : printf("a is greater");
is allocated to that data types. sizeof(char);
//1
6
Dr.NNCE I / 02 C / QB
30. Write a for loop statement to print numbers from 10 to 1. (Jan 2014)
#include <stdio.h>
Output:
void main(void)
{ 10 9 8 7 6 5 4 3 2 1
int count; // Display the numbers 10 to 1
Press any key to continue . . .
for(count = 10; count >= 1; count--)
printf("%d ", count);
}
7
Dr.NNCE I / 02 C / QB
34. Write the limitations of using getchar() and scanf() functions for reading strings.(Jan 2009)
getchar(): It is written in standard I/O library. It reads a single character only from a standard input
device. This function is not use for reading strings.
Scanf: It is use for reading single string at a time. When there is a blank was typed, the scanf()
assumes that it is an end.
35. What are the pre-processor directives? (Jan 2014, May 2014, 2015)
Preprocessor directives are the commands used in preprocessor and they begin with “#” symbol.
Before a C program is compiled in a compiler, source code is processed by a program called
preprocessor. This process is called preprocessing.
Macro Syntax: #define
This macro defines constant value and can be any of the basic data types.
Header file Syntax: #include <file_name>
inclusion The source code of the file “file_name” is included in the main program at
the specified place.
8
Dr.NNCE I / 02 C / QB
41. Define Macro in C. What is the use of #define preprocessor? (Nov 2014)
A macro can be defined as the facility provided by the C preprocessor by which a token can be
replaced by the user defined sequence of characters. Macros are defined with the help of define
directive. Its format is: #define identifier replacement.
#define TABLE_SIZE 100
44. What will be the output for the following program when the value of i is 5 and 10?(June-2012)
#include<stdio.h>
void main()
{
int I; scanf(“%d”,&i); Output
if(i=5) Five (if I is 5)
{ No output if I is 10
printf(“five”);
}
PART-B
1. Explain in detail about the Structure of C Program.
STRUCTURE OF C PROGRAM
Documentation section
Preprocessor section
Definition section
➢ The lines begins with ‘/*’ and ending with ‘*/’ are known as comments lines.
These lines are not executable.
There are two types of comment lines,
➢ Single comment line eg., /* abcd….
➢ Nested comment line (or) multiple comment line (e.g, /*…….. abcd…..... */
Preprocessor Section :
It is used to link system library files for defining the macros and for defining the
conditionalinclusion.
C program depends upon some header file .
Header file is extended with ‘.h’.
This file should be included using #.
Eg., #include<stdio.h>
Global Declaration Section:
The section declares some variables that are used more than one function.
These variables are known as global variables.
These variables must be declared outside of the main function.
Function main:
contain one or more section.
11
Dr.NNCE I / 02 C / QB
Automatic Variable:
The variables without any specification are considered as automatic variables.
They are defined inside a function.
They are called as automatic because their memory space is automatically allocated
as thevariable is declared.
These variable has only temporary memory space .
after execution all automatic variables are disposed. It can not be accessed directly.
Syntax:
storage_class_type data_type var1,var2,…varn;
Example:
auto int a,b;
auto float x,y;
auto char sex
Example Program: Output:
#include<stdio.h> c=340
#include<conio.h> c=458
void main() c=340
{
int c=340;
clrscr();
printf(“c=%
d”,c);
{
int c=458;
printf(“c=%
d”,c);
}
printf(“c=%
d”,c);
getch();
}
Static Variable:
Static variables are the variables for which contents of variables available
throughout theprogram.
12
Dr.NNCE I / 02 C / QB
Example:
extern int a,b;
extern float x,y;
13
Dr.NNCE I / 02 C / QB
14
Dr.NNCE I / 02 C / QB
Example: a+b
Here a, b -> Operator + -> Operand
Types of operators
a) Arithmetic Operators
b) Relational Operators
c) Logical Operators
d) Assignment Operators
e) Increment/ Decrement Operators
f) Conditional Operators(Ternary Operator)
g) Bitwise Operators
h) Special Operators
ARITHMETIC OPERATOR
a+b 10.0
a-b 3.0
15
Dr.NNCE I / 02 C / QB
#include
<stdio.h>int
main()
{
int a = 9,b = 4, c; output
c = a+b; a+b=13
printf("a+b = %d \n",c); a-b=5
c = a-b; a*b=36
printf("a-b = %d \n",c); a/b=2
c = a*b; Remainder when a divided
by b=1printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d
\n",c);c = a%b;
printf("Remainder when a divided by b = %d
\n",c);return 0;
}
RELATIONAL OPERATORS:
Used to compare two or more operands.
Operato Meaning Exampl Return
r e Values
== is equal to 2==3 0
Syntax:
Program For Relational Operators
#include<stdio.h>
#include<conio.h>
void main ()
{
clrscr();
16
Dr.NNCE I / 02 C / QB
|| Logical OR (9>2)||(17==7) 1
17
Dr.NNCE I / 02 C / QB
ASSIGNMENT OPERATOR:
It is used to assign a value of a variable to another variable
Syntax:
Variable=expression(or)value;
Example:
X=10;
X=a+b;
Program For Assignment Operator
#include<stdio.h>
#include<conio.h>
void main ()
{
int i,j,k;
clrscr();
k=(i=4,j=5);
printf(“k=%d”, k);getch();
}
INCREMENT AND DECREMENT OPERATORS
++ is a increment operator
--is a decrement operator
It is also called as unary operator
Operator Meaning
++x Pre increment
--x Pre decrement
x++ Post increment
x-- Post decrement
18
Dr.NNCE I / 02 C / QB
4. Explain different types of conditional statement OR Explain in detail about Decision making
andcontrol statements.
DECISION MAKING STATEMENT
if statements
if…..else statements
nested if….else statements
if……else ladder
IF STATEMENTS
The if statement is a decision making statement
Used to control the flow of execution by executing statements when the condition is true (or) false.
Properties of If statement:
✓ If condition is true, it execute true statement
FLOW CHART:
✓ If condition is false it does not do anything.
✓ Condition must be in parenthesis
SYNTAX: EXAMPLE:
IF…..ELSE STATEMENT
It is two way decision making statement
It has two block if & else
if block is executed when the condition is true, else block is executed when the condition is false.
19
Dr.NNCE I / 02 C / QB
SYNTAX: EXAMPLE:
if(condition is true) #include<stdio.h>
{ #include<conio.h>
True statement; void main()
} { condition
else int num,rem;
{ clrscr();
False statement; printf(“Enter ur number”); True statement False statement
} scanf(“%d”,&num);
rem=num%2;if(rem==0)
printf(“the number is even”);
else
printf(“the number is odd”);getch();
}
20
Dr.NNCE I / 02 C / QB
IF….ELSE LADDER
if(condition 1) #include<stdio.h>
{ main()
Statement 1; {
}
int a,b,c,d;
else if(condition 2)
{ printf(“enter the four numbers”);
Statement 2; scanf(“%d%d%d%d”,&a,&b,&c,&d);
} if((a<b)&&(a<c)&&(a<d))
else if(condition 3) printf(“a is greater”);
{ else
Statement 3; if((b>a)&&(b>c)&&(b>d))
}
printf(“b is greater”);
else
{ else
default-statement; if((c>a)&&(c>b)&&(c>d))
} printf(“c is greater”);
else
printf(“d is greater”);
return(0);
21
Dr.NNCE I / 02 C / QB
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, option;
printf(“\n 1.Addition”);
printf(“\n 2.subtraction”);
printf(“\n 3.multiplication”);
printf(“\n 4.Division”);
printf(“\n 5.Exit”);
printf(“\n Enter two number”);
scanf(“%d %d”, &a, &b);
printf(“\n Enter your option”)
scanf(“%d”, &option);
switch(option)
{
Case 1:
c=a+b;
printf(“\n Addition=%d” ,c);break;
case 2:
c=a-b;
printf(“\n subtraction=%d”,c);
break;
22
Dr.NNCE I / 02 C / QB
case 3:
c=a*b;
printf(“\n multiplication=%d”,c);break;
case 4:
c=a/b;
printf(“\n division=%d”,c);
break; case 5:
Exit(0);break; default:
printf(“Invalid Choice”);
}
getch();
5. Describe the statements for looping OR Explain the various looping constructs.
BRANCHING AND LOOPING:
Loop:
✓ A loop is defined as a block of statements which are repeatedly executed for certain
number oftimes. They are three types of loop control statements
➢ for
➢ while
➢ do-while
WHILE LOOP:
✓ It is repetitive control structure used to executed the statements within the body
until thecondition becomes false. The while loop is an entry controlled loop
statement
✓ In that condition is evaluated first ,if it is true, then the body of the loop is executed.
while(test condition)
{ #include<stdio.h>
body of the loop; void main()
} { condition
int number, digit, rev=0;
printf(“Enter the number:”);
while(number!=0)
{
digit=number%10;
rev=rev*10+digit;
number=number/10; Body of the loop
}
printf(“%d”,rev);
getch();
}
23
Dr.NNCE I / 02 C / QB
DO….WHILE
Here the condition is checked at the end of the loop.
The do while loop will executed at least one time even if the condition is false initially.
The do-while loop executes until the condition becomes false.
SYNTAX: EXAMPLE FLOWCHART:
do #include<stdio.h>
{ #include<conio.h>
Statement; void main()
} { Body of the loop
while(condition); int i=2,sum=0;
do
{
sum=sum+i;
i++; condition
}
while(i<=10)
printf(“sum of the numbers upto 10 is=%d”,sum);
getch();
}
FOR LOOP
✓ It is used to execute set of instructions repeatedly until the condition becomes false.
✓ Initialize counter:
➢ It is used to initialize the counter variable
✓ Test condition:
➢ It is used to test the condition.
✓ Increment / decrement counter:
➢ It is used to Increment / decrement the counter variable
SYNTAX:
SYNTAX: EXAMPLE
Continue; #include<stdio.h>
void main()
{
int i,n,sum=0;
for(i=1;i<=5;i++)
{
printf(“Enter any number”);
scanf(“%d”,&n);
if(n<0)
continue;
else
sum=sum+n;
}
printf(“sum is….%d”,sum;);
}
Goto Statement:
This statement does not require any condition.
This statement passes control any where the program
ie., control is transferred to another part of program without testing the application.
Break statement:
✓ It is used to terminate the loop.
✓ When break is used inside loop, control is automatically transferred to first statement after the loop.
#include <stdio.h>
main()
{
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
//logic
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}
OUTPUT:
n:55 n is not a
Prime number
26
Dr.NNCE I / 02 C / QB
UNIT-II
PART- A
1. Define array. Give example. (Jan 2009, 2014)
Array can be define as a collection of similar data elements of similar data type that arestored in
consecutive memory locations. They are referenced by an index/subscript.
Syntax: data_type array_name [size] Ex.: int rollno[10];
27
Dr.NNCE I / 02 C / QB
28
Dr.NNCE I / 02 C / QB
12. What are the limitation of one-dimensional and two dimensional arrays?
The limitations of one dimensional array are
a. There is no easy method to initialize large number of array elements
b. It is difficult to initialize selected array element The
limitations of two dimensional arrays are
a. Deletion of any element from an array is not possible
b. Wastage of memory when it is specified in large array size
#include <stdio.h>int
main() Output
{
Enter a base number: 3
int base, exponent; long Enter an exponent: 4
long result = 1; Answer = 81
printf("Enter a base number: ");
scanf("%d", &base); printf("Enter an
exponent: "); scanf("%d",
&exponent);
while (exponent != 0)
{
result *= base;
--exponent;
}
printf("Answer = %lld", result);
29
Dr.NNCE I / 02 C / QB
return 0;
}
16. Write a C program to generate Pascal triangle.
#include <stdio.h>
void main()
{
int array[30], temp[30], i, j, k, l, num; //using 2 arrays
printf("Enter the number of lines to be printed: ");
scanf("%d", &num);
temp[0] = 1;
array[0] = 1; Output
for (j = 0; j < num; j++) Enter the number of lines to be printed: 41
{ 1 1
printf(" "); 1 21
} 1 3 3 1
printf(" 1\n");
for (i = 1; i < num; i++)
{
for (j = 0; j < i; j++)
printf(" ");
for (k = 1; k < num; k++)
{
array[k] = temp[k - 1] + temp[k];
}
array[i] = 1;
for (l = 0; l <= i; l++)
{
printf("%3d", array[l]);
temp[l] = array[l];
}
printf("\n");
}
}
n/sizeof(int)); return 0;
}
PART-B
1. Explain in detail about Array
ARRAY
✓ Array is collection of similar data items ,that are stored under a common name.
✓ A value in an array is identified by index or subscript enclosed in square brackets
with arrayname.
✓ Ex: n elements in array q is q[0],q[1],q[2]..q[n]
ARRAY TYPES
✓ One Dimensional Array
✓ Two Dimensional Array
✓ Multidimensional Array
TWO DIMENSIONAL ARRAY
✓ Two dimensional arrays are stored in row –column matrix, where the left index
indicates therow and right index indicates the column.
✓ Two pair of square brackets are required.
✓ Ex: int a[3][3];
Program For Two Dimensional Array(Matrix Multiplication)
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("\n enter the a
matrix\n");for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("a[%d][%d] : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nenter the b
matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("b[%d][%d] : ",i+1,j+1);
scanf("%d",&b[i][j]);
}}
printf("\nmultiplication of the 2 matrices
32
Dr.NNCE I / 02 C / QB
:\n\n");for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3
;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
printf("\t%d",c[i][j]);
}
printf("\n\n");
}
getch();
}
2. Explain in detail about string handling functions
STRING:
The group of characters, digit and symbols enclosed within quotes is called as String (or)
character Arrays. Strings are always terminated with ‘\0’ (NULL) character. The compiler automatically
adds ‘\0’ at the end of the strings.
Example:
char name[]={‘C’,’O’,’L’,’L’,’E’,’G’,’E’,’E’,’\0’};
33
Dr.NNCE I / 02 C / QB
34
Dr.NNCE I / 02 C / QB
#include <stdio.h>
void main()
{
int i, j, a, n, number[30]; printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i]; number[i] = number[j];
number[j] = a;
}
}
}
Output:
Enter the value of N3
Enter the numbers25
35
45
The numbers arranged in ascending order are given below
25 5 45
scanf("%d", &item);
binary_search();
getch();
}
void binary_search()
{
beg = 0;
end = n-1;
mid = (beg + end) / 2;
while ((beg<=end) && (a[mid]!=item))
{
if (item < a[mid]) end = mid - 1;
else
beg = mid + 1;
mid = (beg + end) / 2;
}
if (a[mid] == item)
printf("\n\nITEM found at location %d", mid+1);
else
printf("\n\nITEM doesn't exist");
}
Output:
Enter size of an array: 3
Enter elements of an array in
sorted form: 25
36
45
Enter ITEM to be
searched: 36 ITEM
found at location 2
#include<stdio.h>
int main()
{
int a[10][10], b[10][10], i, j, m, n;
36
Dr.NNCE I / 02 C / QB
return 0;
}
Output:
Enter the number of rows and columns of the matrix:
33
Enter the elements of the matrix:
213
897
153
Transpose of matrix:
281
195
373
6. Write a C Program for Linear Search:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20];
int i,size,key;
printf("\n\t-- Linear Search --\n\n");
printf("Enter total no. of
elements : ");
37
Dr.NNCE I / 02 C / QB
scanf("%d",&size);
for(i=0; i<size; i++)
{
printf("Enter %d
element : ",i+1);
scanf("%d",&arr[i]);
}
printf("Enter the element to be
searched: "); scanf("%d",&key);
for(i=0; i<size; i++)
{
if(key==arr[i])
{
printf("Element exits in the list at position : %d",i+1);
break;
}
}
getch();
}
Output:
-- Linear Search --
Enter total no. of elements : 3
Enter 1 element : 25
Enter 2 element : 36
Enter 3 element : 45
Enter the element to be
searched: 36 Element exits in
the list at position : 2
7. Explain in detail about the sorting with an example.
There are many different techniques available for sorting, differentiated by their efficiency and
space requirements. Following are some sorting techniques which we will be covering in next few
tutorials.
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Heap Sort
3.SELECTION SORT
38
Dr.NNCE I / 02 C / QB
PROGRAM
#include <stdio.h>
int main()
{
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
int i, j, position, swap;
for (i = 0; i < (n - 1); i++)
{
position = i;
for (j = i + 1; j < n; j++)
{
if (arr[position] > arr[j])
position = j;
}
if (position != i)
{
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
OUTPUT:
0 2 6 11 12 18 34 45 99
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Number;
printf("\n Enter Number of rows and columns\n");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for (columns = 0;columns < j;columns++)
{
39
Dr.NNCE I / 02 C / QB
scanf("%d", &Multiplication[rows][columns]);
} }
printf("\n Enter the Multiplication Value : ");
scanf("%d", &Number);
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
a[rows][columns] = Number * a[rows][columns];
}
}
printf("\n The Result of a Scalar Matrix Multiplication is : \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[i][j]);
printf("\nThe matrix is\n");
for(i = 0;i < 3; i++){
printf("\n");
for(j = 0;j < 3; j++)
printf("%d\t", a[i][j]);
}
determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
return 0;
}
OUTPUT:
Enter the elements of matrix:
123
451
234
The matrix is
1 2 3
4 5 1
2 3 4
#include<stdio.h>
main()
{
int
i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,m
ode;float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
41
Dr.NNCE I / 02 C / QB
x=(float)sum/(float)n;
printf("Mean\t= %f",x);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i]; a[i]=a[j];
a[j]=t;
}
}
} if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2]; printf("\nMedian\t= %f",y);
for(i=0;i<n-1;i++)
{
mode=0; for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");else
{
42
Dr.NNCE I / 02 C / QB
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}
OUTPUT:
Median = 3.000000
There is no mode
11. C PROGRAM FOR MATRIX ADDITION:
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);
printf("Enter the elements of second matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &second[i][j]);
printf("Sum of entered matrices:-\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
sum[i][j] = first[i][j] + second[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the number of rows and columns of
matrix2
43
Dr.NNCE I / 02 C / QB
2
Enter the elements of first
matrix1 2
34
Enter the elements of second
matrix5 6
21
Sum of entered
matrices:
6 8
5 5
UNIT-III
PART-A
1. What is a function? (Nov 2014)
A function is a group of statements that together perform a task. Every C program has at least one
function which is main(), and all the most trivial programs can define additional functions.
44
Dr.NNCE I / 02 C / QB
15. What are actual parameters and formal parameters? (May 2015)
Actual Parameters: The parameters in the calling program or the parameters in the function call are
known as actual parameters.
Formal Parameters: The parameters in the called program or the parameters in the function header
are known as formal parameters.
a. The library functions are predefined set a. The user defined functions are defined by
of functions. the user.
b. Their task is limited b. Their task is based on user requirement.
c. The user can use the functions but c. The user can modify the function
cannot change or modify them. according to the requirement.
46
Dr.NNCE I / 02 C / QB
void recursion()
{ recursion(); /* function calls itself */
}
int main()
{ recursion(); }
21. What is a Pointer? How a variable is declared to the pointer? (Jan 2013, May
2009) Pointer is a variable which holds the address of another variable.
Syntax: data_type *variable_name;Ex.: int
*x, a=5;
x=&a;
47
Dr.NNCE I / 02 C / QB
{
int n=20;
printf(“\n %d “, n);
printf(“\n %u“, &n);
printf(“\n %d “, *(&n));
}
30. What are the possible arithmetic operations on pointers in C language?
a. Increment
b. Decrement
c. Subtraction
d. Comparison
Expression Result
C =*y + 10 Value of c is 30
C = *z + *y Value of c is 50
C = *x + 10 + *y Value of c is 40
C = ++*x Value of c is 11
49
Dr.NNCE I / 02 C / QB
PART-B
1. Explain in detail about the function with an example.
Function:
function is itself a block of code which can solve simple or complex task/calculations.
A function performs calculations on the data provided to it is called "parameter" or "argument".A
function always returns single value result.
Types of function:
1. Built in functions(Library functions)
a.) Inputting Functions.
b.) Outputting functions.
2. User defined functions.
a.) fact();
b.) sum();
Parts of a function:
1. Function declaration/Prototype/Syntax.
2. Function Calling.
3. Function Definition.
1.Function Declaration:
Syntax: <return type > <function name>(<type of argument>)
The declaration of function name, its argument and return type is called function declaration.
2.) Function Calling:
The process of calling a function for processing is called function calling.
Syntax: <var_name>=<function_name>(<list of arguments>).
3.) Function definition:
The process of writing a code for performing any specific task is called function definition.
Syntax:
<return type><function name>(<type of arguments>)
{
statement-1> <statement-2>
50
Dr.NNCE I / 02 C / QB
return(<vlaue>)
}
Example: program based upon function:
WAP to compute cube of a no. using function.#include<stdio.h>
#include<conio.h>void
main()
{ int c,n; int cube(int); printf("Enter
a no."); scanf("%d",&n); c=cube(n);
printf("cube of a no. is=%d",c);
} int cube(intn)
{
c=n*n*n;
return(c);
}
51
Dr.NNCE I / 02 C / QB
UNIT IV
STRUCTURES
PART – A
1. Distinguish between arrays and structures.
Arrays Structures
a. An array is a collection of data items of a. A structure is a collection of data items
same data type. Arrays can only be of different data types. Structures can be
declared. declared and defined.
b. There is no keyword for arrays b. The keyword for structures is struct.
c. An array name represents the address of c. A structure name is known as tag. It is a
the starting element Shorthand notation of the declaration.
d. An array cannot have bit fields d. A structure may contain bit fields
1.
2. Differentiate between Structures and Union.
Structure Union
a. Every member has its own memory. a. All members use the same memory.
b. The keyword used is struct. b. The keyword used is union.
c. All members occupy separate memory c. Different interpretations for the same
location, hence different interpretations of memory location are possible.
the same memory location are not possible.
d. Consumes more space compared to union. d. Conservation of memory is possible.
Structure can be defined as a collection of different data types which are grouped together and each
element in a C structure is called member. To access structure members in C, structure variable should
be declared. The keyword used is struct.
53
Dr.NNCE I / 02 C / QB
b. A variable of structure type can store multiple data items of different data types underthe
one name
c.
10. List the main aspects of working with structure.
a. Defining a structure type (Creating a new type).
b. Declaring variables and constants (objects) of the newly created type.
c. Initializing structure elements
18. How will you access the structures member through pointers?
The structures member can be accessed through pointers by the following ways
54
Dr.NNCE I / 02 C / QB
21. Consider the declaration and illustrate the application of size of operator to this structure.
(Nov 2010)
struct student
{ Size of this is 3 bytes:1 byte for name and 2 bytes for integer num.
char name;int
num;
} S;
55
Dr.NNCE I / 02 C / QB
56
Dr.NNCE I / 02 C / QB
FILE PROCESSING
UNIT V
PART-A
1. What is a file?
A file is a collection of related data stored on a secondary storage device like hard disk. Everyfile contains data that is
organized in hierarchy as fields, records, and databases. Stored as sequence of bytes logically contiguous (may not be
physically contiguous on disk
6. What are the statements used for reading a file? (Nov 2014)
a. FILE*p;Initialize file pointer.
b. fp=fopen(“File_name” ”r”);Open text file for reading.
c. Getc(file_pointer_name);Reads character from file.
d. fgets(str,length,fp); Reads the string from the file.
7. What is the difference between getc() and getchar()? (May 2015)
int getc(FILE * stream) gets the next character on the given input stream „s file pointer to pointto the
next character.
int getchar(void) returns the next character on the input stream stdin.
57
Dr.NNCE I / 02 C / QB
unlike binary files, they are less likely to become corrupted. While a small error in a binary file may
make it unreadable, a small error in a text file may simply show up once thefile has been opened.
Mode Description
R Opens a text file in reading mode
W Opens or create a text file in writing mode.
A Opens a text file in appended mode.
r+ Opens a text file in both reading and writing mode.
w+ Opens a text file in both reading and writing mode.
}
else {
printf("One argument expected.\n");
}
}
22. Which methods are used to write in to binary files?
The following are the methods are used to write in to binary files
a. fread() function is used to read a binary file.
b. fwrite() functions is used to write a binary file.
23. What is the basic file operation in C?
There are four basic operations that can be performed on any files in C programminglanguage.
a. Opening / Creating a file.
b. Closing a file
c. Reading a file.
d. Writing in a file.
24. State the fflush() method.
fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output and
move the buffered data to console (in case of stdout) or disk (in case of file output stream). Syntax.
fflush(FILE*ostream);
29. How do you search data in a data file using random access method?
Use the fseek() function to perform random access input / output on a file. After the file was
opened by the fopen() function, the fseek would require three parameters to work: a file pointer to the
file, the number of bytes to search, and the point of origin in the file.
60