Menu

Could not get the "%s" string function

Chris
2008-03-12
2012-09-26
  • Chris

    Chris - 2008-03-12

    I could not get the %s strinf function to work for the elementary C tutorial that comes with your compiler. For instance

    include <stdio.h>

    include <string.h>

    main ()

    {
    int a=3,b=-5;
    double e=7.8945783;
    char eh='h';
    printf ("C standard I/O file is included\n");
    printf ("Hello world!");
    printf("\n");
    printf("%s", eh) ;
    / printf("%-25s, Hello")/
    printf ("%-10d %-10d",a,b);
    printf ("%-#10d %#-12.5g",a,e);

    system("PAUSE");
    }
    compiles without error but when I execute (run ) the code I get an error report saying that the exe code has an error and whether I should send error reprot to Microsoft. Could you rectify this ?
    It died at the printf("%s", eh) ; statement.
    Please help Chris

     
    • cpns

      cpns - 2008-03-12

      The variable eh is a character not a string so %s is an inappropriate format specifier. The compiler option -Wformat will cause a warning to be generated for that code. I suggest that in fact you habitually use -Wall -Wformat -Werror, then the code would not even have compiled - whch is exactly what you want to happen when code contains errors.

      Note that the "C tutorial that comes with your compiler" is a pile of doo. It is very old and very flawed (and was even when it was new). Get your education from elsewhere. Besides would you not rather be learning C++? ;-) Few are prefect, but you could do worse than http://www.cplusplus.com/doc/tutorial/

      Note also that it is not "our" compiler. This is a user community, and Dev-C++ is not a compiler in any case, it is an IDE for the GNU compiler. The version of GCC that you are using comes from www.mingw.org.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.