Calling another function from printf?

后端 未结 2 1451
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 00:39

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 i

2条回答
  •  旧时难觅i
    2021-01-22 01:12

    I called my squareOfInteger function from my printf statement. Is this advisable though? It worked but I'm worried this might be bad practice?

    Calling a function from printf() is not bad. But there might be scenarios leading to undefined behavior since the order of evaluation within printf() is not specified.For example:

    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.

    whether it is enough to declare the result as being of type int, and leaving out type in the parameter

    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.

提交回复
热议问题