Thread: compiler error

  1. #1
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74

    compiler error

    Hi folks,

    In a book I'm reading about C, I have come across a program that was trying to demonstrate basically the same thing that King Mir was teaching me in another thread, how different types of data are alloted the same amount of memory, char, int, float etc...

    The context of this program was to show that the string has the same amount of memory regardless of what one puts in scanf. However I'm getting errors when I try to compile that I don't understand. I have read over the program in the book character by character about ten times now to make sure I typed it right, and as far as I can tell I typed it verbatim, anyways, here is the program + the errors:

    Code:
    /* praise2.c */
    
    #include <stdio.h>
    #include <string.h>
    #define PRAISE "What a super marvelous name!"
    
    int main()
    {
            char name[40];
    
            printf("\nWhat's your name?\n");
            scanf("%s", name);
            printf("\nHello, %s. %s\n", name, PRAISE);
            printf("\nYour name of %d letters occupies %d memory cells.\n", strlen(name), sizeof name);
            printf("\nThe phrase of praise has %d letters ", strlen(PRAISE));
            printf("\nand occupies %d memory cells.\n", sizeof PRAISE);
    
            return 0;
    }
    And the compiler errors:

    praise2.c: In function 'main':
    praise2.c:14:2: warning: format '%d' expects argument of type 'int', but argument 2 has type 'size_t' [-Wformat]
    praise2.c:14:2: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat]
    praise2.c:15:2: warning: format '%d' expects argument of type 'int', but argument 2 has type 'size_t' [-Wformat]
    praise2.c:16:2: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long unsigned int' [-Wformat]

    I have no idea what size_t is nor was I aware that I put a long unsigned int in this program... any ideas?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    A tip when reading strings with scanf. Use the dimension of the array, as limit to how many characters scanf will read, else you 'll get an overflow in bad input.
    In your code, that is
    Code:
    char name[40];
    scanf("%40s", name);
    Use the %u. For more, read the manual.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Firstly, those aren't errors, they're warnings. So the program was compiled and could be run.

    If you're compiling it as a C++ program, try compiling it as a C program.

    Or put (int) in front of the arguments:

    Code:
    printf("\nYour name of %d letters occupies %d memory cells.\n", (int)strlen(name), (int)sizeof name);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
            printf("\nYour name of %zd letters occupies %lu memory cells.\n", strlen(name), sizeof name);
            printf("\nThe phrase of praise has %zd letters ", strlen(PRAISE));
            printf("\nand occupies %lu memory cells.\n", sizeof PRAISE);
    > I have no idea what size_t is nor was I aware that I put a long unsigned int in this program... any ideas?
    They're the respective types returned by strlen and sizeof

    You're using gcc (which is a good thing), because you need to fix these sprintf/scanf format mis-matches before running the code.


    > scanf("%40s", name);
    It's a trap!
    The field width specifies the number of characters, not the storage space.
    You have to allow for the \0 as well, which makes it a real PITA to use scanf to use buffers safely.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by oogabooga View Post
    Firstly, those aren't errors, they're warnings. So the program was compiled and could be run.
    Ha, good point.

    Quote Originally Posted by oogabooga View Post
    Or put (int) in front of the arguments:
    That did the trick! I had no idea you could do that. Thanks!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Warnings are errors.

    The printf warnings would typically result in some weird results being printed.
    But you would ignore similar 'warnings' on scanf format mis-matches at your peril. Doing so would almost certainly cause a crash at run-time.

    A warning tells you that your program is only syntactically valid, but it could well be semantically flawed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf warnings could be as critical. I have had a lot of crashes caused by adding some logging lines... With mismatched parameters and format specifiers. Mostly by trying to print integer with %s specifier. It will cause printf scanning memory at some random location till it encounters 0 byte. As easily it can encounter memory that is protected from reading and cause a crash with access violation.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Not all warnings are programmer errors, but all warning should be understood and considered. This particular warning always indicates an error, however. The reason it's listed as a warning, is that the C standard does not require compilers to catch this particular error.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-13-2010, 10:02 PM
  2. GCC compiler giving syntax error before 'double' error
    By dragonmint in forum Linux Programming
    Replies: 4
    Last Post: 06-02-2007, 05:38 PM
  3. Compiler error error C2065: '_beginthreadex; : undeclared identifier
    By Roaring_Tiger in forum Windows Programming
    Replies: 3
    Last Post: 04-29-2003, 01:54 AM
  4. Compiler error or human error?
    By skyruler54 in forum C++ Programming
    Replies: 6
    Last Post: 09-06-2002, 02:27 PM
  5. fatal error C1001: INTERNAL COMPILER ERROR
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 12:07 PM