Difference between int main() & int main(void)

Difference between int main() & int main(void)

#C #C++ #Programming Languages


In C:

  • int main(): An empty parameter list in C means the function's parameters are unspecified. It does not explicitly state that the main function takes no arguments.
  • int main(void): This explicitly indicates that the main function takes no arguments. It's the preferred modern style in C to avoid ambiguity.

In C++:

  • int main(): In C++, an empty parameter list explicitly means that the main function takes no arguments. There is no ambiguity.
  • int main(void): Although this is legal in C++, it is considered a relic from C. In C++, writing void inside the parentheses is unnecessary because int main() already indicates no parameters.


Practical Considerations

  • Standards Compliance: Modern C code tends to favour int main(void) to adhere more strictly to the language standard and to improve code clarity.
  • Compatibility: Both forms are accepted by most compilers, but if you want to avoid any potential ambiguity (especially when teaching or writing new code), using int main(void) is a good practice.


Summary

While you can write int main() in C, using int main(void) is recommended for clarity and explicitly indicating that the function does not take any parameters.


要查看或添加评论,请登录

Anushkaa Ambuj的更多文章

  • Why do we 'return 0' at the end of int main()?

    Why do we 'return 0' at the end of int main()?

    #C #C++ #Programming Languages C In C, the int main() or int main(void) function is declared to return an int. Hence…

  • Shebang Line (#!)

    Shebang Line (#!)

    #Python #Bash #JS #NodeJS #Interpretors #Programming Languages What Is a Shebang Line? A shebang () is the first line…

  • Why midpoint is calculated as 'low + (high-low)/2' instead of (low+high)/2?

    Why midpoint is calculated as 'low + (high-low)/2' instead of (low+high)/2?

    The formula is often used to calculate the mid-value (or midpoint) between two numbers, and , rather than using the…

  • DSA : Day 2

    DSA : Day 2

    Understanding Problem Constraints and Time Complexity Happy Learning! Happy Programming! Topics 1?? Relation of…

  • DSA : Day 1

    DSA : Day 1

    Happy Learning! Happy Programming! Topics 1?? Decimal to Binary, Binary to Decimal ??Binary of -ve numbers ? Sign…

  • SDE Internship @JohnsonControls

    SDE Internship @JohnsonControls

    ??DAY 7: DEVELOPMENT Exploring Angular18 Topics What are Directives? What are Pipes? How to create custom pipe? How to…

    3 条评论
  • SDE Internship @JohnsonControls

    SDE Internship @JohnsonControls

    ??DAY 3: TESTING Manual Testing Terms 3.1 Priority vs Severity (BUGS) 'Severity' = How serious is the defect?…

  • SDE Internship @JohnsonControls

    SDE Internship @JohnsonControls

    ??DAY 6: DEVELOPMENT Tasks Task 1: Setting Up AngularJS on Windows To build a new angular project Step 1: Install…

  • SDE Internship @JohnsonControls

    SDE Internship @JohnsonControls

    ??DAY 5: TESTING [Automation | Selenium] Selenium is a powerful tool for web automation, and one of its core…

  • SDE Internship @JohnsonControls

    SDE Internship @JohnsonControls

    ??DAY 4: TESTING [Automation] 4.1 When does Automation Testing start? Ans- When the build is stable and no frequent…

社区洞察

其他会员也浏览了