Thread: Newbie - How do I know if I'm writing C code?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Newbie - How do I know if I'm writing C code?

    I'm new to C, have Borland's C/C++ 4.55 compiler installed on a Win98 system, am using SourceForge.net's Chimera PU239 IDE and have searched the forum for this question (actually, the question popped up while reading one of the 'C vs C++' threads - I love religious discussions...). I also have several intro books on C .

    C and C++ are not the same language.
    My compiler handles both C and C++.
    The IDE is basically an amped up text editor with stuff to launch the compiler and do some simple syntax checking.
    If I want to write C, how do I not mistakenly write C++ code? I know to stay away from CLASS, but are there headers I shouldn't use? What about keywords unique to C++? Other stuff I don't even know to ask about?

    If the answer isn't something simple, pointers to the appropriate resources would be appreciated. If the answer is simple, well, I always start something new by asking really stupid questions and keep asking untill I get smarter ;-)

    I'm hoping to use a C API available with another program (Lotus Notes) which also has a C++ API and I want to be sure I don't do anything that will damage databases or crash the server.

    TIA for any responses.

    Doug

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > If I want to write C, how do I not mistakenly write C++ code?
    By making sure you write 'C' code, and compile it with a C compiler. The only way to do this is to make sure you know the language - there isn't really any shortcut.

    Most compilers use the extension of the source file as a guide.
    Pretty much everything treats .c as a 'C' program.
    Common C++ filenames are .cpp, .cxx, .cc

    The way to tell which language you're compiling with, if you're not sure is as follows

    Code:
    #include <stdio.h>
    int main ( ) { 
        if ( sizeof('A') == sizeof(char) ) {
            printf( "C++ code\n" );
        } else {
            printf( "C code\n" );
        }
        return 0;
    }
    Compile this as a console / command line program and run it.

    Once you know you're compiling a C program, you'll get warnings when you step into C++ only territory.

    > I want to be sure I don't do anything that will damage databases or crash the server.
    Rest assured that you can mess this up in any language - this is all down to your skill, and nothing inherent in the languages.

    > The IDE is basically an amped up text editor
    The better IDE's allow you to configure them, so you should be able to choose between C and C++, and not just the amorphous C/C++

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Thanks!

    Salem,

    No shortcuts??? Man, this language sucks!!! ;-)

    Do you know of any references that lists stuff that only works in C++? I'm used to working in VBA type languages with an associated IDE that only allows you to pick the available methods/properties/etc so this wild and wooly world of C is lookin' kinda big.

    Actually, for the place I am now, I doubt if I'm going to get into very much trouble with the C/C++ code goofs so this is more of an intellectual exercise than a problem solving exercise.

    Thanks so much for the help, I appreciate your reply.

    Doug

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Salem

    Code:
    #include <stdio.h>
    int main ( ) { 
        if ( sizeof('A') == sizeof(char) ) {
            printf( "C++ code\n" );
        } else {
            printf( "C code\n" );
        }
        return 0;
    }
    out of curiosity why does this not work in C?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >out of curiosity why does this not work in C?
    It works with both C and C++, but the output is different. In C++ the size of a character literal is that of char, but in C it's int.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  2. Code writing issues Part 2...
    By jaybo684 in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2005, 08:28 AM
  3. Newbie - error in code
    By Chris361 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2003, 12:12 PM
  4. Need some help on writing code
    By A slow C++ user in forum C++ Programming
    Replies: 7
    Last Post: 02-20-2003, 09:32 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM