Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 206d305

Browse files
Add code for int main with no return.
1 parent 3aa5a1d commit 206d305

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

‎009-int-main-no-return/Makefile‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# generate c++17 ast, llvm ir, assembly, and debuggable main executable
2+
all:
3+
clang++ -std=c++17 -Xclang -ast-dump -fsyntax-only main.cpp > main.ast && \
4+
clang++ -std=c++17 -S -emit-llvm main.cpp -o main.ll && \
5+
clang++ -std=c++17 -S main.cpp -o main.s && \
6+
clang++ -std=c++17 -g main.cpp -o main
7+
8+
clean:
9+
rm -rf main.dSYM main main.ast main.ll main.s

‎009-int-main-no-return/README.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# No Return in Int Main
2+
3+
## TL;DR
4+
5+
- I would always write `return 0;` inside the `int main()` function because I thought it was required
6+
- This may have been a force of habit from learning C in a computer architecture course in college
7+
- Turns out that `return 0;` is option, and not required in `int main()`
8+
- The following code (with no return statement in the main function) will compile and run correctly
9+
- In order to investigate why, we need to understand the compiler. (In this case Clang.)
10+
11+
```cpp
12+
int main() {
13+
int a = 123;
14+
}
15+
```
16+
17+
## Compiler Steps
18+
19+
The compiler steps:
20+
21+
1. C++ (source code)
22+
2. AST (clang frontend)
23+
3. IR (intermediate representation) -> happens in this stage
24+
4. Assembly
25+
5. Binary/Executable
26+
27+
The compiler steps in-depth (thanks ChatGPT):
28+
29+
1. C++ Source Code → your .cpp text file.
30+
2. AST (Abstract Syntax Tree) → Clang frontend parses the source into a tree of functions, variables, statements, etc.
31+
3. IR (LLVM Intermediate Representation) → platform-independent SSA-like code for optimizations.
32+
4. Assembly → CPU-specific instructions (.s).
33+
5. Binary / Executable → linked machine code you can run (a.out or your program).
34+
35+
## Why?
36+
37+
Why is `return 0;` optional in `int main()`?
38+
39+
- Many C compilers would `return 0;` by default
40+
- GCC, MSVC did this because OS expected return value
41+
- It would be undefined behavior in C++ otherwise
42+
- This is one of biggest concerns for C++ in this day
43+
- So much so that US govt asking companies to use safe languages
44+
- Was standardized in C++11
45+
- But C++ compilers basically did this before that
46+
47+
## References
48+
49+
- ChatGPT and Gemini (if these count)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
TranslationUnitDecl
2+
|- TypedefDecl __int128_t '__int128' (implicit)
3+
| `- BuiltinType '__int128'
4+
|- TypedefDecl __uint128_t 'unsigned __int128' (implicit)
5+
| `- BuiltinType 'unsigned __int128'
6+
|- TypedefDecl __NSConstantString '__NSConstantString_tag' (implicit)
7+
| `- RecordType '__NSConstantString_tag'
8+
| `- CXXRecord '__NSConstantString_tag'
9+
|- TypedefDecl __builtin_ms_va_list 'char *' (implicit)
10+
| `- PointerType 'char *'
11+
| `- BuiltinType 'char'
12+
|- TypedefDecl __builtin_va_list '__va_list_tag[1]' (implicit)
13+
| `- ConstantArrayType '__va_list_tag[1]' size=1
14+
| `- RecordType '__va_list_tag'
15+
| `- CXXRecord '__va_list_tag'
16+
`- FunctionDecl main 'int ()'
17+
`- CompoundStmt
18+
`- DeclStmt
19+
`- VarDecl a 'int' cinit
20+
`- IntegerLiteral 123

‎009-int-main-no-return/main.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// #include <iostream>
2+
3+
int main() {
4+
int a = 123;
5+
// std::cout << a << std::endl;
6+
return 0;
7+
}
8+
9+
// int main() {
10+
// // return 0;
11+
// }

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /