简体   繁体   中英

Please explain this output of the code

#include<stdio.h>
int GetPositiveInt(void);

int main(void)
{
    int num = GetPositiveInt();
    printf("%d",num);
    return 0;
}

int GetPositiveInt(void)
{
    int n;
    do
    {
        printf("Enter a positive number : ");
        scanf("%d",&n);
    }while(n<=0);

}

The output is the value of n but I don't know how it is returned to num . Does scanf() and printf() affect the return value of a function?

If I add a statement like printf("%d",7) ; after while then the output is 71 ,no idea how 1 comes.

This is my output. This is for reference only .

Text as requested :-

usr@usr:~/C_programs$ gcc -std=c99 return.c -o return

usr@usr:~/C_programs$ ./return

Enter a positive number : 45

45usr@usr:~/C_programs$ ./return

Enter a positive number : 89

89usr@usr:~/C_programs$

TL;DR Your code produces undeifined behavior.

You're missing a return statement in GetPositiveInt() function. Without having an explicit return statement to return a value, collecting the return value of the function call in the caller function and using that leads to undefined behaviour .

Ref: As per the C11 standard document, chapter §6.9.1,

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.


FWIW, only in case of main() , addition of return statement is not mandatory . From same document, chapter 5.1.2.2.3, for main() function

reaching the } that terminates the main function returns a value of 0.

In the following function:

int GetPositiveInt(void)
{
  int n;
  do
  {
    printf("Enter a positive number : ");
    scanf("%d",&n);
  }while(n<=0);

}

the function is expected to return an int value, whereas there is no return statement. You probably wanted to return the value of n . This can be achieved by adding the suitable return statement:

 return n;

Like this:

int GetPositiveInt(void)
{
  int n;
  do
  {
    printf("Enter a positive number : ");
    scanf("%d",&n);
  }while(n<=0);

  return n;
}

no idea how '1' comes.

Because you did not return anything in your method GetPositiveInt - and the behavior is undefined:

int GetPositiveInt(void)
{
    int n;
    do
    {
        printf("Enter a positive number : ");
        scanf("%d",&n);
    }while(n<=0);

    return n; //<---Add this line

}

C99 6.9.1/12 "Function definitions" says:

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

It prints 0 for me.

As explained in the other answers, the lack of the return statement in a function that is declared to return something is signaled by the compiler with a warning but the generated code exposes undefined behaviour. This means the function can return any value, the compiler cannot guarantee anything about it.

You can think of the value returned by a function as a hidden local variable of that function. Since the return statement is missing, the hidden variable is not initialized and its value is whatever garbage data happened to be in the memory at the time when the function was called.

In practice, the return statement is translated into a processor instruction that copies the returned value into one of the general-purpose processor's registries (usually %eax on x86 ). The calling function ( main() in your code) gets the values from that register and uses it.

Because your function GetPositiveInt() does not contain a return statement, the compiler skips generating the instruction that puts the correct value in %eax and the function completes its execution letting in %eax whatever value happened to be there as a result of earlier processing.

This is a case of undefined behaviour because you are missing the return statement.however I can explain why 1 is coming.

The printf function returns the number of characters printed by it.For example

 printf("%d",printf("%d",7));

first printf prints 7 and returns 1 as number of characters printed. So output is 71.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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