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 6666223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:48:21+00:00 2026-05-26T02:48:21+00:00

Here’s what I have: #include <stdlib.h> #include <string.h> #include <stdio.h> int main(int argc, char

  • 0

Here’s what I have:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char **argv)
{

    while(*argv++ != 0)
    {
            printf("Argument!\n");
            printf("%s %d\n",*argv,(int)strlen(*argv));
            int i = 0;

            while(*argv[i])
            {
                    printf("char!\n");
                    printf("%c\n",*argv[i]);
                    i++;
            }

            printf("End of for loop\n");
    }

    return 0;
}

When I run ./a.out test, the output is:

Argument!
test 4
char!
t
Segmentation Fault

I’ve been staring at this for a few hours. Why won’t my program print each command line argument character by character?

I’m new to C, and the array-pointer duality, so I wouldn’t be surprised if that were the problem. Any help is appreciated!

  • 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-26T02:48:22+00:00Added an answer on May 26, 2026 at 2:48 am

    First version

    What you want is use argc:

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int i = 0;
        int j = 0;
        for (i = 0; i < argc; i ++)
        {
        j = 0;      
        while(argv[i][j] != '\0')
           printf("Argument %d letter %d : %c\n", i,j,argv[i][j++]);   
        }
        return 0;
    }
    

    The output is actually letter by letter as you needed:

    $./a.out hello world
    Argument 0 letter 1 : .
    Argument 0 letter 2 : /
    Argument 0 letter 3 : a
    Argument 0 letter 4 : .
    Argument 0 letter 5 : o
    Argument 0 letter 6 : u
    Argument 0 letter 7 : t
    Argument 1 letter 1 : h
    Argument 1 letter 2 : e
    Argument 1 letter 3 : l
    Argument 1 letter 4 : l
    Argument 1 letter 5 : o
    Argument 2 letter 1 : w
    Argument 2 letter 2 : o
    Argument 2 letter 3 : r
    Argument 2 letter 4 : l
    Argument 2 letter 5 : d
    

    Second version:

    You can use the pointer notation for j but not for i since you don’t know the letter count of each argument. It could of course be achieved by using strlen which would lead under the hood to an iteration through the string to count the letter, which is not what you want to do. If you can do it in one iteration through the argument why do it in two?

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int i = 0;int j = 0;
        while(i < argc)
        {
        j=0;
        while(*(argv[i]+j) != '\0')
        {
                printf("Argument %d letter %d : %c\n", i,j,*(argv[i]+(j)));
                j++;
        } 
         i++;
        }
        return 0;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here we go again, the old argument still arises... Would we better have a
Here is what I am trying to achieve in PHP: I have this string:
here's the code: #include <string> class Config { public: static const std::string asdf =
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is what I have: Dim cmsManager As New Telerik.Cms.CmsManager() Dim currentNode As Telerik.Cms.Web.CmsSiteMapNode
Here's a strange question for you guys, I have a nice sorted list that
Here's what I'm trying do to in a single SQL Server procedure: @ID1 int
Here, i have coded to get data from DB. I want to store the

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.