C Programming - Library Functions

1. 

It is necessary that for the string functions to work safely the strings must be terminated with '\0'.

A. True
B. False

Answer: Option A

Explanation:

C string is a character sequence stored as a one-dimensional character array and terminated with a null character('\0', called NULL in ASCII).
The length of a C string is found by searching for the (first) NULL byte.


2. 

FILE is a structure suitably typedef'd in "stdio.h".

A. True
B. False

Answer: Option A

Explanation:

FILE - a structure containing the information about a file or text stream needed to perform input or output operations on it, including:
=> a file descriptor, the current stream position,
=> an end-of-file indicator,
=> an error indicator,
=> a pointer to the stream's buffer, if applicable

fpos_t - a non-array type capable of uniquely identifying the position of every byte in a file.
size_t - an unsigned integer type which is the type of the result of the sizeof operator.


3. 

ftell() returns the current position of the pointer in a file stream.

A. True
B. False

Answer: Option A

Explanation:

The ftell() function shall obtain the current value of the file-position indicator for the stream pointed to by stream.

Example:


#include <stdio.h>

int main(void)
{
	FILE *stream;
	stream = fopen("MYFILE.TXT", "w+");
	fprintf(stream, "This is a test");
    printf("The file pointer is at byte %ld\n", ftell(stream));
    fclose(stream);
    return 0;
}

4. 

Data written into a file using fwrite() can be read back using fscanf()

A. True
B. False

Answer: Option B

Explanation:

fwrite() - Unformatted write in to a file.
fscanf() - Formatted read from a file.


5. 

If the two strings are found to be unequal then strcmp returns difference between the first non-matching pair of characters.

A. True
B. False

Answer: Option A

Explanation:

g = strcmp(s1, s2); returns 0 when the strings are equal, a negative integer when s1 is less than s2, or a positive integer if s1 is greater than s2, that strcmp() not only returns -1, 0 and +1, but also other negative or positive values(returns difference between the first non-matching pair of characters between s1 and s2).

A possible implementation for strcmp() in "The Standard C Library".


int strcmp (const char * s1, const char * s2)
{                
	for(; *s1 == *s2; ++s1, ++s2) 
	{
		if(*s1 == 0)
			return 0;
	}
	return *(unsigned char *)s1 < *(unsigned char *)s2 ? -1 : 1;
}