Author Topic: C::B compiles in K&R C and not ANSI C...?  (Read 12429 times)

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
C::B compiles in K&R C and not ANSI C...?
« on: May 19, 2009, 07:13:27 pm »
Hello everyone :)

I think that my C::B is compiling in K&R and not in ASNI C..

ie. with this code
Code: [Select]
#include <stdlib.h>

int main (void) {

    char *pfilename;
    pfilename = malloc( 1 );

return 0;
}

returns
Quote
error: invalid conversion from `void*' to `char*'

Of course this
Code: [Select]
pfilename = (char*)malloc( 1 ); compiles without errors.. But this is not ANSI C right?


I tried selecting an other compiler from C::B option but not luck.
Even tried the devC++ compiler but nothing..

Any ideas? what is wrong?


Thank you very much!

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #1 on: May 19, 2009, 07:46:39 pm »
The "flavor" of C being compiled is a function of the compiler, not the IDE.  Depending upon the compiler you choose and the options you select (specific to that compiler) you will get different modes of compiling.  Note that (AFAIK) the Dev-C++ IDE uses only the GCC compiler.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #2 on: May 19, 2009, 08:03:00 pm »
Yes i am aware of that :)

But i have GNU GCC Compiler selected at C::B compiler options and i tried the same flags..

So i guess there is also something else i need to change in the IDE's option.

Thx for the reply

Offline AlexN

  • Multiple posting newcomer
  • *
  • Posts: 13
    • Alex's Link Sammlung
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #3 on: May 19, 2009, 08:31:20 pm »
Quote
error: invalid conversion from `void*' to `char*'

Of course this
Code: [Select]
pfilename = (char*)malloc( 1 ); compiles without errors.. But this is not ANSI C right?


Thank you very much!

I my opinion this is behavior is more ANSI C then K&R. ANSI C checks the type, K&R C assigns all to all.  :)

Alex  ;)
best regards
 Alex ;)

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #4 on: May 19, 2009, 08:37:48 pm »
well ANSI C does not need any casting to mallocs

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #5 on: May 19, 2009, 08:51:48 pm »
By default the GCC C-compiler operates in "C99" mode, which does require a cast of one pointer type to another (in this case a "void *" to a "char *").  C99 is neither K&R nor ANSI, but has elements of both.

However, at this point we are no longer talking about C::B, but the multitude of C standards out there.  You could get better help by asking the guys on USENET on the "comp.lang.c" list.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #6 on: May 19, 2009, 09:04:57 pm »
r u sure about this?

Anyway.. Even so the problem still remains..

Not solved even when i put the -ansi flag at the compiler options.

So i still think it has something to do with C::B. I hadn't that problem with DevC++ or Pelles C.


Thanks

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #7 on: May 19, 2009, 09:39:36 pm »
Actually, I copied your original code into a brand new C::B "console" project, and it compiled and ran just fine.  No problems at all.  Apparentely the GCC optimizer is smart enough to translate a "void *" into a "char *" with no problems.

Are you sure you are compiling a "C" project, and not a "C++" project?  Is your main program named "main.c" or "main.cpp" ?

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #8 on: May 19, 2009, 09:48:18 pm »
(!) (!)
Yes i am.. I posted that only this small part for illustration reasons.

the whole program is much bigger with not other errors.. Just 2 errors of this kind..

What is more i reinstalled C::B and same happened.. :(
any ideas?


thx 4 helping :)

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #9 on: May 19, 2009, 10:02:22 pm »
When I create a default C-language project, the only compiler options I have checked is optimization "O2", and your code snippet compiled good.

What options do you have checked?  Also, what version of GCC are you using?  My system is using GCC 3.4.4

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #10 on: May 19, 2009, 10:17:21 pm »
not sure.. in the options its GNU GCC Compiler

i have tried with various flags checked. and i just tried only with checking O2 but no difference

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #11 on: May 19, 2009, 10:38:20 pm »
Even with -pedantic warnings turned on, I cannot get it to fail:


mingw32-gcc.exe -pedantic -W -Wall -ansi  -O2     -c C:\temp\test1\main.c -o obj\Release\main.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

 
Can you show us your full compiler log output?

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #12 on: May 19, 2009, 10:57:42 pm »
sure, but its actually just that
Code: [Select]
\f.c        In function `int main()':
\f.c   6   error: invalid conversion from `void*' to `char*'
             === Build finished: 1 errors, 0 warnings ===

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #13 on: May 19, 2009, 11:18:26 pm »
Please turn on full-command-line logging (Settings -> Compiler and Debugger -> Other Settings then "Compiler Logging") then try to compile again.  Copy that log output into here, so we can see what all (hidden) settings are being used.

Also, can you open a command window (for example, a DOS window or a MSys xterm) and type "gcc -v" to see what version of GCC you are using.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #14 on: May 19, 2009, 11:35:44 pm »
ok i enabled full-command-line logging but the message is exactly the same as above..

