简体   繁体   中英

How to fix unusual error while compiling c program on linux mint?

I have written a C program. It compiles and works fine on DevC on Windows 7. But when I compile it on Linux mint (using 'gcc main.c' command) it does not compile and give errors. These errors are not shown while compiling on Windows 7. So nothing must be wrong on Linux as well! How to compile it on Linux through gcc ?

C Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
   char command[100];

   printf("Enter the command:");
   scanf("%[^\t\n]", &command);
   printf("%s\n", command);
   strchr(command, '&');

   printf("%i", strchr(command, '&'));

   system("PAUSE"); 

   return 0;
}

Errors:

mint@mint ~ $ gcc ass1/main.c
ass1/main.c: In function 'main':
ass1/main.c:8:5: warning: format '%[^   
' expects argument of type 'char *', but argument 2 has type 'char (*)[100]' [-Wformat]
ass1/main.c:11:3: warning: incompatible implicit declaration of built-in function 'strchr' [enabled by default]
ass1/main.c:13:5: warning: format '%i' expects argument of type 'int', but argument 2 has type 'char *' [-Wformat]

These errors are not shown while compiling on windows 7. So nothing must be wrong on linux as well!

That's a wrong conclusion. In this case, the compiler on Windows is far more lenient than gcc.

gcc warns you about your mistakes/errors, in

scanf("%[^\t\n]", &command);

you pass the address of command where you should pass the address of the first byte in command , either as command with the automatic array-to-pointer conversion, or explicitly as &command[0] .

You use strchr without declaring it, an error in non-ancient versions of C, but allowed previously, where a use implicitly declared a function as returning an int . strchr however, returns a char* .

And in your printf call, you use the wrong format, %i .

gcc is completely correct here.

Note that those are warnings , and (unfortunately) not errors .

Those aren't errors, they're warnings. Your code should have still compiled.

The first warnings is because you're passing &command to scanf , which is of type char (*)[100] , and the specifier %s expects an argument of type char * . All you simply need to do is pass command to scanf (without the & ), since a char array will decay into a char* when passed to a function.

You'll probably find that the code still works, with command and &command both referring to the same address ( printf("%p %p", command, &command); ).


The second warning is due to you forgetting to include <string.h> , which declares strchr . Since the compiler can't find the declaration, it implicitly generates one, which doesn't turn out to match the real one.


Lastly, strchr returns a char* , and the specifier %i is intended to be used for int s. If you want to print out an address using printf , use the %p specifier.


You should also probably avoid system("PAUSE"); (which won't work on Linux), and replace it with a function that waits for user input.

Integrating the previous answers, the code that will compile on Linux and will work:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // use this header to include strchr(command, '&');

int main(int argc, char *argv[])
{
   char command[100];

   printf("Enter the command:");
   scanf("%s", command);
   printf("%s\n", command);
   strchr(command, '&');

   printf("%p", strchr(command, '&')); 
   /* strchr(command, '&') returns a pointer so you need to tell printf you are printing one. */

   system("PAUSE"); 

   return 0;
}

The output:

oz@Linux:~$ gcc -Wall test.c
test.c: In function ‘main’:
test.c:12:4: warning: statement with no effect [-Wunused-value]
oz@Linux:~$ ./a.out 
Enter the command:doSomething
doSomething
sh: 1: PAUSE: not found

Instead of

  system("PAUSE");

use: printf("Press 'Enter' to continue: ..."); while ( getchar() != '\\n') {
i=1; } getchar(); return 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM