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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:38:27+00:00 2026-05-30T22:38:27+00:00

I am assuming I am using this the wrong way, but the idea is

  • 0

I am assuming I am using this the wrong way, but the idea is for the command line argument to be the length of my fibonnaci serquence… however the way I am doing this, after 9 I am screwed… how can I resolve this issue?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* for fork */
#include <sys/types.h> /* for pid_t */
#include <sys/wait.h> /* for wait */
int fibonacci(int n)
{
  int first = 0;
  int second = 1;
  int total, i;
  for (i=0;i<n;i++)
  {
    printf("%d\n", first);
    total = first + second;
    first = second;
    second = total;
  }
  return 0;
}
int main(int argc, char *argv[])
{
    /*Spawn a child to run the program.*/
    pid_t pid=fork();
    if (pid==0) { /* child process */
        if(*argv[1] == 45){
            printf("number invalid \n");
        }else{
            int number = *argv[1] - 48;
            fibonacci(number);
        }
    }
    else { /* pid!=0; parent process */
        waitpid(pid,0,0); /* wait for child to exit */
    }
    return 0;
}
  • 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-30T22:38:28+00:00Added an answer on May 30, 2026 at 10:38 pm

    Command line arguments are strings; convert the strings to integers:

    int number = 9;
    if (argc > 1)
        number = atoi(argv[1]);
    

    This gives you a default value (9), and the option to override it. More thorough checking would reject more than 1 argument, and negative or zero returns from atoi():

    enum { MAX_FIBONACCI = 47 };
    
    if (argc > 2)
    {
        fprintf(stderr, "Usage: %s [number]\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    if (argc == 2)
    {
        number = atoi(argv[1]);
        if (number <= 0)
        {
            fprintf(stderr, "Invalid number %s\n", argv[1]);
            exit(EXIT_FAILURE);
        }
        else if (number > MAX_FIBONACCI)
        {
            fprintf(stderr, "Number %s is too large (max is %d)\n", argv[1], MAX_FIBONACCI);
            exit(1);
        }
    }
    

    Note that key information is reported to help identify what went wrong. After 47 entries, you overflow a 32-bit signed integer.

    Note that testing for errors from strtol() et al properly is a moderately complex business if you have to accommodate any return value whatsoever. If you only need to accommodate the range that you can print Fibonacci numbers for, it is rather simpler.

    The repeated four lines of error handling rapidly gets irksome. I use a function like this instead:

    #include <stdarg.h>
    
    void err_exit(const char *fmt, ...)
    {
        va_list args;
        va_start(args, fmt);
        vfprintf(stderr, fmt, args);
        va_end(args);
        exit(EXIT_FAILURE);
    }
    

    This reduces the error reporting to one line per error, which is preferable to four lines. (My full system is more complex than that, by quite a margin — all else apart, it gets told the program name and reports it automatically. But that’s a workable starting point.)

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

Sidebar

Related Questions

this is killing me but is there any simple way to have objective-c code
Assuming I'm using some graphic API which allows me to draw bezier curves by
What is the differences between those 2 assuming I'm using SQL Server as my
Assuming the file exists (using os.path.exists(filename) to first make sure that it does), how
Assuming I change the contents of a control using a XamlReader and add the
Using (local) in the connection string doesn't work on my cluster. I'm assuming it's
How do I retrieve the temperature of my CPU using Python? (Assuming I'm on
I'm using jQuery, building a table through a loop. I thought the best way
Ok, I know this has been asked a thousand times before, but no conclusive
Maybe I'm going about it the wrong way. Here is what I'm trying to

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.