473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

int main(void) { func(); func(6); return 0; } int func(

6
int main(void)
{
func();
func(6);
return 0;
}
int func(int x)
{
printf("hello world");
}

why this code gave no error and printed hello world twice ????
Feb 17 '10 #1
2 3713
weaknessforcats
9,208 Expert Mod 8TB
This situation occurs due to not using function prototypes. If you do, you will get an error and the build will fail.

As it is, func() produces a warning that it is not defined and is assumed an extern returning an int. The linker sees func() as an unresolved function and it has func(int) available. In C, func() just means a "function named func with unspcified arguments". So it links a call to func(int). Later it links func(6) to
func(int) and off you go.

This situation was fixed in C++ by implementing function overloading. since func() in C++ is a function with no arguments and func(int) is a different function with one argument.

I suggest in C that a) never accept a build as OK if it has any warnings and b) always use function prototypes.
Feb 17 '10 #2
Banfa
9,065 Expert Mod 8TB
If you are getting no compiler warnings or errors for that code you could probably afford to set the warning level of your compiler higher.
Feb 17 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Frederick Ding | last post by:
Hi, guys! I met a problem: Should I use "int main()" or "int main(void)", is that a kind of "style" problem? And which is the standard C99 recommend? If it's a "style" one, where can I get the...
5
by: # include | last post by:
what is the ((((return any no;))) in the main mean plz Ex. int main() { ; ; ; return ??;
6
by: rahulsinner | last post by:
Hello everyone, I have noticed everyone using "int main(void)".But doesnt the standard pronounces it as "int main(int argc,char *argv)".And if i don't specify arguments,why can't i simply put...
37
by: Army1987 | last post by:
Is that in the object line a conforming program? If so, why? If not, why? I'd expect it to be much like int main(void) { for (;;); } But if I compile it with lcc-win32 and run it in its...
36
by: x_knifer_x | last post by:
read the subject it says it all int main(void) is better than int main()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.