argv is an array of pointers.
sizeof returns the size of its argument type.
The size of pointers on your system is 8 bytes (i.e., you are on a 64-bit system), so sizeof is returning 8.
To get the size of a string, you need to use strlen, which counts the number of characters up to (but not including) the first '\0' character in the string. I.e., it basically does this:
Code:
size_t my_strlen(const char *str)
{
const char *p = str;
while (*p != '\0')
++p;
return p - str; // return difference between final position
// and initial position of p.
}
Note that both sizeof and strlen return a size_t argument, so to print it you either need to use the proper format specifier ("%zu"), or you need to cast the value to match the format specifier that you use.
Code:
printf("%zu\n", strlen(argv[1]));
printf("%d\n", (int)strlen(argv[1]));
printf("%ld\n", (long)strlen(argv[1]));
The following program prints the sizes and values of all of the arguments passed on the command line:
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
for (int i = 0; i < argc; ++i)
printf("%2d: %s\n", (int)strlen(argv[i]), argv[i]);
return 0;
}