对于C语言,一个函数可以有多少个参数?

如题所述

内存角度来看,函数的参数是要入栈的,栈区大小决定了参数的个数。一般C语言程序运行时默认1M的栈空间,以int类型的参数来看,1M=1024KB=1024*1024字节,32位系统中int类型占4个字节,所以理论上一个函数最多有262144个int类型的参数。实际中要远远小于这个数,因为应用程序中的局部变量都需要占用栈空间。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-07-03
一个函数的参数的数目没有明确的限制,但是参数过多(例如超过8个)显然是一种不可取的编程风格。参数的数目直接影响调用函数的速度,参数越多,调用函数就越慢。另一方面,参数的数目少,程序就显得精练、简洁,这有助于检查和发现程序中的错误。因此,通常应该尽可能减少参数的数目,如果一个函数的参数超过4个,你就应该考虑一下函数是否编写得当。 如果一个函数不得不使用很多参数,你可以定义一个结构来容纳这些参数,这是一种非常好的解决方法。在下例中,函数print_report()需要使用10个参数,然而在它的说明中并没有列出这些参数,而是通过一个RPT_PARMS结构得到这些参数。 # include <atdio. h> typedef struct ( int orientation ; char rpt_name[25]; char rpt_path[40]; int destination; char output_file[25]; int starting_page; int ending_page; char db_name[25]; char db_path[40]; int draft_quality; )RPT_PARMS; void main (void); int print_report (RPT_PARMS* ); void main (void) { RPT_PARMS rpt_parm; /*define the report parameter structure variable * / /* set up the report parameter structure variable to pass to the print_report 0 function */ rpt_parm. orientation = ORIENT_LANDSCAPE; rpt_parm.rpt_name = "QSALES.RPT"; rpt_parm. rpt_path = "Ci\REPORTS" rpt_parm. destination == DEST_FILE; rpt_parm. output_file = "QSALES. TXT" ; rpt_parm. starting_page = 1; rpt_pann. ending_page = RPT_END; rpt_pann.db_name = "SALES. DB"; rpt_parm.db_path = "Ci\DATA"; rpt_pann. draft_quality = TRUE; /*call the print_report 0 function; paaaing it a pointer to the parameteM inatead of paMing it a long liat of 10 aeparate parameteM. * / ret_code = print_report(cu*pt_parm); } int print_report(RPT_PARMS*p) { int rc; /*acccM the report parametcra paaaed to the print_report() function */ oricnt_printcr(p->orientation); Kt_printer_quality((p->draft_quality == TRUE) ? DRAFT ; NORMAL); return rc; } 上例唯一的不足是编译程序无法检查引用print_report()函数时RPT_PARMS结构的10个成员是否符合要求。本回答被提问者采纳
相似回答