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;
}
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.
If you're just compiling it in the Windows environment
try
#include <sys/syscall.h>
change to
#include <io.h>
Related
I'm currently doing an exercise where I have to create a program that takes all the code that is written inside it, and outputs it to the screen when the program is executed.
The exercise suggests that we may find it appropriate to change the file names of the program in the future - and assuming that the renaming is done in a coordinated manner, i.e. the source file and the execution file are given the same new name (except for the extension), the program should work correctly, without the need for any changes to the source code, and without the need to recompile.
The C program itself is called 'prnt.c'-
I wrote the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENDFILEC ".c" /* extension */
int main(int argc, char *argv[])
{
FILE *filePointer;
int character;
char *fileNameToOpen;
fileNameToOpen = (char *)malloc(strlen(argv[0]) + 3); /* allocating memory for string + 3 for the extension - '.c' and \0 */
strcpy(fileNameToOpen, argv[0]);
strcat(fileNameToOpen , ENDFILEC); /* ending '.c' in the end */
filePointer = fopen(fileNameToOpen, "r");
while(!feof(filePointer))
{
character = fgetc(filePointer);
printf("%c" , character);
}
fclose(filePointer);
return 0;
}
I made a 'makefile' to compile the program and I made it so that the executable would be called 'prnt1'.
basically, like the following:
prnt1 : prnt.c
gcc -ansi -Wall -pedantic prnt.c -o prnt1
The compilation worked, but whenever I run the program itself, it gives me a runtime error, saying: "Segmentation fault (core dumped)". When I look at the code itself, I don't seem to reach a memory that doesn't belong to me, so what could be an explanation for that problem and what can be done about it? Thank you in advance for your help.
Since you said that the executable is named "prnt1" and the source file (which you want to read the code from) is named "prnt", argv[0] has the name of the executable (i.e. "prnt1") and, when ".c" is appended to argv[0], it becomes "prnt1.c" – which is definitely not the file you are trying to read from; athen, since this file doesn't exist, you're getting a segmentation fault.
So, as Tom Karzes said, always check the return value of fopen().
This is the code that was provided as starter code on which I have to answer a couple of question using the data provided in data.txt file. data.txt is kept in the same folder as my code and contains line separated words all of which are of length 21. I am familiar with C but I do not know anything related to files and file management.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main(void) {
char* fname = "data.txt";
FILE *fptr = NULL;
char line[20000][22];
int i = 0;
fptr = fopen(fname, "r");
while(fgets(line[i],20000,fptr))
{
line[i][strlen(line[i]) - 1] = '\0';
i++;
}
printf("Read a file with %d lines.\n",i);
}
When I run this code in CLion, the following error appears.
I am using LLVM clang compiler. The same error appears even if I use Microsoft Visual Studio compiler. It would be helpful if you can explain what the error means and how to resolve it!
Please find below the code and the output which I am getting.
My C code is in c:/turboc3/bin directory
and my output macid.txt is in c:/turboc3/disk.
Here is the code which I am compiling
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main ()
{
//char mac[200];
FILE *fp;
clrscr();
// fp=fopen("c:\macid.txt","w");
system("GETMAC>c:/macid.txt");
fp=fopen("c:/macid.txt","r");
if(fp!=NULL)
{
char line[128];
while(fgets(line,sizeof line,fp)!=NULL)
{
char *nwln=strchr(line,'\n');
char *ptr;
if(nwln!=NULL)
*nwln='\0';
ptr=strstr(line,"Physical Address");
if(ptr!=NULL)
{
printf("%s\n",ptr);
break;
}
}
}
getch();
return 0;
}
The output is:
Illegal command: GETMAC.
Can anyone guide me through this?
Illegal command: GETMAC implies, that it tries to run it but can't.
First, things to fix : Remember to escape \ inside string. Do not use / as path separator.
Then, Things to try, in approximate order: Try with full path to GETMAC. Try without redirection. Try with different program. Wrap GETMAC into bat file, which does the redirection.
I am not sure how your file macid.txt resides in c:/turboc3/disk, but - I think you can solve this problem by giving a full path to the system() function call.
LIke this:
system("c:/windows/system32/getmac.exe > macid.txt");
I wrote little program to print "Hello world" in C. I'm not a C programmer, but I liked to try it. In my program there is an error. Please tell me what is it?
This is my program:
int main(){
printf("Hello World");
}
I wrote this with my Java Experiences. I can't find what is wrong.
You can't directly use printf() function as in Java. You should tell the compiler that you are going to use the input/output stream. You can tell it in this line:
#include <stdio.h>
and also you should enter this line at the end of the source code:
return 0;
this will tell the compiler :
"If the program succeed It will return 0 otherwise It will return any other number"
This means if your program is successful main() function will return 0. Then the compile know the program is Ok.
Then at last your complete code is:
#include <stdio.h>
int main() {
printf("Hello world");
return 0;
}
To compile this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. And this compile code is for windows. If your OS is UNIX like OS. then use this code to compile:
gcc hello.c -o hello
./hello
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
First, you have to use a header file.
#include <stdio.h>
What that does is bring up a header file with a bunch of commands in them. That will make it recognize the "printf" piece of code.
Next, you have to close your program. By not having a closing statement, the program will not compile because it doesn't know if that is the end of the code. Use this at the end of your program...
return 0;
That closes the program, meaning that the compiler can stop looking for other code. You may also want to look at some programming manuals (there are dozens of free online ones) to learn about the syntax.
One last thing: Most pieces of C code require a semicolon at the end. This is not true for the "int main" statement, nor is it for the header file which I have defined above. The "return" function that closes the program, does however, need a semicolon.
Hoped this helped.
Should also include a pause at the end:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
//Read a character from the console
getchar();
return 0;
}
Just like import in Java programs, in here you have to include libraries you're using in your program. You have used library function printf, but not included stdio.h.
I agree there are many ways to write one of the simplest way is
#include<stdio.h>
int main(void){
printf("Hello World\n");
return 0;
}
You can even use different ways as suggested above.
You should first look at the structure of "main". Try to understand the various parts as already explained so well in the above answers.
"#include" : The preprocessing directives to be included in the program. But why? Because you are trying to use the functions defined inside them.
int : The return type of "main" program. But why? Because the function calling "main" needs to know if the "main" program has functioned correctly.
main : The entry point of your code. Dont ask why here :-)
main( void ) : To tell the compiler that we are not passing any arguments to program "main"
return 0 : Beacuse you promised "main" that you will return something if "main" will function properly.
Finally the code:
#include <stdio.h>
int main( void )
{
printf( "Hello World\n" ) ; //Notice the '\n' here. Good coding practice.
return 0 ;
}
#include <stdio.h> //Pre-processor commands<br/>
void main() //Starting point of the program<br/>{ //Opening Braces
printf("Hello World\n"); //Print Hello World on the screen<br/>
return 0;
} //Ending Braces
Check it once it will work, I have written it with comments:
#include<stdio.h> //Pre-processor commands
void main() {
printf("Hello World\n"); //Print Hello World on the screen
}
A full hello world program in C:
#include <stdio.h>
int main(void) {
printf("Hello World\n");
return 0;
}
Then compile (assuming gcc) and execute it:
gcc -o test test.c
./test
You can't use printf() function as in Java. You have to tell the compiler what you are going to use.
You can tell this as follows:-
#include <stdio.h>
You must enter this line in last:-
return 0;
Then Your complete code is:-
#include <stdio.h>
int main(){
printf("Hello World");
return 0;
}
For compiling this and see the word "Hello World", just save this file as a .c file and Open cmd in your program directory and type:-
gcc hello.c -o hello && hello
(Replace the 'hello.c' with your filename, and 'hello' with the name you want to put with your .exe file)
Remember My computer is Windows. So I can compile only for Windows OS.
#include <stdio.h>
int main() {
// printf, used to print (display) Hello World
printf("Hello World ! ");
// return 0, as the main function is of type int so it must return an integer value
return 0;
}
Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++