-7
#include<stdio.h>
#include<conio.h>

int main(){
    printf("hello world");
}

What actually does int do to the main function? Is it similar to the data type int? What does conio.h do?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • The `int main()` returns an `int` to the caller just like any other function, except in this special case if you don't explicitly return a value, the compiler will add `return 0;` for you. The `conio.h` is a non-standard library which is not needed in the example. – Weather Vane Apr 19 '22 at 09:43
  • In short terms, int here is the return type of the main function. It can be any type, in this care, it returns 0 which means it's good, and 0 it's an integer of course. – Martzy Apr 19 '22 at 09:45
  • In addition to @Martzy comment, `return 1;` is used when something bad is happened – Darth-CodeX Apr 19 '22 at 09:49
  • There are two questions here and the linked dupe only answers the first. I'll vote to re-open (unless someone can be bothered to dig up a conio.h replacement dupe). – Lundin Apr 19 '22 at 09:51
  • This must be a **duplicate**. – OTheDev Apr 19 '22 at 10:05
  • @oda it is indeed ^^^ – gnat Apr 19 '22 at 11:08
  • @gnat Again, how does the duplicate answer the question "What does conio.h do?" Hence my comment above regarding why I re-opened the post. – Lundin Apr 19 '22 at 14:09
  • @qrdl Please see the comment above. – Lundin Apr 19 '22 at 14:09
  • @Thomas Jager Please see the comment above. – Lundin Apr 19 '22 at 14:10
  • Also why did you lot pick a _closed_ non-canonical post as dupe target instead of the canonical dupe which was originally used the first time this was closed? _Kindly pay attention to edit history before casting close votes_. And don't use closed posts as dupe target, obviously. – Lundin Apr 19 '22 at 14:11
  • @Lundin I've been thinking of closing as too broad as well (["asks many things"](https://meta.stackoverflow.com/a/267059/839601)), maybe this would be a more accurate reason indeed. As for picking particular target of multiple candidates, the one I suggested looked like closest match to me ATM – gnat Apr 19 '22 at 14:20
  • @gnat It's ok to ask multiple questions at once if they are related. In this case it would seem to be a newbie dissecting their first "hello world" program and wants to understand what it all means. In that context the questions are related. I see no particular reason to close this post. Down vote for lack of research perhaps, but that's another story. – Lundin Apr 19 '22 at 14:24
  • At any rate, we should never pick closed posts as dupe targets, because if a post was closed, it can surely not be canonical. In this case it was closed as a duplicate too, so obviously go straight to the source, namely the dupe target of _that_ post. I've fixed the dupe target now, seems dupe hammer still works for that even though I've used up my re-open vote. – Lundin Apr 19 '22 at 14:26

2 Answers2

0

What actually does int do to the main function?

It's the program's return code to the OS, return 0; meaning successful execution. The OS may or may not care about it. For details, see What should main() return in C and C++?

please help me understand what does conio.h do ?

It includes a non-standard, long-since obsolete I/O library, most famously used for MS DOS programming with the ancient Borland Turbo compilers. Some compilers still supported it up to year 2000 somewhere. This lib is mostly obsolete for multiple reasons, one of them being that modern programs typically don't use console I/O but window-based GUI.

In the rare event that you must actually use console I/O, then in Windows you would typically do that through the Windows console API. In Linux there's the library nCurses.

Lundin
  • 195,001
  • 40
  • 254
  • 396
-1

The return value of the function is 0 because the control reaches the closed brace without the return statement.

From the C Standard (5.1.2.2.3 Program termination)

1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;11) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

If you will write

return printf("hello world");

then the returned value will be equal to the length of the string literal "hello world".

From the C Standard (7.21.6.3 The printf function)

3 The printf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.

As for the header <conio.h> then it is not a standard C header and moreover in the presented program it is redundant because neither declaration from the header is used in the program.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335