Using the # character in C

I take an online test in C (not for work, only for myself), and I constantly encounter questions of this type using a hash symbol. I assume this is not a typo and I am not familiar with this use of the hash symbol.

#include <stdio.h>
int* func()
{
    int num = 10;
    return #
}
int main()
{
    int *ptr = func();
    printf("%d\n", *ptr);
    return 0;
}

What is the output of the above C code?

The answer is that this is a runtime error and explanation

The variable defined in the function will be allocated in the stack segment, which will be deleted when the function returns. Therefore, access to the address of these variables leads to a segmentation error.

+4
source share
2 answers

The one who created the test incorrectly encoded their HTML. They wanted this line to look like this:

return &num;

HTML , , &XXX; - , HTML. &num; - #. :

return &amp;num;

&amp; &.

+23

func() , num. num - func(). , - . , main(), . Cz num , , , .

Edit:

Hash - # . return # → . , - - .

0

Source: https://habr.com/ru/post/1691437/


All Articles