-1

I wrote read this code from the K&R book. But i compile it i get an error:

gcc: error: getchar.c: No such file or directory

gcc: fatal error: no input files

compilation terminated.

Code:

#include <sys/syscall.h> 
#include <stdio.h> 

int getchar(void)
{
char c;

return (read(0, &c,1) == 1) ? (unsigned char) c : EOF ;

}
main()
{
printf("\nEnter the character you want to getchar:  \n");
getchar();

return 0;
}
HELP PLZ
  • 41
  • 1
  • 9
  • 3
    What is your source file name? Are you giving right path when compiling with gcc? – awatan Jul 25 '14 at 17:20
  • 4
    This is totally unrelated to your source code (did you _read_ the compiler messages?) How do you compile and in which directory? – mafso Jul 25 '14 at 17:21
  • I complied other programs and seem to work fine (gcc file_name.c -o file) & executive it with (./file) – HELP PLZ Jul 25 '14 at 17:24
  • Do you want me share screenshot @mafso – HELP PLZ Jul 25 '14 at 17:25
  • @JewelTheif my souce file name is getchar.c i am compiling this code correctly and i am in right directory. Seem to compile other programs with gcc but not this one. – HELP PLZ Jul 25 '14 at 17:28
  • I assume it might beacause the getchar is an inbuilt function declared in stdio.h ,can you try renaming function to some another name?? – Sorcrer Jul 25 '14 at 17:40
  • 2
    Check the absolute path of getchar.c and then check your present working directory with "pwd" command. Or Try giving absolute path of source file when compiling. – awatan Jul 25 '14 at 17:55
  • @user3121023 it compile the same code in windows but not in linux whats the error? – HELP PLZ Jul 25 '14 at 19:42
  • @JewelThief the path is correct – HELP PLZ Jul 25 '14 at 19:43

2 Answers2

1

The error message is telling you that there is no file called getchar.c in the current directory to compile. That might mean that you accidentally called it getchar.c or getchar.c instead, so you should look carefully at what you called it -- it might look like it is correct, but have extra invisible characters in it.

The easiest fix is probably to open the file in your editor, and then "save as" and type in a name that has no invisible characters in it.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
0

If you're just compiling it in the Windows environment

try

#include <sys/syscall.h>

change to

#include <io.h> 
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • `getchar()` : It is desirable to have a different name, instead of the same name as a standard function in these cases. – BLUEPIXY Jul 25 '14 at 22:36