Author Topic: C Program compiles without headers?  (Read 5032 times)

Offline crisdorya

  • Single posting newcomer
  • *
  • Posts: 5
C Program compiles without headers?
« on: November 26, 2013, 07:32:34 pm »
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...

Offline beqroson

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: C Program compiles without headers?
« Reply #1 on: November 26, 2013, 07:49:59 pm »
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.
« Last Edit: November 26, 2013, 07:58:15 pm by beqroson »

Offline crisdorya

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Program compiles without headers?
« Reply #2 on: November 26, 2013, 07:57:38 pm »
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.

Offline beqroson

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: C Program compiles without headers?
« Reply #3 on: November 26, 2013, 08:04:06 pm »
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++?

Offline crisdorya

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Program compiles without headers?
« Reply #4 on: November 26, 2013, 08:19:12 pm »
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...

Offline beqroson

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: C Program compiles without headers?
« Reply #5 on: November 26, 2013, 08:25:52 pm »
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.

Offline crisdorya

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Program compiles without headers?
« Reply #6 on: November 26, 2013, 08:33:53 pm »
This is interesting...
I will talk to my professor and ask him about this, it is quite weird indeed.
Thank you for the responses!

Offline beqroson

  • Multiple posting newcomer
  • *
  • Posts: 63
Re: C Program compiles without headers?
« Reply #7 on: November 26, 2013, 08:39:58 pm »
Sure. Next time, please note that this forum is not for such questions about general programming.

Offline crisdorya

  • Single posting newcomer
  • *
  • Posts: 5
Re: C Program compiles without headers?
« Reply #8 on: November 26, 2013, 09:27:45 pm »
I thought it was a setting in C::B that did this "unwanted magic"... Anyway thanks for the given help.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: C Program compiles without headers?
« Reply #9 on: November 27, 2013, 01:07:19 am »
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)