C-Interview Questions level 1
Q1) What will be the output of the following code?
void main () { int i = 0 , a[3] ; a[i] = i++; printf (“%d",a[i]) ; }
Answer:- The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i].
int x = 3000, y = 2000 ;
long int z = x * y ;
Answer:- Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below:
long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.
long int z = ( long int ) x * y ;
Note that ( long int )( x * y ) would not give the desired effect.
char str[ ] = "Hello" ; strcat ( str, '!' ) ;
Answer:- The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below:
strcat ( str, "!" ) ;
strcat ( str, "!" ) ;
Answer:- The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.). In general an array cannot consume more than 64 kb. Consider following program, which shows the maximum number of elements an array of type int, float and char can have in case of Small memory model.
main( )
{
int i[32767] ;
float f[16383] ;
char s[65535] ;
}
main( )
{
int i[32767] ;
float f[16383] ;
char s[65535] ;
}
Answer:- : Use peekb( ) function. This function returns byte(s) read from specific segment and offset locations in memory. The following program illustrates use of this function. In this program from VDU memory we have read characters and its attributes of the first row. The information stored in file is then further read and displayed using peek( ) function.
#include#include main( ) { char far *scr = 0xB8000000 ; FILE *fp ; int offset ; char ch ; if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL ) { printf ( "\nUnable to open file" ) ; exit( ) ; } // reads and writes to file for ( offset = 0 ; offset < 160 ; offset++ ) fprintf ( fp, "%c", peekb ( scr, offset ) ) ; fclose ( fp ) ; if ( ( fp = fopen ( "scr.dat", "rb" ) ) == NULL ) { printf ( "\nUnable to open file" ) ; exit( ) ; } // reads and writes to file for ( offset = 0 ; offset < 160 ; offset++ ) { fscanf ( fp, "%c", &ch ) ; printf ( "%c", ch ) ; } fclose ( fp ) ; }
Answer:- Sometimes in a program we require to compare memory ranges containing strings. In such a situation we can use functions like memcmp( ) or memicmp( ). The basic difference between two functions is that memcmp( ) does a case-sensitive comparison whereas memicmp( ) ignores case of characters. Following program illustrates the use of both the functions.
#includemain( ) { char *arr1 = "Kicit" ; char *arr2 = "kicitNagpur" ; int c ; c = memcmp ( arr1, arr2, sizeof ( arr1 ) ) ; if ( c == 0 ) printf ( "\nStrings arr1 and arr2 compared using memcmp are identical" ) ; else printf ( "\nStrings arr1 and arr2 compared using memcmp are not identical" ) ; c = memicmp ( arr1, arr2, sizeof ( arr1 ) ) ; if ( c == 0 ) printf ( "\nStrings arr1 and arr2 compared using memicmp are identical" ) ; else printf ( "\nStrings arr1 and arr2 compared using memicmp are not identical" ) ; }
Answer:-
char *num[10] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ; char num[10][6] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;
The first form uses variable-size data objects. It allocates 10 pointers, which are pointing to 10 string constants of variable size. Assuming each pointer is of 4 bytes, it requires 90 bytes. On the other hand, the second form uses fixed size data objects. It allocates 10 arrays of 6 characters each. It requires only 60 bytes of space. So, the variable-size in this case does not offer any advantage over fixed size.
DOS is a single tasking operating system, thus only one program runs at a time. The Spawnl( ) function provides us with the capability of starting the execution of one program from within another program. The first program is called the parent process and the second program that gets called from within the first program is called a child process. Once the second program starts execution, the first is put on hold until the second program completes execution. The first program is then restarted. The following program demonstrates use of spawnl( ) function.
/* Mult.c */ int main ( int argc, char* argv[ ] ) { int a[3], i, ret ; if ( argc < 3 || argc > 3 ) { printf ( "Too many or Too few arguments..." ) ; exit ( 0 ) ; } for ( i = 1 ; i < argc ; i++ ) a[i] = atoi ( argv[i] ) ; ret = a[1] * a[2] ; return ret ; } /* Spawn.c */ #include#include main( ) { int val ; val = spawnl ( P_WAIT, "C:\\Mult.exe", "3", "10", "20", NULL ) ; printf ( "\nReturned value is: %d", val ) ; }
Answer:- Here, there are two programs. The program 'Mult.exe' works as a child process whereas 'Spawn.exe' works as a parent process. On execution of 'Spawn.exe' it invokes 'Mult.exe' and passes the command-line arguments to it. 'Mult.exe' in turn on execution, calculates the product of 10 and 20 and returns the value to val in 'Spawn.exe'. In our call to spawnl( ) function, we have passed 6 parameters, P_WAIT as the mode of execution, path of '.exe' file to run as child process, total number of arguments to be passed to the child process, list of command line arguments and NULL. P_WAIT will cause our application to freeze execution until the child process has completed its execution. This parameter needs to be passed as the default parameter if you are working under DOS. under other operating systems that support multitasking, this parameter can be P_NOWAIT or P_OVERLAY. P_NOWAIT will cause the parent process to execute along with the child process, P_OVERLAY will load the child process on top of the parent process in the memory.
-------------------------------------------------------------------------------------------------
char str[6] = "Kicit" ; char *str = "Kicit" ;
Answer:- No! Arrays are not pointers. An array is a single, pre-allocated chunk of contiguous elements (all of the same type), fixed in size and location. A pointer on the other hand, is a reference to any data element (of a particular type) located anywhere. A pointer must be assigned to point to space allocated elsewhere, but it can be reassigned any time. The array declaration char str[6] ; requests that space for 6 characters be set aside, to be known
by name str. In other words there is a location named str at which six characters are stored. The pointer declaration char *str ; on the other hand, requests a place that holds a pointer, to be known by the name str. This pointer can point almost anywhere to any char, to any contiguous array of chars, or nowhere.
const int x = 10 ; int arr[x] ;
Answer:- No! Here, the variable x is first declared as an int so memory is reserved for it. Then it is qualified by a const qualifier. Hence, const qualified object is not a constant fully. It is an object with read only attribute, and in C, an object associated with memory cannot be used in array dimensions.
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.