简体   繁体   English

从printf调用另一个函数?

[英]Calling another function from printf?

I am doing a simple challenge from a book on C/Objective-C where I have to write a program that computes and displays the square of an integer, while also putting the numbers in quotation marks. 我正在从有关C / Objective-C的书中进行一个简单的挑战,我必须编写一个程序来计算和显示整数的平方,同时还要将数字放在引号中。

I made a program that works successfully but I have two questions about it. 我制作了一个可以成功运行的程序,但是对此有两个问题。 Firstly having had the the idea of cutting back on the amount of code in mind, to practice being efficient and concise in my code, I called my squareOfInteger function from my printf statement. 首先,我想到了减少代码量的想法,以实现高效,简洁的代码,我从printf语句中调用了squareOfInteger函数。 Is this advisable though? 这是明智的吗? It worked but I'm worried this might be bad practice? 它奏效了,但我担心这可能是不好的做法?

The other thing nagging at me is whether or not I need to reiterate the type as being an int inside the parameter of my function before my variable number , or whether it is enough to declare the result as being of type int , and leaving out type in the parameter, which I have done below. 困扰我的另一件事是我是否需要在变量number之前将类型重复声明为函数内的int类型,还是将结果声明为int类型并忽略类型是否足够在参数中,我在下面做了。

Here is my code: 这是我的代码:

#include <stdio.h>

int squareOfInteger(number)
{
    int square = number * number;
    return square;



}

int main(int argc, const char * argv[]) {
    int myNumber = 5;
    printf("\"%d\" squared is \"%d\".\n", myNumber, squareOfInteger(myNumber));
    return 0;
}

I would greatly appreciate your help. 非常感谢您的帮助。

I called my squareOfInteger function from my printf statement. 我从printf语句调用了squareOfInteger函数。 Is this advisable though? 这是明智的吗? It worked but I'm worried this might be bad practice? 它奏效了,但我担心这可能是不好的做法?

Calling a function from printf() is not bad. printf()调用函数还不错。 But there might be scenarios leading to undefined behavior since the order of evaluation within printf() is not specified.For example: 但是由于未指定printf()的求值顺序,因此可能会导致行为不确定的情况,例如:

int func(int *p)
{
   *p =30;
}
int main()
{
   printf("%d %d",num,func(&num));
   return 0;
}

Here you are calling func() as well as printing the value of num so you have UB. 在这里,您要调用func()并打印num的值,以便拥有UB。

whether it is enough to declare the result as being of type int, and leaving out type in the parameter 将结果声明为int类型,并在参数中省略类型是否足够

If the type of the argument is not specified then it defaults to int .But it is always good to mention the type of the arguments. 如果未指定参数的类型,则默认为int 。但是最好提及参数的类型。

"I called my squareOfInteger function from my printf statement. Is this advisable though?" “我从我的printf语句中调用了squareOfInteger函数。但是这是明智的做法吗?”

There is nothing wrong in calling the function from within a function. 从函数内部调用函数没有错。 It depends on case, where you say need to retain the squared value (as not in your case where you just need to print the value) and perform other operations on it. 这取决于大小写,在这种情况下您需要保留平方值(而不是仅需要打印该值),并对其执行其他操作。 like: 喜欢:

int value = myFun(100);
int nextVal = myNextFun(value);

"The other thing nagging at me is whether or not I need to reiterate the type as being an int inside the parameter of my function before my variable number, or whether it is enough to declare the result as being of type int, and leaving out type in the parameter, which I have done below." “困扰我的另一件事是我是否需要在变量编号之前将类型重复声明为函数的参数内部的int值,或者是否足以将结果声明为int类型,而忽略掉输入参数,我将在下面完成。”

Yes you should use define the type of number (in your case as by default it would be integer type and would fail if you have it of other type) as a best practice as it will make code more clear and readable. 是的,您应该使用定义数字的类型(在默认情况下为整数类型,如果您使用其他类型的数字,则默认情况下将失败)作为最佳实践,因为这会使代码更清晰易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM