0

I have come across the void functions in c. And I understood that these functions wont return any value but will be having a return statement. I have tried to see the size of the function with the sizeof operator. The value is printed was 1 as size of void function. Can anyone explain how the 1 is the size of the function??

The below is the program i have used.

#include <stdio.h>

void my_func() 
{
   printf("From void my_function\n");
   return ;
}

int func()
{
    printf("From int func\n");
    return 0;
}

int main() 
{
   printf("Size of void function : %lu\n",sizeof(my_func())); // 1 is printed
   printf("Size of integer function : %lu\n",sizeof(func())); // 4 is printed

   return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Pleas format and indent code properly. Machine can read any code, humans cannot. – Sourav Ghosh Jul 09 '20 at 13:37
  • Where did you learn the term "void functions"? – Sourav Ghosh Jul 09 '20 at 13:38
  • Hi larson, I am looking for that reason why the value is 1 for void? – bhanu krishna bhanu Jul 09 '20 at 13:40
  • 1
    Note that in general `sizeof(my_func())` does not yield the size of the function itself (i.e. the number of bytes of machine code, or anything like that) but rather the size of the the type that it returns. Here `my_func()` returns `void` so you get the size of the type `void`, which is an error in standard C but some compilers (e.g. gcc) make it 1 as an extension, see the linked question. In your second example, `sizeof(func())` is just giving you the size of `int`. – Nate Eldredge Jul 09 '20 at 13:41
  • 1
    @SouravGhosh: It's not an uncommon synonym for "function returning void". – Nate Eldredge Jul 09 '20 at 13:42
  • Hey Sourav, I mean the functions with return type as void(non returning functions). Thanks – bhanu krishna bhanu Jul 09 '20 at 13:43
  • Well no, you *cannot* get the size of `void`, because that is an incomplete type. If you apply the `sizeof` operator to it, you get undefined behavior. In fact, such a use of `sizeof` is a constraint violation, so a conforming implementation must also emit a diagnostic. – John Bollinger Jul 09 '20 at 13:43
  • @NateEldredge Thanks for reply. I understood like, void is a datatype and its size is 1. So only the output is printing as 1. Is this correct? – bhanu krishna bhanu Jul 09 '20 at 13:48
  • Well, it acts like a datatype, but it's a very special one in that you can't actually have any objects of that type. As I said before, whether "its size is 1" depends on the dialect of C you are using. In standard C, `void` does not have a size and `sizeof(myfunc())` would cause a compiler error (or technically a "diagnostic"). Some compilers intentionally deviate from the standard and say that `void` does have a size after all and that it is 1; on such a compiler `sizeof(my_func())` indeed yields 1. – Nate Eldredge Jul 09 '20 at 13:52
  • So your compiler is presumably one of the second kind. Technically speaking, your program is not valid in the C language, but only in the "dialect" of C that your compiler provides. – Nate Eldredge Jul 09 '20 at 13:53

0 Answers0