C-Interview Questions level 7
Q1) How come the output for both the programs is different when the logic is same?
Answer:-
main( ) { int i, j ; for ( i = 1, j = 1 ; i <= 5, j <= 100 ; i++, j++ ) { gotoxy ( 1, 1, ) ; printf ( "%d %d", i, j ) ; } } main( ) { int i, j ; for ( i =1, j = 1; j <= 100, i <= 5; i++, j++ ) { gotoxy ( 1, 1 ) ; printf ( "%d %d", i, j ) ; } } Output -> 5 5
Even if logic of both the programs is same the output of the first program comes out to be 100, 100, but of the second program it is 5, 5. The comma operator plays a vital role inside the for loop. It always considers the value of the latest variable. So, at the time of testing the condition in for loop, the value of j will be considered in the first program and value of i in the second.
Answer:- The function wherex( ) and wherey( ) returns the x-coordinate and y-coordinate of the current
cursor position respectively. Both the functions return an integer value. The value returned by wherex( )
is the horizontal position of cursor and the value returned by wherey( ) is the vertical position of
the cursor. Following program shows how to use the wherex( ) and wherey( ) functions
#include#include main( ) { printf ( "Just\n To\n Test\n Where\n the cursor\n goes" ) ; printf ( "Current location is X: %d Y: %d\n", wherex( ), wherey( ) ) ; }
Answer:- While writing programs that perform screen-based I/O, you may want to-delete the current
line's contents, moving one line up, all of the output that follows. In such cases a function
called delline( ) can be used. Following code snippet illustrates the use of function delline( ).
#includemain( ) { int i ; clrscr( ) ; for ( i = 0; i <= 23; i++ ) printf ( "Line %d\r\n", i ) ; printf ( "Press a key to continue : " ) ; getch( ) ; gotoxy ( 2, 6 ) ; for ( i = 6; i <= 12; i++ ) delline( ) ; getch( ) ; }
Answer:- The function difftime( ) finds the difference between two times. It calculates the elapsed time
in seconds and returns the difference between two times as a double value.
#include#include #include main( ) { int a[] = { 2, -34, 56, 78, 112, 33, -7, 11, 45, 29, 6 } ; int s ; time_t t1, t2 ; // time_t defines the value used for time function s = sizeof ( a ) / 2 ; t1 = time ( NULL ) ; sel_sort ( a, s ) ; // sort array by selection sort bub_sort ( a, s ) ; // sort array by bubble sort method t2 = time ( NULL ) ; printf ( "\nThe difference between two function calls is %f", difftime ( t2, t1 ) ) ; } In the above program we have called difftime( ) function that returns the time elapsed from t1 to t2.
Answer:- The function swab( ) swaps the adjacent bytes of memory. It copies the bytes from source string
to the target string, provided that the number of characters in the source string is even. While
copying, it swaps the bytes which are then assigned to the target string.
#include#include #include main ( ) { char *str1 = "hS eesll snsiasl not eh es as oher " ; char *str2 ; clrscr( ) ; swab ( str1, str2, strlen ( str1 ) ) ; printf ( "The target string is : %s\n", str2 ) ; // output -- She sells snails on the sea shore getch( ) ; }
Answer:- The compiler
options include : displaying specific warning messages, generating 8087 hardware instructions, using
a filename for generating assembly code, etc. Instead of compiler options being executed at
command line we can use these compiler options in our program. This can be achieved using #pragma
options. We can use various flags with #pragma options to use the compiler options. All these
flags are available in turbo C's online help
and used in the file 'F2.C' as, extern int a[ ] ;
In the file F2.C, why sizeof doesn't work on the array a[ ]?
Answer:- An extern array of unspecified size is an incomplete type. You cannot apply sizeof to it,
because sizeof operates during compile time and it is unable to learn the size of an array that
is defined in another file. You have three ways to resolve this problem:
1. In file 'F1.C' define as, int a[ ] = { 1, 2, 3, 4, 5, 6 } ; int size_a = sizeof ( a ) ; and in file F2.C declare as, extern int a[ ] ; extern int size_a ; 2. In file 'F1.H' define, #define ARR_SIZ 6 In file F1.C declare as, #include "F1.H" int a[ ARR_SIZ ] ; and in file F2.C declare as, #include "F1.H" extern int a[ ARR_SIZ ] ; 3. In file 'F1.C' define as, int a[ ] = { 1, 2, 3, 4, 5, 6, -1 } ; and in file 'F2.C' declare as, extern int a[ ] ; Here the element -1 is used as a sentinel value, so the code can understand the end without any explicit size.
Answer:- Sometimes, specially when we are creating a text editor like program we may wish to allow user
to delete a line. We can do so by using two functions namely clreol( ) and delline( ). The clreol( )
function deletes the line from the current cursor position to the end of line. The delline() function
deletes the entire line at the current cursor position and
moves up the following line. Following program shows how to use these functions.
#includemain( ) { int i ; for ( i = 1 ; i <= 20 ; i++ ) printf ( "This is Line %d\n", i ) ; getch( ) ; gotoxy ( 1, 7 ) ; clreol( ) ; getch( ) ; gotoxy ( 1, 12 ) ; delline( ) ; getch( ) ; }
Answer:- We can insert a blank line in the text window using the insline( ) function. This function
inserts line at current cursor position. While doing so, it shifts down the lines that are below
the newly inserted line.
#includevoid main( ) { printf ( "The little snail was slowly moving up. She wanted\r\n" ) ; printf ( "to reach the top of the tree. It was chilly\r\n" ) ; printf ( "winter season. Most of the animals were resting in\r\n" ) ; printf ( "their nests as there was a heavy snow fall.\r\n" ) ; printf ( "\r\nPress any key to continue:" ) ; gotoxy ( 10, 2 ) ; getch( ) ; insline( ) ; getch( ) ; }
main( ) { unsigned int num ; int i ; printf ( "\nEnter any number" ) ; scanf ( "%u", &num ) ; for ( i = 0 ; i < 16 ; i++ ) printf ( "%d", ( num << i & 1 << 15 ) ? 1 : 0 ) ; }
Answer:- The output of this program is the binary equivalent of the given number. We have used bitwise
operators to get the binary number.
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.