Forum: PC-Programmierung MinGW Kommandozeilen Argumente werden nicht übergeben


von Jörg Stamm (Gast)


Lesenswert?

Hallo,

ich habe ein "Hello World" Programm, das ich mit MinGw kompiliere.
Leider werden die Kommandozeilen Argumente nicht erkannt und beim 
Kompilieren auch ignoriert. Das Programm endet mit der ersten Nutzung 
von argc.

Wie kann ich die Kommandozeilen Argumente sichtbar machen ?

------------ gcc
gcc (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is 
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR 
PURPOSE.

------------ mingw32-make
GNU Make 4.2.1
Built for i686-w64-mingw32
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
<http://gnu.org/licenses/gpl.html>;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


------------ Code


#include <stdio.h>

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

  printf("Hello, World !");


  int i=0;

  printf("\nindex=%u", i);

  printf("\ncmdline args count=%s", argc);


  /* First argument is executable name only */

  printf("\nexe name=%s", argv[0]);


  for (i=1; i< argc; i++) {

    printf("\narg%d=%s", i, argv[i]);

  }


  printf("\n");

  return 0;

}


------------ Ausgabe beim Kompilieren

echo hello world

hello world

gcc main.c -o hello.exe


------------ Aufruf von hello.exe:

...>hello 1 2 3

Hello, World !

index=0

*keine weiteren Ausgaben, das Programm endet mit der ersten Nutzung von 
argc

von g457 (Gast)


Lesenswert?

> das Programm endet mit der ersten Nutzung von argc

> printf("\ncmdline args count=%s", argc);

∗hust∗ nochmal genau hinschauen, da sollte eigentlich sogar eine 
Compilerwarnung kommen.

HTH

von Hmmm (Gast)


Lesenswert?

Jörg Stamm schrieb:
> printf("\nindex=%u", i);
> printf("\ncmdline args count=%s", argc);

Bei beiden gehört %d hin, das %s ist hier tödlich.

von leo (Gast)


Lesenswert?

Jörg Stamm schrieb:
> printf("\ncmdline args count=%s", argc);

Falscher Formatspecifier. Dreh mal die Warnungen auf.

leo

von Jörg Stamm (Gast)


Lesenswert?

*hit!
Danke euch.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.