Thread: trying to learn strlen

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    trying to learn strlen

    Hi,

    trying to learn strlen, and after typing out an example out of a book.

    For some reason, it doesn't work. =(

    I double checked that there is no typing/syntax errors. It's in german, but the code is in English =)

    Can anyone give me a clue as to why this doesn't compile?

    Thanks!
    Code:
    //* noch ein beispiel: strlen.c *//
    
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
        char string [100] = "So lang ist dieser String:";
        printf ("So gross ist das char-Array: %d\n", sizeof (string));
        printf ("%s %d\n", string, strlen (string));
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Its is compiling, what kind of error you are facing?

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    strange. I get the following error.

    Code:
    strlen.c: In function ‘main’:
    strlen.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
    strlen.c:10: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘size_t’
    I found another simple example online, and it also doesn't compile.

    This is my compiler version:
    gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
    maybe that has something to do with it.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Type specifier for `size_t` type, which is what `strlen()` and `sizeof` return, is `%zu`. Also, it's common to not put a space between function name and respective parenthesis in a function call; that's usually used for loops.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It does compile... those are warnings, not errors. Errors would result in no executable being produced. Warnings will result in an executable being produced.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    [QUOTE=jalisco;1088000]strange. I get the following error.

    Code:
    strlen.c: In function ‘main’:
    strlen.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
    strlen.c:10: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘size_t’
    The above mentioned are warnings not errors, it will not halt your compil

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Ah! thanks for the replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another strlen
    By voidX in forum C Programming
    Replies: 15
    Last Post: 01-09-2011, 11:24 PM
  2. You have to learn C in order to learn C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 07-16-2004, 10:33 AM
  3. strlen...int?
    By Extol in forum C++ Programming
    Replies: 9
    Last Post: 04-17-2003, 04:35 PM
  4. strlen
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2002, 11:57 PM
  5. Why O why, strlen?
    By Sebastiani in forum C Programming
    Replies: 11
    Last Post: 08-24-2001, 01:41 PM