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

  • Home
  • SEARCH
  • 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 4024986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:47:25+00:00 2026-05-20T10:47:25+00:00

I have made little program for computing pi (π) as an integral. Now I

  • 0

I have made little program for computing pi (π) as an integral. Now I am facing a question how to extend it to compute an integral, which will be given as an extra parameter when starting an application. How do I deal with such a parameter in a program?

  • 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-20T10:47:26+00:00Added an answer on May 20, 2026 at 10:47 am

    When you write your main function, you typically see one of two definitions:

    • int main(void)
    • int main(int argc, char **argv)

    The second form will allow you to access the command line arguments passed to the program, and the number of arguments specified (arguments are separated by spaces).

    The arguments to main are:

    • int argc – the number of arguments passed into your program when it was run. It is at least 1.
    • char **argv – this is a pointer-to-char *. It can alternatively be this: char *argv[], which means ‘array of char *‘. This is an array of C-style-string pointers.

    Basic Example

    For example, you could do this to print out the arguments passed to your C program:

    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        for (int i = 0; i < argc; ++i)
        {
            printf("argv[%d]: %s\n", i, argv[i]);
        }
    }
    

    I’m using GCC 4.5 to compile a file I called args.c. It’ll compile and build a default a.out executable.

    [birryree@lilun c_code]$ gcc -std=c99 args.c
    

    Now run it…

    [birryree@lilun c_code]$ ./a.out hello there
    argv[0]: ./a.out
    argv[1]: hello
    argv[2]: there
    

    So you can see that in argv, argv[0] is the name of the program you ran (this is not standards-defined behavior, but is common. Your arguments start at argv[1] and beyond.

    So basically, if you wanted a single parameter, you could say…

    ./myprogram integral


    A Simple Case for You

    And you could check if argv[1] was integral, maybe like strcmp("integral", argv[1]) == 0.

    So in your code…

    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        if (argc < 2) // no arguments were passed
        {
            // do something
        }
    
        if (strcmp("integral", argv[1]) == 0)
        {
            runIntegral(...); //or something
        }
        else
        {
            // do something else.
        }
    }
    

    Better command line parsing

    Of course, this was all very rudimentary, and as your program gets more complex, you’ll likely want more advanced command line handling. For that, you could use a library like GNU getopt.

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

Sidebar

Related Questions

No related questions found

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.