Professional Documents
Culture Documents
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 size determines the number of characters in the string_name. Some examples are:
Example: char city[10];
char name[30];
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;
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 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”.
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.
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;
array[position-1] = value;
n++;
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;
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];
}
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);
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");
void main()
{
int array[100], minimum, size, c, location = 1;
minimum = array[0];
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.
• 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.
#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;
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’.