c - 如果我想捕获我在 fun1 的正式参数中传递的值,我该怎么做?

标签 c function arguments

void fun1()
{
    puts("In fun1\n");
}
void fun2(void)
{
    puts("In fun2\n");
}
main()
{
    fun1();
    fun1(5);
    fun2();
}`

它工作正常,但我想捕获我传递的参数值,但仅限于此格式。

最佳答案

我不确定我是否能理解“但仅以这种格式”是什么意思。 如果您的意思是您无权更改void fun1()void fun1(<some suitable data type>) ,那么您将无法实现您的目标。

将参数传递给 fun1() 的最基本和常用的方法之一。是通过改变

void fun1()

void fun1(int a)  //int for example

我用过int作为参数的数据类型,它可以是以下任何一种:

int
unsigned int
char
unsigned char
short
unsigned short
long
unsigned long

等等..

有很多方法可以使用函数参数。您可以选择任何一本 C 编程的基本标准引用书,并查看涉及函数的章节。

对于初学者来说,阅读和更重要的是理解 C 标准可能非常困难,但它始终是前进的道路。我将为您提供引用 C11 标准的链接,这是最新的标准。

引用C11 standard, section 6.5.2.2 Function calls ,

  1. If the expression that denotes the called function has a type that includes a prototype, the number of arguments shall agree with the number of parameters. Each argument shall have a type such that its value may be assigned to an object with the unqualified version of the type of its corresponding parameter.

  1. An argument may be an expression of any complete object type. In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument

关于c - 如果我想捕获我在 fun1 的正式参数中传递的值,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49857006/

相关文章:

c - Geany 有什么问题吗?

c# - 可以在没有循环的情况下做到这一点吗?

c - 在 C 中将指针传递给函数的正确方法

javascript - 如何将 "arguments"对象转换为 JavaScript 中的数组?

c - 我如何理解复杂的函数声明?

c - 简单流程图

swift - 如果我们可以简单地调用它来实现,为什么要将函数作为参数传递给其他函数?

C 函数参数 : struct, 或 const struct*

c - 运行非阻塞 TCP 代码时出现 memcheck 错误

actionscript-3 - 匿名函数中变量的奇怪行为?