C Programming Question With Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
91. Explain about array declaration and initialization for different data types.
The array declaration and initialization for different data types are listed below:
•One dimensional Array:
▪Declaration: data_type array_name[size];
▪Examples: int num[5]; char list[10]; float marks[20];
▪Initialization at declaration:
oint num[5]={1, 2, 3, 4, 5};
ochar list[] = {‘a’, ’b’, ’c’, ’d’};
▪Initialization: num[0] = 1, num[1] = 2, list[0] = ‘a’, list[1] = ‘b’;
•Two dimensional Array:
▪Declaration: data_type array_name[row_size][col_size];
▪Examples: int num[5][5]; char list[10][10];
▪Initialization at declaration:
oint num[2][2] = {{1, 2},{3, 4}};
ochar list[][]={{‘a’, ’b’}, {‘c’, ’d’}};
47
▪Initialization: num[0][0] = 1, list[1][1] = ‘c’;
92.What does the name of an array represent? Explain with example how to access elements of an array
The name of an array holds the address of first element of an array. So, it represents the base address of that array.
For one dimensional array, the elements are accessed by using array name followed by an index within a square bracket. For example: if an array is declared as: int num[5]; then num[0], num[1] are used to access first and second element respectively.
For two dimensional array, the elements are accessed using array name followed by two square
bracket within which row index and column index should be given. For example: if an array is
declared as: int list[3][3]; then to access the value we use list[0][1] to access the value of first row and second column.
93.Demonstrate the difference that can be seen in the function declaration, function call and function definition, when a single array element is passed and when complete array is passed.
Let us consider a simple example to demonstrate the difference.
Assuming, Array declaration and initialization: int num[5] = {1 , 2, 3, 4, 5}; And a function check() to do some operation.
|
SN
|
Section
|
Passing Single Element
|
Passing Complete Array
|
|
1.
|
Function declaration
|
void check(int);
|
void check(int[]);
|
|
2.
|
Function call
|
check(num[1]);
|
check(num);
|
|
3.
|
Function definition
|
void check(int x) { }
|
void check(int x[]) { }
|
Here, we can see that, in function declaration and definition the square brackets are not required but square brackets with index are required in function call when single element is passed. On the other hand, there is a presence of square bracket without index in function declaration and definition while passing complete array. And in function call, only the name of an array is passed without pair of square brackets. Since name of an array represents the address of first element, it exhibits pass by reference whereas passing single element represent pass by value.
94.“Passing single element of an array indicates passing by value whereas passing complete array
(array name) represents pass by reference”, explain the statement with example.
When we pass a single element, we use array name with index. For instance, let num be an array defined as: int num[10]; then num[2] represents the third element of an array. Since it stores value
48
only, passing in such way symbolizes passing by value. But when array name is passed, it passes the starting address of an array, as we know that name of an array represents the address of first element. For example, passing array name num to the function represents passing of the address of num[0]; as num = &num[0]. Using that base address, all other elements can be accessed. Hence passing array name or complete array represents pass by reference.
95.Define string and null character. Explain, with example, about string constants and string variables.
A string is an array of characters terminated with a null character. The null character represents the end of a string. The null character is written as ‘’ in C programming.
Let us take an example to demonstrate string constants and variable. char name[20] = “Johnson”;
Here, the variable name is declared as array of characters, which can be used as a string variable.
But to use it as a string variable, it must be assigned a string constant or a set of characters with a null character at the end. The set of characters defined within double quotation marks is a string constant. There will be a default null character in a string constant. While string formed from set of characters must be terminated with null character manually.
96. How are string declared? Demonstrate the different ways of initializing strings.
Strings are declared using an array, with char as data type.
char str[10]; here, str is a string variable.
Stings are initialized as
•char str[] = “Johnson”;
•char str[] = {‘N’,’E’,’P’,’A’,’L’,’’};
Size of an array is optional, when strings are initialized in the declaration. The size of the string will be equal to the number of elements. If size needs to be declared then it must be equal or greater than the number of characters initialized.
97.What is the drawback of using %s in scanf to read a string? Illustrate different ways of reading a string.
When %s is used as format specifier in scanf then it does read a string with a space. All characters before the space character will be read but others characters will be ignored.
Different ways of reading a string are as follows.
•Using unformatted input function: gets(string_variable);
•Using formatted input function: scanf(“%s”, string_variable);
49
▪Character search sets can be used
scanf(“%[^n]”, string_variable); reads characters unless new line is detected.
•Reading character wise
i= 0;
while(string_variable[i]!=’n’)
{
string_variable[i] = getchar();
i++;
}
string_variable[i] = ‘’;
98. Explain why address operator (ampersand) is not used while reading a string?
String is the name of an array of character. Since name of an array represents the address of the first elements, string also symbolizes the address of first character. Hence address operator is not used while reading a string using scanf().
99. Describe the general form, purpose, and return value (if any) of the following string functions:
strlen(), strcat(), strcpy(), strcmp()
The description of different string functions are listed in the table below:
|
SN
|
Functions
|
General Form
|
Purpose
|
Returning value
|
|
1.
|
strlen()
|
len = strlen(str1);
|
Givens the length of a string
|
Length as integer
|
|
(str1)
|
||||
|
2.
|
strcpy()
|
strcpy(str1, str2);
|
Copy value of one string
|
None
|
|
(str2) to another string (str1)
|
||||
|
3.
|
strcat()
|
strcat(str1, str2);
|
Combines two string(str1 and
|
None
|
|
str2) into one (str1)
|
||||
|
4.
|
strcmp();
|
c = strcmp(str1, str2);
|
Compares two strings (str1,
|
Difference of ASCII
|
|
str2)
|
value of character
|
|||
100.Explain briefly about array of strings.
As one dimensional array of characters represent a string, two dimensional arrays of characters represent an array of strings.
•General Form:
char string_name[number_of_strings][Number of characters in each string];
50
{“Johnson”,
“Sarah”,
“Holmes”
};
char namelist[10][20]; it represents an array of strings with total 10 strings with each 20 characters long.
•Initialization:
char namelist[10][10] =
In two dimensional declarations of arrays of characters, if the variable is followed with two pair of square brackets then it represents a single character. For instance, namelist[1][1] represents the character ‘a’. In simple, it selects the second row and then second column, pointing to character ‘a’. But if the variable is followed with single pair of square brackets then it represents a string. For example, namelist[2] symbolizes a string “Holmes”.
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …
