You are on page 1of 12

1 What is the difference between Local variable and Global variable?

In C, variables can be declared before they are used in the programs. Here the main difference
between a local and a global variable is that, a local variable is declared inside a function block, where as
the global variable is declared outside the functions in the program.
2 How to declare and define string?

The general form of declaration of a string variable is:


Syntax: char string_name[size];

The size determines the number of characters in the string_name. Some examples are:
Example: char city[10];
char name[30];

string can be initialized in the following forms:


Example: char str[9] = “COMPUTER”;
char str[8] = {‘C’, ‘O’, ‘M’, ‘P’, ‘T’, ‘E’, ‘R’, ‘\0’};
When the compiler assigns a character string to a character array, it automatically supports a null character
(‘\0’) at the end of the string.

3 What is void pointer?

Void pointer or generic pointer is a special type of pointer that can be pointed at objects of any data type.
A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type.
Pointers defined using specific data type cannot hold the address of the some other type of variable.
Declaration of void pointer:
Syntax:
void *pt_name;
Example:
void *v;
int *i;
int ivar;

4 Give the differences between structure and union.

Structure Union
Each member is assigned its own uniqstorage
All members share the same storage area.
area.
Maximum memory requi red by the member is
Total memory requi red by all memberallocated.
allocated.
Only one member is active a time.
All members are active at a time.
Only the first member can be initialized.
All members can be initialized.
Requires more memory. Requires less memory.
Example: Example:
struct SS union UU
{
{ int a;
int a; float b;
float b; char c;
char c; };
}; 4 bytes b a c
1 byte for c for
2 bytes for a c,b,a
4 bytes for b
4 bytes are there between a,b and c because
Total bytes = 1 + 2 + 4 = 7 bytes. largest memory occupies by float which is 4
bytes.
5 Give limitations of array.

• The number of element, which is to be stored in an array, must be known first.


• When an array is declared, memory is allocated to it. If array is not filled completely, then vacant
place occupies memory.
These are static structure, Static in the sense that memory is allocated at compilation time their memory
used by them cannot be reduced or extended.
6 Differentiate actual and dummy arguments.

The terms “formal argument” & “actual argument” are synonymous to the
terms “parameters” & “argument” respectively.

• Formal Argument :
The formal arguments are the arguments in the function declaration. The scope of formal arguments is
local to the function definition in which they are used. They belong to the called function.
• Actual arguments :
The arguments that are passed in a function call are called actual arguments. These arguments are defined
in the calling function.
To sum up in one line — Calling programs pass information to called functions in "actual arguments” &
the called functions access the information using their corresponding "formal arguments”.

7 Define pointer. Write advantages of a pointer


“A pointer is a variable that contains address or location of another variable”
Pointer is a derived data type in C.
Example:
void main()
{
int a=10, *p;
p = &a; // Assign memory address of a to pointer variable p
printf(“%d %d %d”, a, *p, p);
}
Output: 10 10 5000
Variable Value Address
A 5000
10

P 5048

5000

Advantages :
(i) Pointers reduce the length and complexity of a program.
(ii) They increase execution speed.
(iii) A pointer enables us to access a variable that is defined outside the function.
(iv) Pointers are more efficient in handling the arrays and structures.
(v) The use of a pointer array of character strings results in saving of data storage space
in memory.
12 Write the advantages and disadvantages of recursion.

Advantages:
• Reduce unnecessary calling of function.
• Easy solution for recursively defined solution.
• Complex programs can be easily written with less code.
Disadvantages:
• Recursive code is difficult to understand and debug.
• Terminating condition is must; otherwise it will go in an infinite loop.
Execution speed decreases because of function call and return activity many times.
1 Explain declaration and initialization of 2-D array with example.
Two dimensional arrays:
• Two dimensional arrays are also called table or matrix.
• Two dimensional arrays have two subscripts.
• First subscript denotes the number of rows and second subscript denotes the number of columns.
Syntax : data_type array_name [row_size] [column_size];
Example : float marks [10][20];
• Here a mark is declared as a matrix having 10 rows (0 to 9) and 20 columns (0 to 19). The first
element of the matrix is marks[0][0] and the last row last column is marks[9][19]
• A two dimensional array marks[4][3] is shown below. The first element is given by
marks[0][0]contains 35.5 & second element is marks[0][1] and contains 40.5 and so on.

