因为工控上有时候用c比较方便
请给出示例代码,谢谢

解决方案 »

  1.   

    VC不过是个C++的版本VS是个编译工具可以编译C的代码
      

  2.   

    Linkage to Non-C++ Functions
    C functions and data can be accessed only if they are previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.Syntaxlinkage-specification :extern string-literal { declaration-listopt }
    extern string-literal declarationdeclaration-list :declaration
    declaration-list declarationMicrosoft C++ supports the strings "C" and "C++" in the string-literal field. The following example shows alternative ways to declare names that have C linkage:// Declare printf with C linkage.
    extern "C" int printf( const char *fmt, ... );//  Cause everything in the header file "cinclude.h"
    //   to have C linkage.
    extern "C"
    {
    #include <cinclude.h>
    }//  Declare the two functions ShowChar and GetChar
    //   with C linkage.
    extern "C"
    {
        char ShowChar( char ch );
        char GetChar( void );
    }//  Define the two functions ShowChar and GetChar
    //   with C linkage.
    extern "C" char ShowChar( char ch )
    {
        putchar( ch );
        return ch;
    }extern "C" char GetChar( void )
    {
        char ch;    ch = getchar();
        return ch;
    }// Declare a global variable, errno, with C linkage.
    extern "C" int errno;
      

  3.   


    Mixed-Language Programming with C++
    Home |  Overview |  How Do IC++ uses the same calling convention and parameter-passing techniques as C, but naming conventions are different because of C++ decoration of external symbols. By causing C++ to drop name decoration, the extern "C" syntax makes it possible for a C++ module to share data and routines with other languages.The following example declares prn as an external function using the C naming convention. This declaration appears in C++ source code.extern "C"
    {
        void prn();
    }To call functions written in Fortran (or MASM), declare the function as you would in C and use a "C" linkage specification. For example, to call the Fortran function FACT from C++, declare it as follows:extern "C" { int __stdcall FACT( int n ); }The extern "C" syntax can be used to adjust a call from C++ to other languages, or to change the naming convention of C++ routines called from other languages. However, extern "C" can be used only from within C++. If the C++ code does not use extern "C" and cannot be changed, you can call C++ routines only by determining the name decoration and generating it from the other language. You can always determine the decoration by using the DUMPBIN utility. Use this approach only as a last resort, because the decoration scheme is not guaranteed to remain the same between versions.Use of extern "C" has some restrictions: You cannot declare a member function with extern "C".
    You can specify extern "C" for only one instance of an overloaded function; all other instances of an overloaded function have C++ linkage. 
    For more information on the extern "C" linkage specification, see Linkage Specifications in C++ Language Reference.
      

  4.   

    to isdong(有些事情应该忘记):我当然知道它可以编译c的代码,我的意思是c++与c混合编程
                  比如用vc造个对话框,但是框里显示的一些数据是通过c与
                                单片机通讯获得的
      

  5.   

    你太在乎C和C++这两个名字了,你把你的C函数拿过来,直接放在VC里编译,通过以后,你想怎么调就怎么调,至于显示数据之类的,无非是些变量传递而已
      

  6.   

    可以,不过VC对中断等的访问逐渐被禁用了,你可以使用汇编或者使用VC以前的版本作个DLL然后使用VC作界面来调用,这样更好
      

  7.   

    to fang_jb(寂寞如雪):你的意思是,比如在一个基于对话框的工程中,我只要把一些与c
                         有关的头文件(如stdio)include上,我就能直接用这些c的库函数了?
               ( 如kbhit())
    to isdong(有些事情应该忘记):呵呵,小弟系初学,对于dll之类的还不甚了解,以后在这些
                  还请多多指教
      

  8.   

    kbhit是基于控制台的吧,这个估计不行,就象在对话框里用scanf一样,不会给你程序中止等待键盘输入的时间
      

  9.   

    to  fang_jb(寂寞如雪) : 只要include进来conio.h,_kbhit()也好用