-
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));
}
-
Its is compiling, what kind of error you are facing?
-
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.
-
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.
-
It does compile... those are warnings, not errors. Errors would result in no executable being produced. Warnings will result in an executable being produced.
-
[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
-
Ah! thanks for the replies.