Exercise: Input Output

Questions for: Input Output

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

int main()
{
    char ch;
    int i;
    scanf("%c", &i);
    scanf("%d", &ch);
    printf("%c %d", ch, i);
    return 0;
}
A:
Error: suspicious char to in conversion in scanf()
B:
Error: we may not get input for second scanf() statement
C:
No error
D:
None of above
Answer: B
No answer description is available. Let's discuss.
Point out the error in the program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    unsigned char;
    FILE *fp;
    fp=fopen("trial", "r");
    if(!fp)
    {
        printf("Unable to open file");
        exit(1);
    }
    fclose(fp);
    return 0;
}
A:
Error: in unsigned char statement
B:
Error: unknown file pointer
C:
No error
D:
None of above
Answer: C

This program tries to open the file trial.txt in read mode. If file not exists or unable to read it prints "Unable to open file" and then terminate the program.

If file exists, it simply close the file and then terminates the program.

What will be the output of the program if value 25 given to scanf()?
#include<stdio.h>

int main()
{
    int i;
    printf("%d\n", scanf("%d", &i));
    return 0;
}
A:
25
B:
2
C:
1
D:
5
Answer: C

The scanf function returns the number of input is given.

printf("%d\n", scanf("%d", &i)); The scanf function returns the value 1(one).

Therefore, the output of the program is '1'.

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

int main()
{
    FILE *fp;
    char ch, str[7];
    fp=fopen("try.c", "r"); /* file 'try.c' contains "This is Nagpur" */
    fseek(fp, 9L, SEEK_CUR);
    fgets(str, 5, fp);
    puts(str);
    return 0;
}
A:
agpur
B:
gpur
C:
Nagp
D:
agpu
Answer: D
No answer description is available. Let's discuss.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int a=250;
    printf("%1d\n", a);
    return 0;
}
A:
1250
B:
2
C:
50
D:
250
Answer: D

int a=250; The variable a is declared as an integer type and initialized to value 250.

printf("%1d\n", a); It prints the value of variable a.

Hence the output of the program is 250.

Ad Slot (Above Pagination)
Quiz