- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C/C++ difference's between "int main()" and "int main(void)"
C
In C programming language, if a function signature is not having any parameters then it can take multiple arguments as input but the same is not true with C++. The compilation will fail if arguments are passed to such a function in C++. This is reason int main() and int main(void) are same in C, but int main(void) is better approach, which restricts the user to pass multiple arguments to main function.
Example (C)
#include <stdio.h> int main() { static int counter = 3; if (--counter){ printf("%d ", counter); main(5); } }
Output
2 1
Example (C++)
#include <iostream> using namespace std; int main() { static int counter = 3; if (--counter){ cout << counter; main(5); } }
Output
main.cpp: In function 'int main()': main.cpp:10:13: error: too many arguments to function 'int main()' main(5); ^ main.cpp:5:5: note: declared here int main() ^~~~
- Related Articles
- Difference between “int main()” and “int main(void)” in C/C++?
- Difference between void main and int main in C/C++
- Differentiate between int main and int main(void) function in C
- Difference between const int*, const int * const, and int const * in C/C++?
- Difference between const int*, const int * const, and int const * in C
- What is difference between int and const int& in C/C++?
- Write the main difference between '==' and '===' operators in javascript?
- What is the difference between const int*, const int * const, and int const *?
- what is the main difference between '=' and '==' operators in javascript?
- Difference Between int and long
- What is the difference between size_t and int in C++?
- What is the difference between int and Int32 in C#?
- What is the main difference between int.Parse() and Convert.ToInt32 in C#?
- Can we change the order of public static void main() to static public void main() in Java?
- C/C++ difference's between strncmp() and strcmp.

Advertisements