c - 我的 C 脚本停止工作

标签 c printf

<分区>

我正在开发一种新的编程语言来获得乐趣,当我键入“print+:”时,我会插入 Pfunc1plus 和 Pfunc2plus,当你同时注册这两个数字时,我会抛出错误“程序已停止工作……”

#include <stdio.h>
#include <stdlib.h>

int main(){

char sysF[300];
char Pfunc[300];
int Pfunc1plus; int Pfunc2plus;

scanf("%s",sysF);
// Print
if (strcmp(sysF, "print:") == 0){
    scanf("%s",Pfunc);
    system("cls");
    printf("%s",Pfunc);
}
// Error code       
if(strcmp(sysF, "print+:") == 0) {
    scanf("%i %i",Pfunc1plus,Pfunc2plus);
    printf("%i %i",Pfunc1plus+Pfunc2plus);
} // Error code

system("pause >nul");
return 0;
}

最佳答案

两件事:

scanf("%i %i",Pfunc1plus,Pfunc2plus);

scanfaddresses 赋给变量,因此您需要在 & 前面放置一个寻址运算符 ( Pfunc1plus )和 Pfunc2plus .

printf("%i %i",Pfunc1plus+Pfunc2plus);

您有两个整数格式说明符,但只传递一个参数 ( Pfunc1plus+Pfunc2plus )。如果您想同时打印两者,请将加法运算符更改为逗号;如果您真的想打印总和,请删除 %i .

关于c - 我的 C 脚本停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28724913/

相关文章:

c - 如何在不强制转换的情况下取消引用 void 变量

C 获取不同的字符串和整数

在 K20 上没有 -G 选项时 CUDA C 返回不确定且奇怪的结果

c - C 中的格式化字符串攻击

c - 如何在 C 中打印出 fread() 的值

c++ - 如何在 Linux 上获得等效的 printf_l?

c - struct node *head 和 struct node ** head 有什么区别?

c - Windows RPC 编程错误 : LNK 2019 unresolved external symbol RPCServerListen

c - 由于带有字符的输入函数 (scanf()) 存在一些问题,while 循环中的条件表达式出现奇怪的行为

c - 如何将键盘输入写入txt文件