简体   繁体   English

代码是C还是C++?

[英]Is the code C or C++?

I know the code below used to be C, however, I wrote it in visual studio 2008 as a c++ program and it works fine (it is saved as C++).我知道下面的代码曾经是 C,但是,我在 visual studio 2008 中将它编写为 c++ 程序并且它工作正常(它被保存为 C++)。 However, the program is in C code, correct?但是,程序中的代码是C,对吗? (or is it?). (或者是吗?)。

So, when I tried to compile it inside Visual Studio as C (Go to-> Properties of file -> c/c++ ->Advanced -> Compile as -> changed it to 'Compile as C code') I then get many errors, the main of which it does not recognize the LPSTR type.因此,当我尝试在 Visual Studio 中将其编译为 C(转到 -> 文件属性 -> c/c++ -> 高级 -> 编译为 -> 将其更改为“编译为 C 代码”)时,我遇到了很多错误, 主要是它不识别 LPSTR 类型。 So, I guess my question is: is it C or C++ code and if it is C, why did it not work when I changed it to compile C code?所以,我想我的问题是:它是 C 还是 C++ 代码,如果是 C,为什么当我将其更改为编译 C 代码时它不起作用?

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <strsafe.h>
#include <direct.h>
#include <string.h>
#include <conio.h>

int main(VOID)
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    //allocate memory
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));


    //create child process
    if (!CreateProcess(NULL,
                L"C:\\Windows\\Notepad.exe",
                NULL,
                NULL,
                FALSE,
                0,
                NULL,
                NULL,
                &si,
                &pi))
    {
        fprintf(stderr, "create process failed");

        return -1;
    }

    //parent waits for child to complete
    WaitForSingleObject(pi.hProcess, INFINITE);

    printf("Child Complete");

    //close handle
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hthread);

}  

It compiles cleanly as C .它干净地编译为 C The only error is triggered by CloseHandle(pi.hthread) , since it's not a member of PROCESS_INFORMATION .唯一的错误是由CloseHandle(pi.hthread)触发的,因为它不是PROCESS_INFORMATION的成员。 You're looking for hThread (capital T).您正在寻找hThread (大写字母 T)。

It's C. But most C++ compilers are capable compiling C code.它是 C。但是大多数 C++ 编译器都能够编译 C 代码。 The header conio.h is not a part of standard C so you may want to avoid that. header conio.h不是标准 C 的一部分,因此您可能希望避免这种情况。

LPSTR is defined in windows.h and since you have included it, it shouldn't give any error. LPSTR在 windows.h 中定义,因为您已经包含它,所以它不应该给出任何错误。 It is typedef'ed:它是类型定义的:

typedef char* PSTR, *LPSTR; 

For other typedef'ed variables in your code, you have to check if their headers.对于代码中的其他 typedef 变量,您必须检查它们的标头。 Try including Winbase.h尝试包含Winbase.h

From what you give we can't see if it is valid C because you are using non-standard headers and macros.从您提供的内容我们看不到它是否有效 C 因为您使用的是非标准标头和宏。 But I can imagine that there could be macro definitions that turns this into C code.但我可以想象,可能会有宏定义将其转换为 C 代码。

If it is C, it is not so good one, and not so good one for C++ either.如果它是 C,它就不是那么好,对于 C++ 也不是那么好。 Both languages have their way of initializing variables, but your code uses none of them.两种语言都有它们初始化变量的方式,但您的代码没有使用它们。 In particular in C (and for PODs in C++) there are initializers for initializing variables.特别是在 C(以及 C++ 中的 POD)中,有用于初始化变量的初始化程序。 Supposing that both ugly macros here expand to types:假设这里的两个丑陋的宏都扩展为类型:

STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };

of if you have a C99 compliant compiler the first would even better be如果你有一个 C99 兼容的编译器,第一个会更好

STARTUPINFO si = { .cb = sizeof si };

If for some reason you'd need a function to zero out a whole block of memory, better use the standard function memset for that.如果出于某种原因你需要一个 function 来将整个 memory 块清零,最好使用标准的 function memset But if you use the correct initializers in C (or constructors in C++) you should rarely need that explicitly.但是,如果您在 C 中使用了正确的初始值设定项(或 C++ 中的构造函数),那么您应该很少明确地需要它。

every c code is a valid c++ code.每个 c 代码都是有效的 c++ 代码。 but its not the other way around.但事实并非如此。 So if you want to use OS api's which are c++ then you should compile it as c++.因此,如果您想使用 c++ 的 OS api,则应将其编译为 c++。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM