Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

C Language Library Functions Interview Questions and Answers For Freshers Part-3

Be First!

library function4. What is the difference between a free-standing and a hosted environment?

Not all C programmers write database management systems and word processors. Some write code for embedded systems, such as anti-lock braking systems and intelligent toasters. Embedded systems don’t necessarily have any sort of file system, or much of an operating system at all. The ANSI/ISO standard calls these “free-standing” systems, and it doesn’t require them to provide anything except the language itself. The alternative is a program running on a PC or a mainframe or something in-between; that’s a “hosted” environment.

Even people developing for free-standing environments should pay attention to the standard library. For one thing, if a free-standing environment provides some functionality (such as a square root function), it’s likely to provide it in a way that’s compatible with the standard. (Reinventing the square root is like reinventing the square wheel; what’s the point?) Beyond that, embedded programs are often tested on a PC before they’re downloaded to a toaster (or whatever). Using the standard functions will increase the amount of code that can be identical in both the test and the real environments.

5. What standard functions are available to manipulate strings?

Short answer: the functions in <string.h>.

C doesn’t have a built-in string type. Instead, C programs use char arrays, terminated by the NUL (‘\0′) character.

C programs (and C programmers) are responsible for ensuring that the arrays are big enough to hold all that will be put in them. There are three approaches:

1. Set aside a lot of room, assume that it will be big enough, and don’t worry what happens if it’s not big enough (efficient, but this method can cause big problems if there’s not enough room).

2. Always allocate and reallocate the necessary amount of room (not too inefficient if done with realloc; this method can take lots of code and lots of runtime).

3. Set aside what should be enough room, and stop before going beyond it (efficient and safe, but you might lose data).

There are two sets of functions for C string programming. One set (strcpy, strcat, and so on) works with the first and second approaches. This set copies or uses as much as it’s asked to—and there had better be room for it all, or the program might be buggy. Those are the functions most C programmers use. The other set (strncpy, strncat, and so on) takes the third approach. This set needs to know how much room there is, and it never goes beyond that, ignoring everything that doesn’t fit.

The “n” (third) argument means different things to these two functions:

To strncpy, it means there is room for only “n” characters, including any NUL character at the end. strncpy copies exactly “n” characters. If the second argument doesn’t have that many, strncpy copies extra NUL characters. If the second argument has more characters than that, strncpy stops before it copies any NUL character. That means, when using strncpy, you should always put a NUL character at the end of the string yourself; don’t count on strncpy to do it for you.

An example of the “string-n” functions.

#include <stdio.h>

#include <string.h>

/*

Normally, a constant like MAXBUF would be very large, to

help ensure that the buffer doesn’t overflow.  Here, it’s very

small, to show how the “string-n” functions prevent it from

ever overflowing.

*/

#define MAXBUF 16

int

main(int argc, char** argv)

{

char buf[MAXBUF];

int i;

buf[MAXBUF – 1] = ‘\0’;

strncpy(buf, argv[0], MAXBUF-1);

for (i = 1; i < argc; ++i) {

strncat(buf, ” “,

MAXBUF – 1 – strlen(buf));

strncat(buf, argv[i],

MAXBUF – 1 – strlen(buf));

}

puts(buf);

return 0;

}

 

(40)

Previous
Next

Leave a Reply