Find Output of Program
Practice and master this topic with our carefully crafted questions.
What will be the output of the program ?
#include<stdio.h>
int main()
{
int a = 250;
printf("%1d\n", a);
return 0;
}
Correct Answer: D. 250 ✅
Consider the following C program:
#include<stdio.h>
int main()
{
int a = 250;
printf("%1d\n", a);
return 0;
}
Note: The question contains an extra non-printable character after %1d. The intended format string is:
printf("%1d\n", a);
The format specifier:
%1d
%dprints an integer.1specifies the minimum field width.- If the number requires more than one character, the entire number is printed.
Since 250 requires three characters, the minimum field width of 1 has no effect.
Therefore, the output is:
250
- A.
1250❌
Incorrect. The field width does not become part of the output. - B.
2❌
Incorrect.%1ddoes not limit the number of digits printed. - C.
50❌
Incorrect. The format specifier does not truncate the value. - D.
250✅
Correct. A field width specifies only the minimum width, not the maximum.
Exam Fact:
For integer format specifiers, the field width in %wd specifies the minimum number of character positions. If the integer is wider than the specified width, printf() prints the entire value without truncation.
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;
}
Correct Answer: D. agpu ✅
Consider the following C 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;
}
The file contains:
This is Nagpur
Let's number the characters:
Index : 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Chars : T h i s _ i s _ N a g p u r
- When the file is opened, the file pointer is at the beginning (index
0). -
fseek(fp, 9L, SEEK_CUR);moves the file pointer 9 bytes forward from the current position, placing it at the character:a -
fgets(str, 5, fp);reads at most4characters (because one byte is reserved for the terminating'\0'). - The characters read are:
a g p u puts(str);prints:agpu
- A.
agpur❌
Incorrect.fgets(str, 5, fp)reads at most four characters. - B.
gpur❌
Incorrect. Reading starts from'a', not'g'. - C.
Nagp❌
Incorrect. The file pointer is positioned at'a', not'N'. - D.
agpu✅
Correct. The file pointer starts at'a', andfgets()reads the next four characters.
Exam Fact:
fseek(fp, offset, SEEK_CUR) moves the file pointer relative to its current position. Also, fgets(str, n, fp) reads at most n - 1 characters and appends a null character ('\0').
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;
}
Correct Answer: C. 1 ✅
Consider the following C program:
#include<stdio.h>
int main()
{
int i;
printf("%d\n", scanf("%d", &i));
return 0;
}
The scanf() function returns the number of input items successfully read and assigned.
Here, the format string contains only one conversion specifier:
"%d"
If the user enters:
25
scanf()successfully reads the integer25.- It stores the value in the variable
i. - Since exactly one input item is successfully assigned,
scanf()returns1.
Therefore, the statement:
printf("%d\n", scanf("%d", &i));
prints:
1
- A.
25❌
Incorrect. The value25is stored ini, but the program prints the return value ofscanf(), noti. - B.
2❌
Incorrect. Only one input item is read and assigned. - C.
1✅
Correct.scanf()returns the number of successfully matched and assigned input items. - D.
5❌
Incorrect. The number of digits entered does not affect the return value.
Exam Fact:
The return value of scanf() is the number of input items successfully assigned. It returns EOF if an input failure occurs before any assignment is made. Always check the return value of scanf() to ensure that the expected input was successfully read.