Saturday, July 14, 2018

C Questions And Answers – 2018A14

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

 

-------------------------------------------------------------------------------------------------

 

How to delete a line from text displayed on the screen?

 

Ans:

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.

 

#include <conio.h>

 

main( )

{

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( ) ;

}

 

-------------------------------------------------------------------------------------------------

 

How do I programmatically insert lines in the text window?

 

Ans:

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.

 

#include <conio.h>

 

void 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( ) ;

}

 

-------------------------------------------------------------------------------------------------

 

What will be the output of the following program?

 

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 ) ;

}

 

Ans:

The output of this program is the binary equivalent of the given number. We have used

bitwise operators to get the binary number.

 

-------------------------------------------------------------------------------------------------

 

If we have declared an array as global in one file and we are using it in another file then why doesn't the sizeof operator works on an extern array?

 

Ans:

An extern array is of incomplete type as it does not contain the size. Hence we cannot use sizeof operator, as it cannot get the size of the array declared in another file. To resolve

this use any of one the following two solutions:

 

1. In the same file declare one more variable that holds the size of array. For example,

 

array.c

int arr[5] ;

int arrsz = sizeof ( arr ) ;

 

myprog.c

extern int arr[] ;

extern int arrsz ;

 

2. Define a macro which can be used in an array declaration. For example,

myheader.h

#define SZ 5

 

array.c

#include "myheader.h"

int arr[SZ] ;

 

myprog.c

#include "myheader.h"

extern int arr[SZ] ;

 

-------------------------------------------------------------------------------------------------

 

How do I write printf( ) so that the width of a field can be specified at runtime?

 

Ans:

This is shown in following code snippet.

 

main( )

{

int w, no ;

printf ( "Enter number and the width for the number field:" ) ;

scanf ( "%d%d", &no, &w ) ;

printf ( "%*d", w, no ) ;

}

 

Here, an '*' in the format specifier in printf( ) indicates that an int value from the argument list should be used for the field width.

 

-------------------------------------------------------------------------------------------------

 

How to find the row and column dimension of a given 2-D array?

 

Ans:

Whenever we initialize a 2-D array at the same place where it has been declared, it is not necessary to mention the row dimension of an array. The row and column dimensions of such an array can be determined programmatically as shown in following program.

 

void main( )

{

int a[][3] = { 0, 1, 2,9,-6, 8,7, 5, 44,23, 11,15 } ;

int c = sizeof ( a[0] ) / sizeof ( int ) ;

int r = ( sizeof ( a ) / sizeof ( int ) ) / c ;

int i, j ;

printf ( "\nRow: %d\nCol: %d\n", r, c ) ;

for ( i = 0 ; i < r ; i++ )

{

for (j=0;j<c;j++ )

printf ( "%d ", a[i][j] ) ;

printf ( "\n" ) ;

}

}

 

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment