Thread: How to find size of command line arguments?

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    5

    How to find size of command line arguments?

    Question about C , is `argv' 8 bytes? Asking because I'm trying to find the sizeof of my command line arguments. It's returning 8 bytes, but was expecting 1, since I read that all command line args are chars. Here's the code I wrote:


    Code:
    #include <stdio.h>
    
    
    int main(int argc, char* argv[]) {
    // User enters parameters for the argument
    // Get the argument for argv[1], read in the key in that positon of the argument
    printf("\nThis is the first argument after the program name: %s\n", argv[1]);
    // Get the type
    printf("\nThis is the size of the first argument: %ld\n", sizeof(argv[1]));
    }

    Here's my output:


    This is the first argument after the program name: 5


    This is the size of the first argument: 8

    We still haven't covered pointers yet in my class, that's the next lesson

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,485
    but was expecting 1, since I read that all command line args are chars.
    The command line arguments are an array of C-strings not single characters.

    What did you enter for your command line arguments? What is the value of argc?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    3,748
    To find the length in characters use strlen
    Note: strlen does not count the zero byte at the end.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    The command line arguments are an array of C-strings not single characters.

    What did you enter for your command line arguments? What is the value of argc?
    Thank you for your reply. I entered: ./main 5
    And the output was 8.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    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;
    }
    Last edited by john.c; 02-21-2019 at 11:59 AM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguments
    By jeedi khan in forum C Programming
    Replies: 9
    Last Post: 07-30-2013, 03:33 AM
  2. Command Line Arguments
    By DuckCowMooQuack in forum C Programming
    Replies: 5
    Last Post: 03-30-2011, 03:33 PM
  3. command line arguments
    By KBriggs in forum C Programming
    Replies: 7
    Last Post: 06-25-2009, 01:57 PM
  4. Command line arguments
    By Kelvin in forum C Programming
    Replies: 3
    Last Post: 07-12-2002, 08:22 AM

Tags for this Thread