c - 我在使用命令行参数时遇到问题

标签 c command-line command-line-arguments command-prompt

//我的程序假设从命令提示符中进行操作并拖出数字并应用//对两个数字的操作但结果总是错误的为什么?

#include "stdafx.h"
#include<stdio.h>
#include<tchar.h>
#include<stdlib.h>

int main(int argc, char*argv[])
{
    if(argc !=4)
    {
        printf("number of CLP is incorrect\n");
        return 0;
    }

    int num1 = atoi(argv[2]);
    int num2 = atoi(argv[3]);
    int res ;

    if(argv[1] == "+")
        res = (num1 + num2);

    else if(argv[1]=="-")
        res = (num1-num2);

    else if(argv[1]=="*")
        res = (num1*num2);

    else if(argv[1]=="/")
        res = (num1/num2);

    printf("You enterd Operation %s and the Resualt = %d\n" , argv[1] , res);
    return 0;
}

这是我的代码,如果有更好的方法,请告诉我。

最佳答案

您不能将字符串与 == 进行比较 使用 if(strcmp(argv[1], "+") == 0) 并且在其余的代码。

关于c - 我在使用命令行参数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955230/

相关文章:

windows - Raku:带空格的 shell 调用的语法是什么?

C: 数组没有正确分配更多内存

c++ - 找不到 Xcode 版本 6.0.1 gmp.h 头文件,即使 gmp.h 和 gmpxx.h 位于 local/include

c - “C”编程 - 神秘的结构语法 - 几乎看起来像 Python 字典 - 这是什么?

ruby-on-rails - 具有非 HTTP 访问权限的 Rails 应用

linux - 在 Linux 中,如何从终端在文件夹内创建文件夹?

c# - 如何通过 C# 传递多个命令行参数

c - 将 Contiki 目标文件与 RUST 库链接时出错

c - Linux 上的 strdup 使用哪个宏?

linux - 为linux中的特定组分配权限文件夹