marks [0][0] marks [0][1] marks [0][2]


35.5 40.5 45.5
marks [1][0] 66.5 marks [1][1] marks [1][2]
55.5 60.5
marks [2][0] marks [2][1] marks [2][2]
85.5 78.5 65.3
marks [3][0] marks [3][1] marks [3][2]
25.6 35.2 76.2

Initialization of two dimensional array:


1. int table [2][3] = {1,2,3,4,5,6}; will initialize 1st row 1st column element to 1, 1st row 2nd column
to 2, 1st row 3rd column to 3, 2row 3rd column to 6 and so on.
2. int table [2][3] = {{1,2,3},{4,5,6}}; here, 1st group is for 1st row and 2nd group is for 2nd row. So
1st row 1st column element is 1, 2row1st column element is 4, 2nd row 3rd column element is 6 so
on.
3. int table [2][3] = {{1,2},{4}}
Initializes as above but missing elements will be initialized by 0.

2 Explain how an element will be inserted at position p into a single dimensional array
of size n.
#include <stdio.h>
#include<conio.h>

void main()
{
int array[50], position, c, n, value;

printf("Enter number of elements in the array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

for (c = 0; c < n; c++)


{ scanf("%d", &array[c]); }
printf("Please enter the location where you want to insert an new element\n");
scanf("%d", &position);

printf("Please enter the value\n");


scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)


{
array[c+1] = array[c];
}

array[position-1] = value;
n++;

printf("Resultant array is\n");

for (c = 0; c <n; c++)


{
printf("%d\n", array[c]);
}

getch();
}

3 Write a program to define a macro SQUARE (X) and CUBE(X) to find A (square) of
and (cube)of a given number .
/**
* C program to find square and cube of a number using macro
*/

#include <stdio.h>
#include<conio.h>
// Define macro to find square and cube
#define SQUARE(x) (x * x)
#define CUBE(x) (x * x * x)

int main()
{
int num;

// Input a number from user


printf("Enter any number to find square and cube: ");
scanf("%d", &num);

// Calculate and print square


printf("SQUARE(%d) = %d\n", num, SQUARE(num));

// Calculate and print cube


printf("CUBE(%d) = %d\n", num, CUBE(num));

getch();
}
Output:
Enter any number to find square and cube: 10
SQUARE(10) = 100
CUBE(10) = 1000
4 Explain various string handling operations available in ‘C’ with example.

C has several inbuilt functions to operate on string. These functions are known as string handling functions.

Example:
char s1[]= ”Their”, s2[]= ”There”;

Function Meaning
Returns length of the string.
strlen(s1)
l = strlen(s1); it returns 5
Compares two strings.
It returns negative value if s1<s2, positive if s1>s2 and zero if s1=s2.
strcmp(s1,s2)
printf(“%d”, strcmp(s1,s2));
Output : -9
Copies 2nd string to 1st string.
strcpy(s1,s2) strcpy(s1,s2) copies the string s2 in to string s1 so s1 is now
“There”.s2 remains unchanged.
Appends 2string at the end of 1string.
strcat(s1,s2); a copy of string s2 is appended at the end of string s1.
strcat(s1,s2)
Now s1
becomes “TheirThere”
Returns a pointer to the first occurrence of a given character in the
string s1.
strchr(s1,c)
printf(“%s”,strchr(s1,’i’));
Output : ir
Returns a pointer to the first occurrence of a given string s2 in string
s1.
strstr(s1,s2)
printf(“%s”,strstr(s1,”he”));
Output : heir
Reverses given string.
strrev(s1)
strrev(s1); makes string s1 to “riehT”
Converts string s1 to lower case.
strlwr(s1)
printf(“%s”, strlwr(s1)); Output : their
strupr(s1) Converts string s1 to upper case.
strncpy(s1,s2,n) Copies first n character of string s2 to string s1
strncat(s1,s2,n) Appends first n character of string s2 at the end of string s1.
Compares first n character of string s1 and s2 and returns similar result
strncmp(s1,s2,n)
as
strrchr(s1,c) Returns the last occurrence of a given character in a string s1.
5 Write a program of addition for two 3x3 matrix.
//C program for matrix addition:
#include <stdio.h>

