---
title: "What should main() return in C and C++?"  
description: "What should main() return in C and C++?"  
author: "Utpal Vishwas"  
published: 2023-07-07  
updated: 2023-07-10  
canonical: https://www.mindstick.com/forum/159001/what-should-main-return-in-c-and-c-plus-plus  
category: "visual c++"  
tags: ["c++", "programming language"]  
reading_time: 2 minutes  

---

# What should main() return in C and C++?

What should main() return in C and [C++](https://www.mindstick.com/blog/745/array-in-c-plus-plus)?

## Replies

### Reply by Aryan Kumar

In C and C++, the `main()` function is the entry point for all programs. It is the first function that is called when the program starts executing. The `main()` function can return an integer value, which can be used by the operating system to determine the status of the program.

- **If the program terminates normally, the** `main()` **function should return the value** `0`**.** This is the most common return value for `main()`.
- **If the program terminates abnormally, the** `main()` **function should return a non-zero value.** The specific value that is returned can be used by the operating system to indicate the type of error that occurred.

For example, the following code shows a `main()` function that returns `0` if the program terminates normally, and `1` if the program terminates abnormally:

C

```plaintext
int main() {
  // Do something
  if (something_went_wrong) {
    return 1;
  } else {
    return 0;
  }
}
```

It is important to note that the `main()` function must always return an integer value, even if you do not want to return any specific value. If you do not return an integer value, the compiler will generate an error.

In C, the `main()` function can also be defined with a `void` return type. This means that the `main()` function does not return any value. However, it is still recommended to return `0` from the `main()` function, even if the function is defined with a `void` return type.

Here is an example of a `main()` function that is defined with a `void` return type:

C

```plaintext
void main() {
  // Do something
}
```

In general, you should return `0` from the `main()` function, even if the function is defined with a `void` return type. This will help to ensure that the operating system can correctly determine the status of the program.


---

Original Source: https://www.mindstick.com/forum/159001/what-should-main-return-in-c-and-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
