简体   繁体   English

在printf程序结束自身而不执行其余代码之后? C

[英]after printf program ends itself without doing the rest of code ? C

after printf line program ends itself but i didnt get it why. 在printf行程序结束本身,但我没有得到它为什么。

#include<stdio.h>

int main ()
{
    int Sum,multiply,divide,difference,num1,num2;
    char i;
    scanf("%d", &s1);
    scanf("%d", &s2);
    printf("Type initial of your operation : ");
    scanf("%c", &i);

    return 0 ;
}

There is no way you could compile that, since s1 and s2 are undefined variables. 由于s1s2是未定义的变量,因此无法编译。

Thus, any information about what happened when you ran it is moot, since there is no way you could run it. 因此,关于运行时发生的任何信息都是没有意义的,因为您无法运行它。

You meant: 你的意思是:

if(scanf("%d %d", &num1, &num2) == 2)
{
  printf("Operands are %d and %d, now type initial of desired operation:\n");
  if(scanf("%c", &i) == 1)
  {
  }
}

It's important to check that scanf() has succeeded before relying on the return value. 在依赖返回值之前检查scanf()是否成功很重要。

use scanf(" %c",&i); 使用scanf(" %c",&i); there is new line character is present in buffer,so it is not asking for any input and storing it in i. 缓冲区中有换行符,因此它不需要任何输入并将其存储在i中。

s1 & s2 are not declared which you are trying to read into them. 没有声明s1s2 ,您试图读取它们。 I feel they should be num1 & num2 which you been declared as integers. 我认为它们应该是num1num2 ,您num2它们声明为整数。

After printf you just read a char value into i using scanf and main ends doing nothing. printf之后,您只需使用scanf将char值读入i并且main端什么也不做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM