1 Library Functions General Questions 1. What will the function rewind() do? A. Reposition the file pointer to a character reverse. B. Reposition the file pointer stream to end of file. C. Reposition the file pointer to begining of that line. D. Reposition the file pointer to begining of file. Answer: Option D rewind() takes the file pointer to the beginning of the file. so that the next I/O operation will take place at the beginning of the file. Example: rewind(filepointer); 2. Input/output function prototypes and macros are defined in which header file? A. conio.h B. stdlib.h C. stdio.h D. dos.h Answer: Option C stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations. 3. Which standard library function will you use to find the last occurance of a character in a string in C? A. strnchar() B. strchar() C. strrchar() D. strrchr() Answer: Option D strrchr() returns a pointer to the last occurrence of character in a string. Example: #include <stdio.h> #include <string.h>
2 char str[30] = "12345678910111213"; printf("the last position of '2' is %d.\n", strrchr(str, '2') - str); Output: The last position of '2' is 14. 4. What is stderr? A. standard error B. standard error types C. standard error streams D. standard error definitions Answer: Option C The standard error(stderr) stream is the default destination for error messages and other diagnostic warnings. Like stdout, it is usually also directed to the output device of the standard console (generally, the screen). 5. Does there any function exist to convert the int or float to a string? A. Yes B.No 1. itoa() converts an integer to a string. 2. ltoa() converts a long to a string. 3. ultoa() converts an unsigned long to a string. 4. sprintf() sends formatted output to a string, so it can be used to convert any type of values to string type. #include<stdlib.h> int main(void) int num1 = 12345; float num2 = 5.12; char str1[20]; char str2[20]; itoa(num1, str1, 10); /* 10 radix value */ printf("integer = %d string = %s \n", num1, str1); sprintf(str2, "%f", num2); printf("float = %f string = %s", num2, str2);
3 // Output: // integer = 12345 string = 12345 // float = 5.120000 string = 5.120000 6. What is the purpose of fflush() function. A. flushes all streams and specified streams. B. flushes only specified stream. C. flushes input/output buffer. D. flushes file buffer. "fflush()" flush any buffered output associated with filename, which is either a file opened for writing or a shell command for redirecting output to a pipe or coprocess. Example: fflush(filepointer); fflush(null); flushes all streams. 7. Can you use the fprintf() to display the output on the screen? A. Yes B.No Do like this fprintf(stdout, "%s %d %f", str, i, a); 8. What will the function randomize() do in Turbo C under DOS? A. returns a random number. B. returns a random number generator in the specified range. C. returns a random number generator with a random value based on time. D. return a random number with a given seed value. Answer: Option C The randomize() function initializes the random number generator with a random value based on time. You can try the sample program given below in Turbo-C, it may not work as expected in other compilers. /* Prints a random number in the range 0 to 99 */ #include <stdlib.h>
4 #include <stdio.h> #include <time.h> int main(void) randomize(); printf("random number in the 0-99 range: %d\n", random (100)); Find Output of Program 1. What will be the output of the program? int i; i = printf("how r u\n"); i = printf("%d\n", i); printf("%d\n", i); How r u A. 7 2 How r u C. 1 1 How r u B. 8 2 D. Error: cannot assign printf to variable In the program, printf() returns the number of charecters printed on the console i = printf("how r u\n"); This line prints "How r u" with a new line character and returns the length of string printed then assign it to variable i. So i = 8 (length of '\n' is 1). i = printf("%d\n", i); In the previous step the value of i is 8. So it prints "8" with a new line character and returns the length of string printed then assign it to variable i. So i = 2 (length of '\n' is 1). printf("%d\n", i); In the previous step the value of i is 2. So it prints "2". 2. What will be the output of the program? #include<math.h> float i = 2.5; printf("%f, %d", floor(i), ceil(i));
A. 2, 3 B. 2.000000, 3 C. 2.000000, 0 D. 2, 0 Answer: Option C Both ceil() and floor() return the integer found as a double. 5 floor(2.5) returns the largest integral value(round down) that is not greater than 2.5. So output is 2.000000. ceil(2.5) returns 3, while converting the double to int it returns '0'. So, the output is '2.000000, 0'. 3. What will be the output of the program? int i; i = scanf("%d %d", &i, &i); printf("%d\n", i); A. 1 B. 2 C. Garbage value D. Error: cannot assign scanf to variable scanf() returns the number of variables to which you are provding the input. i = scanf("%d %d", &i, &i); Here Scanf() returns 2. So i = 2. printf("%d\n", i); Here it prints 2. 4. What will be the output of the program? int i; char c; for(i=1; i<=5; i++) scanf("%c", &c); /* given input is 'b' */ ungetc(c, stdout); printf("%c", c);
ungetc(c, stdin); A. bbbb B. bbbbb C. b D. Error in ungetc statement. Answer: Option C The ungetc() function pushes the character c back onto the named input stream, which must be open for reading. This character will be returned on the next call to getc or fread for that stream. One character can be pushed back in all situations. A second call to ungetc without a call to getc will force the previous character to be forgotten. 6 5. What will be the output of the program? #include<stdlib.h> char *i = "55.555"; int result1 = 10; float result2 = 11.111; result1 = result1+atoi(i); result2 = result2+atof(i); printf("%d, %f", result1, result2); A. 55, 55.555 B. 66, 66.666600 C. 65, 66.666000 D. 55, 55 Answer: Option C Function atoi() converts the string to integer. Function atof() converts the string to float. result1 = result1+atoi(i); Here result1 = 10 + atoi(55.555); result1 = 10 + 55; result1 = 65; result2 = result2+atof(i); Here result2 = 11.111 + atof(55.555); result2 = 11.111 + 55.555000;
7 result2 = 66.666000; So the output is "65, 66.666000". 6. What will be the output of the program? #include<string.h> char dest[] = 97, 97, 0; char src[] = "aaa"; int i; if((i = memcmp(dest, src, 2))==0) printf("got it"); else printf("missed"); A. Missed B. Got it C. Error in memcmp statement D. None of above memcmp compares the first 2 bytes of the blocks dest and src as unsigned chars. So, the ASCII value of 97 is 'a'. if((i = memcmp(dest, src, 2))==0) When comparing the array dest and src as unsigned chars, the first 2 bytes are same in both variables.so memcmp returns '0'. Then, the if(0=0) condition is satisfied. Hence the output is "Got it". 7. What will function gcvt() do? A. Convert vector to integer value B. Convert floating-point number to a string C. Convert 2D array in to 1D array. D. Covert multi Dimensional array to 1D array The gcvt() function converts a floating-point number to a string. It converts given value to a null-terminated string. #include <stdlib.h> #include <stdio.h> int main(void) char str[25];
8 double num; int sig = 5; /* significant digits */ /* a regular number */ num = 9.876; gcvt(num, sig, str); printf("string = %s\n", str); /* a negative number */ num = -123.4567; gcvt(num, sig, str); printf("string = %s\n", str); /* scientific notation */ num = 0.678e5; gcvt(num, sig, str); printf("string = %s\n", str); return(0); Output: string = 9.876 string = -123.46 string = 67800 8. What will be the output of the program? int i; char c; for(i=1; i<=5; i++) scanf("%c", &c); /* given input is 'a' */ printf("%c", c); ungetc(c, stdin); A. aaaa B. aaaaa C. Garbage value. D. Error in ungetc statement. for(i=1; i<=5; i++) Here the for loop runs 5 times. Loop 1: scanf("%c", &c); Here we give 'a' as input. printf("%c", c); prints the character 'a' which is given in the previous "scanf()" statement. ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream. Loop 2:
Here the scanf("%c", &c); get the input from "stdin" because of "ungetc" function. printf("%c", c); Now variable c = 'a'. So it prints the character 'a'. ungetc(c, stdin); "ungetc()" function pushes character 'a' back into input stream. This above process will be repeated in Loop 3, Loop 4, Loop 5. 9 Point Out Errors 1. Point out the error in the following program. fprintf("sbdsisaikat"); printf("%.ef", 2.0); A. Error: unknown value in printf() statement. B. Error: in fprintf() statement. C. No error and prints "Sbdsisaikat" D. No error and prints "2.0" Declaration Syntax: int fprintf (FILE *stream, const char *format [, argument,...]); Example: fprintf(filestream, "%s %d %s", Name, Age, City); 2. Point out the error in the following program. #include<string.h> char str1[] = "Learn through Sbdsisaikat\0.com", str2[120]; char *p; p = (char*) memccpy(str2, str1, 'i', strlen(str1)); *p = '\0'; printf("%s", str2); A. Error: in memccpy statement B. Error: invalid pointer conversion C. Error: invalid variable declaration D. No error and prints "Learn through Indi"
10 Answer: Option D Declaration: void *memccpy(void *dest, const void *src, int c, size_t n); : Copies a block of n bytes from src to dest With memccpy(), the copying stops as soon as either of the following occurs: => the character 'i' is first copied into str2 => n bytes have been copied into str2 3. Point out the error in the following program. char str[] = "Sbdsisaikat"; printf("%.#s %2s", str, str); A. Error: in Array declaration B. Error: printf statement C. Error: unspecified character in printf D. No error Answer: Option D True / False Questions 1. It is necessary that for the string functions to work safely the strings must be terminated with '\0'. A. True B.False 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
11 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 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); 4. Data written into a file using fwrite() can be read back using fscanf() A. True B.False 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.
12 A. True B.False 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 *(unsigned char *)s1 < *(unsigned char *)s2? -1 : 1; 1. Is standard library a part of C language? A. Yes B.No The C standard library consists of a set of sections of the ISO C standard which describe a collection of header files and library routines used to implement common operations, such as input/output and string handling, in the C programming language. The C standard library is an interface standard described by a document; it is not an actual library of software routines available for linkage to C programs. 2. Will the program outputs "Sbdsisaikat.com"? #include<string.h> char str1[] = "Sbdsisaikat.com"; char str2[20]; strncpy(str2, str1, 8); printf("%s", str2); A. Yes B.No
13 No. It will print something like 'Sbdsisaikat(some garbage values here)'. Because after copying the first 8 characters of source string into target string strncpy() doesn't terminate the target string with a '\0'. So it may print some garbage values along with Sbdsisaikat. 3. The itoa function can convert an integer in decimal, octal or hexadecimal form to a string. A. Yes B.No itoa() takes the integer input value input and converts it to a number in base radix. The resulting number a sequence of base-radix digits. Example: /* itoa() example */ #include <stdio.h> #include <stdlib.h> int main () int no; char buff [50]; printf ("Enter number: "); scanf ("%d",&no); itoa (no,buff,10); printf ("Decimal: %s\n",buff); itoa (no,buff,2); printf ("Binary: %s\n",buff); itoa (no,buff,16); printf ("Hexadecimal: %s\n",buff); Output: Enter a number: 1250 Decimal: 1250 Binary: 10011100010 Hexadecimal: 4e2 4. The prototypes of all standard library string functions are declared in the file string.h. A. Yes B.No
14 string.h is the header in the C standard library for the C programming language which contains macro definitions, constants, and declarations of functions and types used not only for string handling but also various memory handling functions. 5. scanf() or atoi() function can be used to convert a string like "436" in to integer. A. Yes B.No scanf is a function that reads data with specified format from a given string stream source. scanf("%d",&number); atoi() convert string to integer. var number; number = atoi("string");