ภาษา C
ในภาษา C หากลายเซ็นของฟังก์ชันไม่มีพารามิเตอร์ สามารถรับอาร์กิวเมนต์หลายตัวได้ แต่ใน C++ ไม่เป็นเช่นนี้ การคอมไพล์จะล้มเหลวหากส่งอาร์กิวเมนต์ให้ฟังก์ชันแบบนี้ใน C++ นี่คือเหตุผลที่ int main() และ int main(void) เหมือนกันใน C แต่ int main(void) เป็นแนวทางที่ดีกว่า ซึ่งจำกัดผู้ใช้ไม่ให้ส่งอาร์กิวเมนต์หลายตัวให้ main function
ตัวอย่าง (C)
#include <stdio.h>
int main() {
static int counter = 3;
if (--counter) {
printf("%d ", counter);
main(5); // ส่งอาร์กิวเมนต์ได้ใน C
}
}
เอาต์พุต (C)
2 1
ตัวอย่าง (C++)
#include <iostream>
using namespace std;
int main() {
static int counter = 3;
if (--counter) {
cout << counter;
main(5); // Error ใน C++
}
}
เอาต์พุต (C++)
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() ^~~~