C Programming - Input / Output - Discussion

1. 

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: Option C

Explanation:

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.


Chris said: (May 11, 2011)  
Tried it in the MS compiler, A is correct!

Muthu said: (Aug 3, 2011)  
Does unsigned char is correct? Will it work in TurboC compiler? But is works in gcc compiler too ?

Kaushik said: (Aug 22, 2011)  
It gave error in DEV C++ also.

Ivo said: (May 31, 2012)  
It works in Turbo C, but only if declared at the beginning of main...

Chandu said: (Jul 20, 2012)  
What do you mean by unsigned char; in this program ?

Guru said: (Sep 24, 2012)  
Syntax is correct but unsigned int leads to infinite loop while checking EOF(-1).

Abc said: (Sep 27, 2012)  
Char is keyword. It cannot be redeclared.

Veena Sameera said: (Dec 4, 2012)  
Why the compilation of a program from one compiler to another compiler varies (some times) ? we need output which accepts globally. But in some perspectives it may not.

Abhimanyu said: (Jul 5, 2013)  
It gives error with code blocks.

Prashant Choudhary said: (Aug 6, 2013)  
A is correct in any modern compiler. Please do not rely on turbo c.

Gangadhar said: (Sep 20, 2013)  
Option A is correct.

unsigned char; //this is wrong.

A compilation error due to bad syntax of declaration.

Swapnil said: (Jul 6, 2014)  
Depends on compiler to compiler.

In Turbo C it will give error to unsigned char;.

Faraz Ahmad Ramish said: (Sep 20, 2014)  
When there is no extension given along with file name then how come trail.txt in explanation?

Amit Saxena said: (May 13, 2016)  
I found the option [A] correct. How can a declaration remain incomplete without var?

Vidya said: (May 21, 2017)  
It is showing error in my compiler. How the answer is 'c' instead of 'a'?

Please explain me.

Aditya Dixit said: (Nov 6, 2017)  
In my GCC Compiler, it is showing error. The answer should the option (a). If anyone from the Website is seeing this please review the Answer again.

Aditi said: (Dec 11, 2019)  
Can anyone explain line by line execution?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.