C compiler accepting c++ code!!

This is a discussion on C compiler accepting c++ code!! within the C Programming forums, part of the General Programming Boards category; I used many compilers like turboc ,gcc ,borland c,microsoft c terminal,,to test this problem. In c programming, there are many ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27

    Question C compiler accepting c++ code!!

    I used many compilers like turboc ,gcc ,borland c,microsoft c terminal,,to test this problem.
    In c programming, there are many syntaxes different from their c++ counterpart,and i have seen that the above PURE C COMPILERS are accepting c++ syntaxes in betwen c code in a .c file, without any warning.

    One example is commenting:
    in c, we use
    Code:
    /*comment*/
    as the syntax.
    but i see that the compilers accept c++ syntax within c code,as well:
    Code:
    //comment
    Another example is that some compilers accept
    Code:
    #include <iostream.h>
    which is used in c++, in c code whereas it should be
    Code:
    #include <stdio.h>
    similarly,i have seen in many places that many people use the c++command cout to print statements instead of printf and it works!

    Why is this so??Suppose that a compiler can also compile c++.Then also,the code is in a .c file.Then,why is the c++ code being accepted flawlessly?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by suryap.kv1bbsr View Post
    Why is this so??Suppose that a compiler can also compile c++.Then also,the code is in a .c file.Then,why is the c++ code being accepted flawlessly?
    Because there is probably an additional compiler switch that sets it for C or CPP mode... Look in your documentation.

    If you are looking for a compiler that is pure C (C-99, to be exact) try Pelles C It will very nicely puke on C++ syntax for you.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    gcc -ansi -pedantic prog.c
    will reject anything which isn't strict ANSI-C.

    Similarly, other compilers have a "strict" flag to reign in any vendor specific extensions to the C compiler. One almost universal extension being that // is allowed as a comment in a C program.

    Nearly all compilers work in "vendor extended C mode" by default, which is a bit of a trap for the unwary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974
    The // commenting style is not necessarily C++; this is also known as C99-style commenting.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    bbsr,odisha,india
    Posts
    27

    Thumbs up thanks

    thanks for your satisfying replies.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice requested, Code makes sense to me, not compiler
    By andrew.bolster in forum C Programming
    Replies: 53
    Last Post: 01-06-2008, 12:44 PM
  2. Compiler or code error?
    By Hunter2 in forum Windows Programming
    Replies: 2
    Last Post: 08-03-2003, 05:59 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21