Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 928751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:03:44+00:00 2026-05-15T20:03:44+00:00

Here, i have a program, which takes arguments (how surprising…). I want him to

  • 0

Here, i have a program, which takes arguments (how surprising…). I want him to have several arguments, as:

./myprogram -f filename.txt -x -o

so i want main args with “-“, and these arg shall accept an other arg, in the example, a filename.

I have this structure, very simple:

int main(int argc, char *argv[])
{
    printf("Program name: %s\n", argv[0]);

    while ((argc > 1) && (argv[1][0] == '-'))
    {
        switch (argv[1][1])
        {
            case 'f':
                printf("%s\n",&argv[1][3]);
                break;

            case 'd':
                printf("%s\n",&argv[1][2]);
                printf("%s\n",&argv[1][2]);
                break;

            default:
                printf("Wrong Argument: %s\n", argv[1]);
                usage();
        }

        ++argv;
        --argc;
    }


    return 0;
}

As you can see, in case of -d, this prints what’s following the argument, without space; here is a sample output:

./myprogram -dfilename
Program name: myprogram
filename
filename

and with the -f parameter,

./myprogram -f filename
Program name: myprogram
ffilename

it prints twice the first letter, and i don’t understand why.
Could someone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:03:44+00:00Added an answer on May 15, 2026 at 8:03 pm

    argv contains an array of strings. Argv[0] contains the program name, argv[1] contains -f in your case, argv[2] contains filename. If you print argv[1][3], you are printing the string starting at the third letter of -f. There is no such thing, so the behavior is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A slightly archaic question I'm afraid but here goes: I have a program which
Just a quick question here. I have a program in which I need to
I have written a program which takes a file as input and whenever it
I have a python program which takes input from stdin. Now, I've to write
I have a command-line program which takes input from stdin. What's the best way
I have a Windows program which generates forms. It takes the Top/Left properties at
I have a simple program which takes a video and plays it (though it
I'm at the end of my rope here: I have a single-threaded C++ program.
NOTE: This is a followup to my question here. I have a program that
I am trying to separate a few things in here. I have a program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.