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