C语言void函数未执行问题排查及指定规则main函数编写咨询
原代码跳过自定义函数的原因及存在的错误
函数未执行的核心原因
- C语言不允许在函数内部嵌套定义其他函数,你提供的代码把
Fun2_xxx_F3、F3的完整定义写在了main函数的花括号内部,属于语法错误,即使部分编译器兼容该写法,也只会将这两段代码识别为main函数的无效局部代码,不会被当做独立函数执行。 - 即便函数定义位置正确,C语言的自定义函数不会自动运行,必须显式编写调用语句才能执行,原代码的
main逻辑里没有任何调用这两个函数的语句,自然不会运行。
代码存在的其他错误
- 变量未初始化:
total_discount定义时没有赋初值0,直接执行+=操作会读取内存随机值,最终计算结果完全异常。 - 分支逻辑缺失:判断
M取值的逻辑只有A>B和A<B两个分支,当A=B时M没有被赋值,会成为随机值,后续while循环的运行次数完全不可控。 - 栈溢出风险:
Fun2_xxx_F3内部定义了长度为60897的局部字符数组S1,而程序的局部栈空间通常只有几MB,超大长度的局部数组极易触发栈溢出导致程序崩溃。 - 函数声明和实现不匹配:后续需求要求调用
Function1_42357_F2,原代码既没有该函数的声明也没有实现,直接调用会编译报错。
新main函数需求的含义与实现方法
需求含义解释
- main函数体内无局部变量:要求
main函数的花括号内部不能定义任何int/float/数组等类型的局部变量,只能编写函数调用语句。 - 以学号作为实参调用第一个函数
Function1_42357_F2:需要先实现Function1_42357_F2函数,将原来main里的前半段折扣计算逻辑迁移到该函数内,调用时把你的学号作为参数传入即可。 - 调用Fun2和F3函数:在
main里添加两个函数的调用语句即可。
实现示例代码
#include <stdio.h> // 全局函数声明,需根据你的学号长度调整参数类型,长学号用long long即可 void Function1_42357_F2(long long stu_id); void Fun2_xxx_F3(); void F3(); // 符合要求的main函数,内部无局部变量,仅保留函数调用 int main() { // 把12345678替换为你的实际学号即可 Function1_42357_F2(12345678); Fun2_xxx_F3(); F3(); return 0; } // 原main中折扣计算逻辑迁移到该函数内 void Function1_42357_F2(long long stu_id) { int M,A=6,B=8,items=1,counter=0,d; float price,discount,discounta,total_discount=0; printf("Function1_xxx_F3 is created by student ID = %lld- section F3", stu_id); // 补全A=B的分支 if (A>B) { M=A; } else if(A<B) { M=B; } else { M = A; } while(items<=M) { printf("\n\nEnter the price of item %d: ",items); scanf("%f",&price); if(price<1100) { discounta=price-price*10/100; printf("\nThe price of the item after discount = %.3f KD",discounta); total_discount+=discounta; counter++; } else if(price>=1100 && price<=2200) { discount=price-price*15/100; printf("\nThe price of the item after discount = %.3f KD",discount); } else { discount=price-price*20/100; printf("\nThe price of the item after discount = %.3f KD",discount); } items++; } printf("\n\nThe total price after discount of all items that got a discount of 10 percent is: %.3f KD",total_discount); } // Fun2_xxx_F3定义移到main外部,缩小数组长度避免栈溢出 void Fun2_xxx_F3() { int M=30; unsigned int computer=1,counter=0,counter2=0; float computer_price,computer_tax,avg1,avg2,computer_tax3=0,computer_tax5=0; // 缩小数组长度到200,足够存储字符串即可 char S1[200]= "XXX YYY section:F3 ID xxx"; printf("\n\nM is equal to the sum of my ID-number:xxx which is: %d",M); printf("\n\nFunction (Fun2_xxx_F3) is created by student: %s\n\n\n",S1); while(computer<=30) { printf("\nEnter the price of computer %d: ",computer); scanf("%f",&computer_price); if (computer_price> 1100) { computer_tax= computer_price+computer_price*3/100; computer_tax3+=computer_tax; counter++; } else { computer_tax= computer_price+computer_price*5/100; computer_tax5+=computer_tax; counter2++; } computer++; } // 避免除数为0的异常 avg1 = counter>0 ? computer_tax3/counter : 0; avg2=(computer_tax3+computer_tax5)/30; printf("\n\n%.3f KD ... %.3f KD",avg1, avg2); } // F3定义移到main外部 void F3() { int i=6; unsigned int N; for(N=1 ; N<=i ; N+=1) { printf("\nID:xxx Name:XXX YYY SECTION:F3"); } }
内容的提问来源于stack exchange,提问作者Raghad Sami
相关产品推荐
相关产品推荐

