News:

When registered with our forums, feel free to send a "here I am" post here to differ human beings from SPAM bots.

Main Menu

C Program compiles without headers?

Started by crisdorya, November 26, 2013, 07:32:34 PM

Previous topic - Next topic

crisdorya

Hello,

I am a student and use C::B in order to code C applications.
I found out recently that most of my apps weren`t working outside of my computer. I found out that I was not including all the Headers that I needed, like I could access strlen(); without including string.h, which is strange indeed!
So I decided to test something and this is what I wrote:

int main()
{
    printf("%d", strlen("That is bad!"));
    return 0;
}

It should give me errors, but it does not! It actually compiles and runs like a charm! The compiled exes do so too! (I used a File and under a Project)
I do not understand what settings are wrong, but I fear that most of my assignments would not compile on any other computer!

Could some one help me please with my anomaly? Thank you! 

PS: I once used a pointer in a wrong way, and it compiled, but it threw a error when compiling it at my lab.

Note: I use C::B 12.11 and the GCC compiler while at the lab we use C::B 10.50 I think...

beqroson

#1
Well, do you feel lonely, or why do you come to this forum? The answer to your question is that you are including the proper headers, I just do not know where. Since you did not format the code example properly and do not give the full view of the code.

Edit: Actually, if you use C, then the printf would be implicitly defined at the place of first reference.

crisdorya

I come to this forum for help, that is why it`s the "Help" section now isn`t it?

The code I wrote is the entire program, it should not run, but it does. I include no header and still C::B gives me no error, even better, it gives me the Auto-Complete pop-out for any function (like strlen) even if I include no header for it!
That is my problem, and I do not understand what`s going on. I tried a fresh reinstall but that does not "fix" it, it has to be a setting or something.

beqroson

The C language is different from C++. C will accept such things, there is nothing wrong, except for if you think that the behavior is wrong. Why do you not use C++?

crisdorya

Because our course is in C, and it is enforced upon us.
So how come? I do not understand... So I do not need to white #include? Then why doesn`t it work on the Labs computers. Is there a setting in C::B that would enforce headers to be included, or would it work in any circumstance without the #include? I have to admit that sounds weird...

beqroson

I cannot tell you for sure, but my guess is that you use a flag for nostdlib, that will make it not link to std lib and (I guess) that would create an error instead. Otherwise it will link to std lib by default. I usually do not program in C, so I am only guessing to what the reason is. But I cannot see why the behavior should be wrong, in particular. Probably just related to some flag and std lib.

crisdorya

This is interesting...
I will talk to my professor and ask him about this, it is quite weird indeed.
Thank you for the responses!

beqroson

Sure. Next time, please note that this forum is not for such questions about general programming.

crisdorya

I thought it was a setting in C::B that did this "unwanted magic"... Anyway thanks for the given help.

BlueHazzard

this is really OT here, but this slide-show will explain a bit what is happening here: http://de.slideshare.net/olvemaudal/deep-c (the first 20 slides)
probably in your lap you are using an c++ compiler (the file ending is cpp and not c)