Question about namespace

Need help with C, C++, perl, python, etc?

Question about namespace

Postby pepribal » 2006-11-15 15:17

Hi. I've got a question about namespaces. Let's say we have the program:

#include <cstdio>
using namespace std;

int main(void)
{
printf("Just a test\n");
return 1;
}


As I've read, the #include directive places the cstdio functions in the std namespace. That's whay we have to write the using sentence. Tell me if I am wrong, but without this using statement we would have to write:

std::printf("whatever");

But I must have missed something, as I've tried the same program commenting out the using statement, and I've been surprised to see that the program compiles, and executes perfectly with g++.

Someone knows why?

Thanks.
pepribal
 
Posts: 5
Joined: 2006-10-22 17:54

Postby brain » 2006-11-15 17:28

You're absolutely right. The thing is that the functions from stdio.h is placed BOTH in the std namespace AND the global namespace for compability reasons when including the cstdio header. However, the right thing to do it to either use std::printf(); or using std::printf();
User avatar
brain
 
Posts: 253
Joined: 2006-04-25 17:15

Postby pepribal » 2006-11-15 18:22

Thanks brain,

however, I assume that compatibility you are talking about is meant to be with early C++ versions, isn't it? I mean, it can't be with standard C, because in C you cannot write #include <cstdio>.

Is my supposition right?
pepribal
 
Posts: 5
Joined: 2006-10-22 17:54

Postby lacek » 2006-11-15 20:38

Actually, you can #include any file (I mean, the file doesn't need to end with .h, altough it's a convention) in a c code, at least with gcc (I don't have experience with other compilers, but I guess they behave the save way).
lacek
Moderator Team Member
 
Posts: 769
Joined: 2004-03-11 18:49
Location: Budapest, Hungary


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable