Why does this code run?

1
2
3
#include<cstdio>
int main()
{puts("abcde");}


I haven't put std:: before puts and main doesn't return.
The code was compiled and linked using 'Microsoft Visual C++ 2008 Express Edition'. Is there a way to make this code not compile?
I'd like to know why this compiles too. I don't see why it does but it does in MSVC and GCC.
For compatibility with the C standard library and the C Unicode TR, the C++ standard library provides the 25 C headers, as shown in Table 154.

Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace
scope. It is unspecified whether these names are first declared or defined within namespace scope of the namespace std and are then injected into the global namespace scope by explicit using-declarations.

[ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. — end example ] - IS
It was stupid of me to not see the header file itself before posting. Sorry. MS has first defined the header file in global namespace and then imported them into std using using.I might be wrong in some cases as there are a lot of #defines. I have only analysed that part which the IDE didn't grey out.

But why have the MS guys done it? Does it offer some advantage to keep identifiers in the global namespace?

Also, I've noticed that including <iostream> automatically seems to include <cstring> and <cstdio>. (I haven't actually seen the #includes, but the functions of those libraries work). Is this behaviour standard? If I forget to include to <cstring>, is it going to make my code not run on other compilers?
Last edited on
> Does it offer some advantage to keep identifiers in the global namespace?

I Suppose it is convenient for the implementers in cases where C++ parameter passing and return does not clash with the C mechanism; one does not have to write a separate <stdio.h> for C and another for C++.


> Also, I've noticed that including <iostream> automatically seems to include <cstring> and <cstdio>.
> Is this behaviour standard?

The standard allows it; an implementation may include one standard header in another.


> If I forget to include to <cstring>, is it going to make my code not run on other compilers?

The only assured, portable way to use std::strlen() is to #include <cstring> The only assured portable way to use ::strlen() is to #include <string.h>



Why does this code run even when main doesn't return?

1
2
3
#include<cstdio>
int main()
{puts("abcde");}
Last edited on
§3.6.1/5
If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
cppreference wrote:
If control reaches the end of a function without encountering a return statement, return; is executed (except in the main function, where return 0; is executed).
Flowing off the end of a value-returning function (except main) without a return statement is undefined behavior.


http://en.cppreference.com/w/cpp/language/return
MSVC++ 2008 gives compile error with this code:

1
2
3
4
5
6
7
#include<iostream>

int func()
{std::cout<<"Hello\n";}

int main()
{func();}


error C4716: 'func' : must return a value


However, this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using std::cout;

int func(int a)
{
	if(a==5)return 100;
}

int main()
{
	cout<<"1\n";
	cout<<func(5);
	cout<<"\n2\n";
	cout<<func(7)<<'\n'<<func(7);
	cout<<"\n3\n";
}


Gives a warning
warning C4715: 'func' : not all control paths return a value


Running this program gave this output:
1
100
2
-858993460
-858993460
3
Press any key to continue . . .


where -858993460 is CCCC CCCC in hexadecimal.

I didn't know this earlier that main gets special treatment w.r.t. return value.
Last edited on
Topic archived. No new replies allowed.