How to write a calculator program in c language using command line arguments??? The calculator only needs to perform basic functions of addition, subtraction, multiplication and division of 2 integers only.

4 Years
Discussion Span
Last Post by Lucaci Andrew

Start by reading up on how to parse command line arguments. Then see what you can come up with based on what you learn, and if you have any specific problems or questions, don't hesitate to ask.

It's very difficult to tell you how to do this without making assumptions about your level and without writing a complete tutorial on the subject (far more than is justified for a forum post).


Here's a quick example of how you can integrate command line arguments into your program:

#include <iostream>
#include <cstdlib>
int main(int argc, char* argv[]){
    if (argc>1){
        int a=std::atoi(argv[1]), b=std::atoi(argv[2]);
        std::cout<<"a: "<<a<<", b: "<<b<<" "<<std::endl; //could be std::printf("a: %d b: %d", a, b); if it's 
                                                         //required as a C program.
    return 0;

You'll run this from the command line like this:

executable.exe 1 2
//which will output:
a: 1, b: 2

./executable 1 2
//which will output:
a: 1, b: 2

Now, knowing that, try to figure out how to do that simple calculations. Good luck.

Edited by Lucaci Andrew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.