Quick question?

This is a discussion on Quick question? within the C++ Programming forums, part of the General Programming Boards category; Hi, Just wondering if anyone knows the command to wrap a section of code in C++ so that it is ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    1

    Quick question?

    Hi,

    Just wondering if anyone knows the command to wrap a section of code in C++ so that it is C instead, but the whole file stays as C++?


    Thanks in advance

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    What??? I'm not sure what you are trying to do but you can call C functions from within a C++ source file. If you make a C source code file of your own and then try to call them from within a C++ source file, then your prototypes would need an extern "C" declaration. If you #include a C header when you are compiling a C++ source file, you can see in the headers that they typically automatically do this for you with some type of declaration along the following:

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    // misc. declarations in here
    
    #ifdef __cplusplus
    }
    #endif
    If you are compiling a C file then when the headers are read, these are ignored. However when you are compiling a C++ source file, then the headers automatically have these added for you. Not sure if this is what you are asking...

    [edit]The above may be implementation dependant, I don't know. This was for the headers installed under MSVC. Somebody know if this standardized in any way or even required in other compilers?[/edit]
    Last edited by hk_mp5kpdw; 08-13-2004 at 05:44 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Read this FAQ section 32

    > but the whole file stays as C++?
    Use what hk_mp5kpdw showed you.
    You can call 'C' functions from C++, but in any single module of code, it will be compiled as C or C++.

    > Somebody know if this standardized in any way or even required in other compilers?
    I think the approach you have is standard amongst all ANSI compliant C++ compilers.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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