gpt4 book ai didi

c - 有谁有C语言条件语句的例子吗?

转载 作者:行者123 更新时间:2023-11-30 18:16:58 25 4
gpt4 key购买 nike

类似这样的东西,我想看看完整的语法。

伪代码:

var = user_input

if var > 5:
output = 'var > 5'
else:
output = 'var < 5'

最佳答案

类似这样的事情怎么样:

#include <stdio.h>
#include <string.h>

int main (void) {
int var;
char buff[100];

printf ("Enter number> ");
fflush (stdout);
if (fgets (buff, sizeof(buff), stdin) == NULL) {
printf ("\nfgets() failed\n");
return 1;
}
if (sscanf (buff, "%d", &var) != 1) {
printf ("\nsscanf() failed\n");
return 1;
}

if (var > 5)
printf ("%d is greater than 5\n", var);
else
printf ("%d is less than 6\n", var);

return 0;
}

进行了几次测试:

pax> testprog
Enter number> 99
99 is greater than 5

pax> testprog
Enter number> -5
-5 is less than 6

关于c - 有谁有C语言条件语句的例子吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3723658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com