void 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 (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

}
OUTPUT:
Enter the number of rows and columns of matrix 3 3
Enter the elements of first matrix
1
2
3
4
5
6
7
8
9
Enter the elements of first matrix 3 3
1
2
3
4
5
6
7
8
9
Sum of entered matrices:-
2 4 6
8 10 12
14 16 18
6 Write a C program to read N elements into a single dimensional array and sort it.
//C program to accept N numbers and arrange them in an ascending order
#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;
}
}
}

printf("The numbers arranged in ascending order are given below \n");


for (i = 0; i < n; ++i)
printf("%d\n", number[i]);

OUTPUT:
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780
7 Explain how to read and print one dimensional string array with example.
#include <stdio.h>

void main()
{
int arr[10];
int i;
printf("\n\nRead and Print elements of an array:\n");
printf("-----------------------------------------\n");

printf("Input 10 elements in the array :\n");


for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}

printf("\nElements in array are: ");


for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
OUTPUT:
Read and Print elements of an array:
-----------------------------------------
Input 10 elements in the array :
element - 0 : 1
element - 1 : 1
element - 2 : 2
element - 3 : 3
element - 4 : 4
element - 5 : 5
element - 6 : 6
element - 7 : 7
element - 8 : 8
element - 9 : 9

Elements in array are: 1 1 2 3 4 5 6 7 8 9


8 Write a C program to read 10 elements into a single dimensional array and
find minimum number in that array.
#include <stdio.h>
#include<conio.h>

void main()
{
int array[100], minimum, size, c, location = 1;

printf("Enter number of elements in array\n");


scanf("%d", &size);

printf("Enter %d integers\n", size);

for (c = 0; c < size; c++)


scanf("%d", &array[c]);

minimum = array[0];

for (c = 1; c < size; c++)


{
if (array[c] < minimum)
{
minimum = array[c];
location = c+1;
}
}

printf("Minimum element is present at location %d and it's value is %d.\n", location, minimum);
return 0;
}
9 Explain how to initialize one dimensional array with example.
Single Dimensional Array:
• An array using only one subscript to represent the list of elements is called single dimensional
array.

Syntax : data_type array_name [size];


Example : int marks [5];

• An individual array element can be used anywhere like a normal variable with a statement such as
g = marks [60]; More generally if i is declared to be an integer variable, then the statement
g=marks[i]; will take the value contained at ith position in an array and assigns it to g.
• We can store value into array element by specifying the array element on the left hand side of the
equals sign like marks[60]=95; The value 95 is stored at 60 th position in an array.
• The ability to represent a collection of related data items by a single array enables us to develop
Concise and efficient programs.
• For example we can very easily sequence through the elements in the array by varying the value of
the variable that is used as a subscript into the array.
for(i=0; i<66; i++)
{
sum = sum + marks[i];
}
• Above for loop will sequence through the first 66 elements of the marks array (elements 0 to 65)
and will add the values of each marks into sum. When for loop is finished, the variable sum will
then contain the total of first 66 values of the marks.

• The declaration int values[5]; would reserve enough space for an array called values that could
hold up to 5 integers. Refer to the below given picture to conceptualize the reserved storage space.

// Program to find the average of n (n < 10) numbers using arrays

#include <stdio.h>
void main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;

printf("Average = %d", average);

return 0;
}
Output

Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
10 Write different styles to declare and initialize pointers.
A pointer is initialized in the following way:
Pointervariable = &variable;
In above declaration &variable signifies the address of variable.
For example :
Char ch = ‘c’;
Char *chptr = &ch;
Or
Char ch=’c’;
Char *chptr;
Chptr =&ch;
Or
Char ch,*chptr=&ch;
In above code, we declared a character variable ch which stores the value ‘c’. now , we declared pointer
‘chptr’ and initialized it with the address of variable ‘ch’.

You might also like