Exercise: Library Functions

Questions for: Library Functions

Point out the error in the following program.
#include<stdio.h>

int main()
{
    char str[] = "ExamAdept";
    printf("%.#s %2s", str, str);
    return 0;
}
A:
Error: in Array declaration
B:
Error: printf statement
C:
Error: unspecified character in printf
D:
No error
Answer: D
No answer description is available. Let's discuss.
Point out the error in the following program.
#include<stdio.h>
#include<string.h>

int main()
{
    char str1[] = "Learn through ExamAdept\0.com",  str2[120];
    char *p;
    p = (char*) memccpy(str2, str1, 'i', strlen(str1));
    *p = '\0';
    printf("%s", str2);
    return 0;
}
A:
Error: in memccpy statement
B:
Error: invalid pointer conversion
C:
Error: invalid variable declaration
D:
No error and prints "Learn through Indi"
Answer: 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

Point out the error in the following program.
#include<stdio.h>

int main()
{
    fprintf("ExamAdept");
    printf("%.ef", 2.0);
    return 0;
}
A:
Error: unknown value in printf() statement.
B:
Error: in fprintf() statement.
C:
No error and prints "ExamAdept"
D:
No error and prints "2.0"
Answer: B

Declaration Syntax:
int fprintf (FILE *stream, const char *format [, argument, ...]);

Example:
fprintf(filestream, "%s %d %s", Name, Age, City);

What will be the output of the program?
#include<stdio.h>

int main()
{
    int i;
    char c;
    for(i=1; i<=5; i++)
    {
        scanf("%c", &c); /* given input is 'a' */
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}
A:
aaaa
B:
aaaaa
C:
Garbage value.
D:
Error in ungetc statement.
Answer: B

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.

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
Answer: B

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

Ad Slot (Above Pagination)
Quiz