i open a cmd window and type gcc -v but it is not found..(i guess because its not in the windows directory)

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7265
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #15 on: May 20, 2009, 12:05:32 am »
ok i enabled full-command-line logging but the message is exactly the same as above..

Please post the commandline !!!

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #16 on: May 20, 2009, 12:07:42 am »
Did you see my example above, where it showed the compiler being called?  Yours has it also (somewhere).  That is the command I need to see.

For the version number ... open the command window, then CD to the directory with the gcc compiler.  Then issue the command "gcc -v".  I need to know what version of gcc you  are using.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #17 on: May 20, 2009, 09:51:21 am »
ok gcc version is 3.4.2 (mingw-special).

ok i see your line. its the build log. Here is mine
[ 50.0%] mingw32-g++.exe -pedantic -Wall -ansi -g    -c "C:\...\1.c" -o "C:\...\1.o"

it is using g++ instead of gcc.. why?
In compiler settings mingw32-gcc.exe is selected as default C compiler.
mingw32-g++.exe is selected for C++.

The file is blabla.c and when i create a new one i select C, not C++..


Thx

Offline rcoll

  • Almost regular
  • **
  • Posts: 150
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #18 on: May 20, 2009, 06:50:04 pm »
The g++ compiler of course expects all input files to be be C++ files; I'm not sure how you got to this point.

Go to Settings->Compiler and Debugger and on the "Selected Compiler" list select the compiler you are using (probably called GNU GCC Compiler), then click the tab called "Toolchain executables".  There will be both a C compiler and a C++ compiler listed there.  Make sure that "mingw32-gcc.exe" is listed for the C compiler.

Ringo

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #19 on: May 20, 2009, 07:54:01 pm »
Thing is that i did not change anything there..

yes i already checked there and its mingw32-gcc.exe as C compipler.

But for some reason it uses the mingw32-g++.exe compiler which is the selection for C++.

It will only work if i put mingw32-gcc.exe as C++ compiler(!)

Seems like it cannot see that my file is a C file (despite the fact that its .c)

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #20 on: May 21, 2009, 02:13:10 pm »
Seems like it cannot see that my file is a C file (despite the fact that its .c)
Look at the file's (!) properties what compiler environment is selected there. Maybe you changed that by accident.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #21 on: May 21, 2009, 02:43:20 pm »
what do u mean? where do i find this?
file-> properties? (nothing strange there..)


Thank you

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #22 on: May 21, 2009, 07:03:53 pm »
what do u mean? where do i find this?
File -> properties -> tab "advanced" -> compiler variable and compiler. Do *not* modify just post what's written there.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #23 on: May 22, 2009, 12:22:01 am »
erm.... nothing?!!

check the image attached

[attachment deleted by admin]

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #24 on: May 22, 2009, 06:47:06 am »
erm.... nothing?!!
...but you*did* create a project? Or do you try to compile a single file? If so - create a project. ;-)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #25 on: May 22, 2009, 06:13:51 pm »
erm... no i didn't :shock:

i tried with project and the file compiles jsut fine! thank you!

But again.. why it did not compile as a single file? More specifically why it does compile but uses the wrong compiler?
Has to do with the project thing?

ie In ms visual C++ IDE u cannot (i think:P) compile a single file, *but* the compile etc buttons are non-clickable..

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #26 on: May 25, 2009, 07:15:49 am »
why it does compile but uses the wrong compiler?
This is by design. Supporting single-file compilation has limitations (for several good reasons). Actually I would vote to disable it completely to avoid such misunderstandings.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline LiK

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #27 on: May 25, 2009, 12:09:36 pm »
hm.. ok now i understand :)

btw.. better disable it completely or warn that it only compiles C++ single files!

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #28 on: May 31, 2009, 03:46:23 pm »
Quote from: BloodyCake
Actually I would vote to disable it completely to avoid such misunderstandings.

I would vote to improve it (that's what I use the most of C::B) :D

Online stahta01

  • Lives here!
  • ****
  • Posts: 6589
    • My Best Post
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #29 on: June 01, 2009, 01:16:05 am »
Quote from: BloodyCake
Actually I would vote to disable it completely to avoid such misunderstandings.

I would vote to improve it (that's what I use the most of C::B) :D

I would vote to have an option to enable it; that by default is disabled.
Tim S
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: C::B compiles in K&R C and not ANSI C...?
« Reply #30 on: June 02, 2009, 08:14:55 am »
Quote from: BloodyCake
Actually I would vote to disable it completely to avoid such misunderstandings.
I would vote to improve it (that's what I use the most of C::B) :D
I would vote to have an option to enable it; that by default is disabled.
I change my vote to have an "all-in-one device suitable for every purpose" a.k.a. "Swiss Army knife". I am already developing the coffee maker plugin... stay tuned. :lol:
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