Friday, July 13, 2018

C Questions And Answers – 2018A13

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 do I use swab( ) in my program ?

 

Ans:

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 <stdlib.h>

#include <stdio.h>

#include <string.h>

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

}

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

 

Turbo C provides various command line compiler options which we can use through TCC. 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.

 

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

 

I have an array declared in file 'F1.C' as,

int a[ ] = { 1, 2, 3, 4, 5, 6 } ;

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[ ]?

 

Ans:

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.

 

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

 

What is environment and how do I get environment for a specific entry?

 

Ans:

While working in DOS, it stores information in a memory region called environment. In

this region we can place configuration settings such as command path, system prompt, etc.

 

Sometimes in a program we need to access the information contained in environment. The function getenv( ) can be used when we want to access environment for a specific entry.

 

Following program demonstrates the use of this function.

 

#include <stdio.h>

#include <stdlib.h>

main( )

{

char *path = NULL ;

path = getenv ( "PATH" ) ;

if ( *path != NULL )

printf ( "\nPath: %s", path ) ;

else

printf ( "\nPath is not set" ) ;

}

 

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

 

How do I display current date in the format given below?

Saturday October 12, 2002

 

Ans:

Following program illustrates how we can display date in above given format.

 

#include <stdio.h>

#include <time.h>

main( )

{

struct tm *curtime ;

time_t dtime ;

char str[30] ;

time ( &dtime ) ;

curtime = localtime ( &dtime ) ;

strftime ( str, 30, "%A %B %d, %Y", curtime ) ;

printf ( "\n%s", str ) ;

}

 

Here we have called time( ) function which returns current time. This time is returned in terms of seconds, elapsed since 00:00:00 GMT, January 1, 1970. To extract the week day, day of month, etc. from this value we need to break down the value to a tm structure. This is done by the function localtime( ). Then we have called strftime( ) function to format the time and store it in a string str.

 

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

 

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

No comments:

Post a Comment