0

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]
Simone-Cu
  • 1,109
  • 8
  • 21
  • `scanf("%[^\t\n]", &command);` -- this is wrong. For a char array using %s you would simply pass `command` as the param. That's the warning on line 8. Line 13 is complaining that strchr returns a char, but you're formatting it as an int. Use %c. – Joe Sep 29 '12 at 13:56
  • Also, strchr is defined in on many platforms. You should #include that to get rid of the implicit declaration warning. – Joe Sep 29 '12 at 13:58
  • Lastly: system("PAUSE"); won't work on Linux, there's no PAUSE command. But you shouldn't need this anyway. – Joe Sep 29 '12 at 13:59
  • strchr is also included in string.h, that you don't include. Another thing: I don't think that system("PAUSE") works in linux. You can wait for an input using getchar for example. – Simone-Cu Sep 29 '12 at 14:00
  • The strchr problem is solved. And I used the same piece of code on windows. It compiled without errors and gave the correct results. – user1501587 Sep 29 '12 at 14:01
  • 1
    Dev-C++ is a crappy compiler. – Joe Sep 29 '12 at 14:03

3 Answers3

3

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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • If you want to print the pointer value, `printf("%p\n", (void*)strchr(command, '&));` is the correct way. But it may be possible that your Windows compiler doesn't know that format (it's C99, as far as I remember), then casting the pointer to an integer type may be what you want. – Daniel Fischer Sep 29 '12 at 14:11
3

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 ints. 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.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
0

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;

oz123
  • 27,559
  • 27
  • 125
  • 187
  • Pay attention at the last line of the output. It is warning you that it's trying to execute the PAUSE command. – Simone-Cu Sep 29 '12 at 14:12
  • 1
    @Simone-Cu: `alias PAUSE=true` ;) – Daniel Fischer Sep 29 '12 at 14:14
  • @Simone-Cu, that is how the OP typed the program, it's because the Prompt in windows always closes after the program ends. – oz123 Sep 29 '12 at 14:16
  • @Oz123 I know that, but I think it's better to avoid it on linux. And what about a solution with `getchar(); getchar();`? Is there a better way to manage waiting for that's "cross-platform"? – Simone-Cu Sep 29 '12 at 14:34
  • @Simone-Cu, I have no clue, I am not the best C programmer, still learning this weired language :-( – oz123 Sep 29 '12 at 14:44