Assertion (A) : The following program code in C — #include <stdio.h>, struct tryit { char *str; int i; } t;, int main() { scanf("%s %d", t.str, &t.i); printf("%s\n", t.str); return 0; } will cause compile time error. Reason (R) : When pointer is not initialized then it holds garbage value. Select your answer using the codes given below.
(A) is false, but (R) is true.
Read the program carefully. The structure contains a char *str, which is a pointer, and an int i. The variable t is declared at file scope. Then scanf is asked to read a string into t.str and an integer into t.i.
Examine the assertion — false. The code is syntactically perfect. Every declaration is well formed, the structure member access is legal, and scanf's %s conversion expects a char * — which is exactly the type of t.str. A compiler has nothing to object to, so there is no compile-time error. At worst a modern compiler emits a warning about a possibly uninitialised value.
What actually goes wrong. The failure is at run time. t.str is a pointer that has never been made to point anywhere; being a static-storage object it is initialised to NULL, and in the general case an uninitialised pointer holds an arbitrary address. scanf then writes the input characters to that address, which is not memory the program owns. The result is undefined behaviour — typically a segmentation fault, sometimes silent corruption.
The fix is to give the pointer somewhere to point, either by allocating storage
t.str = malloc(100);
or by declaring the member as an array instead, char str[100];, so that space is reserved as part of the structure.
Examine the reason — true. An uninitialised pointer — in automatic storage, at least — does hold whatever bit pattern happened to be in that memory: a garbage value. Dereferencing it is undefined behaviour. The statement is a correct general fact about C.
Assemble. (A) is false and (R) is true, which is code 4.
The distinction the question is really testing. A compile-time error is a violation of the language's syntax or type rules, caught before the program runs. A run-time error arises from what the program does with valid constructs — bad pointers, division by zero, out-of-range indices. C's type system checks that a char * is supplied to %s; it cannot check that the pointer points to usable memory.
Hence, (A) is false, but (R) is true.
The function "isalnum( )" returns a non-zero value if the character is alphanumeric and zero otherwise. What is header file to be used in the main program ?
To read a integer variable, find the correct format given below :
Int m;
Find the correct list, which is the reverse (at all levels) of the given list :
[[4, 7, 8], 2, 9, [19, 6], 20, 3]
(i) 3, 20
(ii) 9, 2
(iii) [6, 19]
(iv) [8, 7, 4]
Which of the following is not an infinite loop ?
(A) int i = 1;
while (1)
{
i++ ;
}
(B) for ( ; ; ) ;
(C) int t = 0, f ;
while (t)
{
f = 1;
}
(D) int y, x = 0 ;
do
{
y = x;
}
while (x == 0);
The three types of loops available in C language are
i. for
ii. while
iii. do-while
Which loops do not operate without testing the condition even once ?
Match the following :
| List – I | List – II |
| a. associativity | i. memory storage |
| b. # define | ii. if-then-else |
| c. auto | iii. operators with equal precedence |
| d. conditional operator | iv. define operator |
Codes :
Assertion (A) : 'C' uses many data types such as integers (short and long) and float etc.
Reason (R) : Conversion specifier used for short unsigned integer is % lu.
Which bitwise operator is suitable for turning off a particular bit in a number ?
Which storage class is used for the variables that are often required in a program ?
Which is not a proper way of array declaration ?
The magazine in which Mahatma Gandhi mentioned what he wanted the Constitution to do is:
Which gas shields the surface of the earth from ultraviolet radiation from the sun?
Which event is marked as an Intangible Cultural Heritage of Humanity by UNESCO?
Who has been conferred with the rank of the Commander of the Order of the British Empire in 2018?
Who directead the film ‘Bhuvan Shome’?