C Programming - Input / Output
Exercise : Input / Output - Find Output of Program
6.
What will be the output of the program ?
#include<stdio.h>
int main()
{
printf("%c\n", ~('C'*-1));
return 0;
}
7.
What will be the output of the program ?
#include<stdio.h>
int main()
{
FILE *fp;
unsigned char ch;
/* file 'abc.c' contains "This is IndiaBIX " */
fp=fopen("abc.c", "r");
if(fp == NULL)
{
printf("Unable to open file");
exit(1);
}
while((ch=getc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
printf("\n", ch);
return 0;
}
Answer: Option
Explanation:
The macro EOF means -1.
while((ch=getc(fp)) != EOF) Here getc function read the character and convert it to an integer value and store it in the variable ch, but it is declared as an unsigned char. So the while loop runs infinitely.
8.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char *p;
p="%d\n";
p++;
p++;
printf(p-2, 23);
return 0;
}
9.
What will be the output of the program ?
#include<stdio.h>
int main()
{
FILE *ptr;
char i;
ptr = fopen("myfile.c", "r");
while((i=fgetc(ptr))!=NULL)
printf("%c", i);
return 0;
}
Answer: Option
Explanation:
The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.
10.
What will be the output of the program ?
#include<stdio.h>
int main()
{
printf("%%%%\n");
return 0;
}
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers