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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:23:30+00:00 2026-06-10T06:23:30+00:00

I would like a user to pass either two parameters or leave it blank.

  • 0

I would like a user to pass either two parameters or leave it blank. For instance:

./program 50 50

or

./program

When I tried to use int main(int argc, char *argv[]), first thing I have done was to change char *argv[] to int *argv[] but it did not work. What I want is from the user is just to enter two integers between 0 and 100. So if it is not two integers then it should give an error.

I was sort of thinking to give out an error with types (as I used to program on C#) but whatever I enter, argv[1] would be ‘char’ type all the time.

So what I have done is

for (int i = 0; i <= 100; i++) {
    //printf("%d", i);
    if (argv[1] == i) {
        argcheck++;
        printf("1st one %d\n", i);
    }
    else if (argv[2] == i) {
        argcheck++;
        printf("2nd one %d\n", i);
    }

This does not work as well. Also it gives warning when compiling, but if I change argv with atoi(argv[1]) for instance, then it gives a Segmentation fault (core dumped) error.

I need a simple way to solve this problem.

EDIT:

So I fixed with atoi(), the reason why it was giving segmentation fault was because I was trying it with null value when I have no parameter. So I fixed it up by adding an extra cond. But now the problem is if the value is let’s say

./program asd asd

Then the output of atoi(argv[1]) would be 0. Is there a way to change this value?

  • 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-06-10T06:23:32+00:00Added an answer on June 10, 2026 at 6:23 am

    Don’t use atoi() and don’t use strtol(). atoi() has no error checking (as you found out!) and strtol() has to be error-checked using the global errno variable, which means you have to set errno to 0, then call strtol(), then check errno again for errors. A better way is to use sscanf(), which also lets you parse any primitive type from a string, not just an integer, and it lets you read fancy formats (like hex).

    For example, to parse integer “1435” from a string:

    if (sscanf (argv[1], "%i", &intvar) != 1) {
        fprintf(stderr, "error - not an integer");
    }
    

    To parse a single character ‘Z’ from a string

    if (sscanf (argv[1], "%c", &charvar)!=1) {
        fprintf(stderr, "error - not a char");
    }
    

    To parse a float “3.1459” from a string

    if (sscanf (argv[1], "%f", &floatvar)!=1) {
        fprintf(stderr, "error - not a float");
    }
    

    To parse a large unsigned hexadecimal integer “0x332561” from a string

    if (sscanf (argv[1], "%xu", &uintvar)!=1) {
        fprintf(stderr, "error - not a hex integer");
    }
    

    If you need more error-handling than that, use a regex library.

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

Sidebar

Related Questions

I'm using Authlogic with my Ruby on Rails App. I would like user to
I would like the user to be able to view a hyperlink to my
I would like the user to select the time in javascript and to avoid
I would like the user to add several pins to a map to represent
I would like the user to be able to edit the title of the
I'm developing an ASP.NET Web app and would like the user to be able
My app displays a list of albums and I would like the user to
I’m developing an application dedicated to generate statistical reports, I would like that user
I would like to create a user interface like the iGoogle or facebook profiles
I would like to embed user-specific data on-the-fly when distributing an APK from my

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.