Tag: while loop

while循环有两个参数吗?

我的女士给了我一个问题要解决。 预测以下代码的输出。 #include int main() { int i = 0, j = 0; printf(“Output is : “); while (i < 5, j < 10) // Doubt: how does while accept 2 arguments?? and how it works?? { i++; j++; } printf("%d, %d\n", i, j); } 我认为这是一个语法错误。 但是当我试图跑步时,它给了我输出。 Output is : 10, 10 但是怎么样? 谁有人解释一下? 但是,如果我删除第一个printf语句printf(“Output […]

使用bool编译c代码而不使用c99标准

我试图在C中使用bool变量编译代码并且我已经包含了stdbool头但是当我编译它时我没有指定我想用c99标准编译它(因此它是用ANSI C编译的)标准)但无论如何它都有效。 我想知道为什么会这样? 这是代码: #include #include int main() { char name[20]; printf(“What’s your name ? “); gets(name); printf(“Nice to meet you %s.\n”, name); bool exit = false; char c; printf(“Do you wish to exit the program ? (Y/N) “); while (!exit) { c = getchar(); if (c == ‘\n’) { continue; } printf(“Do you wish […]

针对多种条件进行测试(C语言)

我必须创建一个菜单,如果输入无效。 它应该继续要求有效的输入。 我在下面写了(在C中) #include int main() { int input = 0; printf(“What would you like to do? \n 1 (Subtraction) \n 2 (Comparison) \n 3 (Odd/Even) \n 4 (Exit) \n “); scanf_s(“%d”, &input); while (input != 1 || input != 2 || input != 3|| input != 4) { printf(“Please enter a valid option \n”); […]

什么(;;)和while(); 在C中的意思

我正在看一些示例代码,我看到有人这样做了 for (;;) { // … } 这相当于while(1) { } ? 而什么while(condition); 做? 我没有理由把’;’放在后面 而不是{}

scanf check in while循环以限制整数输入

我正在编写一个代码,要求用户输入10个整数,然后将它们反馈给他。 我想创建一个“scanf check”来限制字符输入。 while循环的工作原理是它不接受char,但它会跳过整数输入。 int main() { int i = 0, number[10] = {0}; char buf[128] = {0}; for (i = 0; i < 10; i++) { printf("Please input number %d : ", i+1); while(scanf("%d", &number[i]) != 1) { scanf("%s", &buf); printf("Sorry, [%s] is not a number. Please input number %d : ", &buf, i); […]

在C中混合’切换’和’同时’

我最近阅读了有关奇怪的C代码段的页面 。 大多数都是可以理解的。 但我无法理解这一点: switch(c & 3) while((c -= 4) >= 0){ foo(); case 3: foo(); case 2: foo(); case 1: foo(); case 0: } 任何人都可以帮我解释一下这段代码背后的逻辑是什么? 它是如何工作的?

c – while循环在输入错误后继续忽略scanf

我在论坛上搜索了解决方案,但仍然对我的代码产生的输出感到困惑。 所以,该程序非常简单。 它在输入处获得两个数字,直到到达文件末尾。 如果输入错误,则应将错误打印到stdout并继续进行下一对。 如果两者都是素数,它会打印出prime 。 否则,它会打印他们的GCD。 问题是,如果输入不好,即一个或两个数字实际上都不是数字,程序会跳过scanf并继续向stderr打印错误。 然而,在调试期间,我发现scanf()所有下一次迭代都会通过,它返回0 ,好像根本没有输入任何东西。 并且提示对于输入无效,因为程序不断打印到stderr。 nd和nsd分别是返回最大分频器和最大公约数的函数。 主要计划如下: #include #include “nd.h” #include “nsd.h” int main() { int a; int b; int inp_status = 0; while (1){ inp_status=scanf(” %d %d”, &a, &b); if (inp_status == EOF){ break; } else if (inp_status < 2){ fprintf(stderr, "Error: bad input\n"); } else { if […]

while(getchar()!=’\ n’);

我有以下for循环,我提示用户输入一个4位数的针脚并按Enter键。 有人可以向我解释while循环真正在做什么,因为我不完全理解它。 //user input for pin for(i = 0; i < PIN_LENGTH; i++) { printf("Enter digit %d of your PIN: ", i); user_pin[i] = getchar(); while (getchar() != '\n'); //what is this line doing?? }

C中的循环 – ()或while() – 哪个最好?

for()或while() – 哪个最好? for (i=1; i<a; i++) /* do something */ 要么 i=1; while (i<a) { /* do something */ i++; }

当换行符在格式字符串中时scanf的行为

下面是我的代码的副本。 基本上,我需要创建一个程序,根据“工资代码”计算工资,例如工人的职位。 我已经创建了我的switch语句,除了刚开始输入第一个付费代码时,一切正常。 我输入第一个付款代码,它转到下一行,将其留空。 我输入了另一个数字,它按照预期的方式运行该数字和前一个数字。 然后,在那之后一切正常。 我确信这是一个简单的解决方案,但对我来说有点棘手。 另外,我不确定我应该如何在这里格式化我的代码以使它看起来像在服务器上,所以我为它看起来令人困惑的方式道歉。 #include //precompiled header int main(void) { //declare variables unsigned int counter = 0; //counters to calculate amount of workers paid unsigned int counter2 = 0; unsigned int counter3 = 0; unsigned int counter4 = 0; int paycode; float overtime; //amount of overtime hours float salary; //weekly salary […]