Can I use cstdio in a C program? - c

I get a ton of errors in cstdio when I add #include <cstdio>
to the C program.
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2143: syntax error : missing '{' before ':'
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2059: syntax error : ':'
Thanks
EDIT - I would like to use snprintf, which is why I am trying to include this.

You want #include <stdio.h>. cstdio is the C++ wrapper for the C header.
Edit: MSVC only supports the elements in C99 that form a subset of C++.
This site has a C implementation of snprintf() licensed under the GPL.

With Visual Studio, I believe you have to use sprintf_s or something similar. See this. There's also vsnprintf.

MSVC offers the _snprintf function in stdio.h.
If you prefer not to use the leading underscore, you can:
#include <stdio.h>
#define snprintf _snprintf
This is a C library function, not specifically related to C++ (although you can use it there too).

Related

variable "using" is a not a type name?

I am currently trying to create an application in C as an assignment for school, and my professor requires me to use the following to make it compatible with his compiler:
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>
(I am using visual studio)
And for some reason #include <string> throws up a bunch of run time errors mainly consisting of the one in the title of the question. It pops up within a bunch of other dependencies ranging from cmath, cstudio, cstdlib, cstring, etc. So the bottom line is, can anyone tell me how to fix it? Thanks!
In visual studio you can get this error if you extension is '.c' VS studio will assume the file is type c and not c++ and use the c compiler and not the c++.
In the project settings under advanced options you can select compile as C++ and then files will all be compiled as C++. If you scroll down you will find the flag '-x' on the command line as added to it.
If you are using the gcc compiler this is the same as adding the following to the compiler command '-x c++'
Check the gcc reference for this flag.
The #include <string> is a C++ header file and therefore incompatible with C.
As others have suggested, you can change this to #include <string.h> so that you have a valid C header, however if your professor dictated that you use that specific set of headers, then you need to change your source file to a .ccp file instead of a .c file.
You're including C++ header file. Use #include <string.h>
You are including a C++ library in C code. The header files without any extension are C++ header files. Having ".h" extension are C header files. C++ also accepts C header files still they have no extension. They have given same names having "c" prefix and no ".h" extension.
for example
<string>
is a C++ header
<string.h>
is a C header
<cstring>
is a C header but in a C++ code.

C program compilation error

I am new to programming and picked up the C language as my first computer language. I was doing a sample conversion program and when i compiled the code, i get the following error.
What does it mean and how do i fix it?
Thanks!
1>------ Build started: Project: ConversionProgram, Configuration: Debug Win32 ------
1>Build started 24-07-2012 20:58:37.
1>InitializeBuildStatus:
1> Touching "Debug\ExcerciseProgram1.unsuccessfulbuild".
1>ClCompile:
1> conversion.cpp
1>c:\my documents\visual studio 2010\projects\excerciseprogram1\excerciseprogram1\question2.cpp(12): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\my documents\visual studio 2010\projects\excerciseprogram1\excerciseprogram1\question2.cpp(18): warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>c:\my documents\visual studio 2010\projects\excerciseprogram1\excerciseprogram1\question2.cpp(32): error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source file
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.1
I am using VB 2010 as my IDE
The reason why you are experiencing a compile error is mentioned here:
error C2857: '#include' statement specified with the /YcStdAfx.h command-line option was not found in the source
This means that the compiler is making the inclusion of StdAfx.h compulsory. You can remedy this by adding #include <StdAfx.h> to your source code.
It seems that your source file is not including the precompiled header file. All source files must, as first non-comment, include "stdafx.h" if you are using precompiled headers.

structure repetition error compiling C WinSock code in visual studio 2010

I have been trying to compile C source code in visual studio 2010. While compiling i am getting some errors like:
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winnt.h(4277): error C2040: 'CONTEXT' : '_CONTEXT' differs in levels of indirection from 'binding *'
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\winuser.h(5341): error C2365: 'INPUT' : redefinition; previous definition was 'enumerator'
1>c:\documents and settings\xyz\desktop\abc\clause.h(72) : see declaration of 'INPUT'
1>C:\Program Files\Microsoft Visual Studio 10.0\VC\include\sys/time.h(16): error C2011: 'timeval' : 'struct' type redefinition
1>C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\WinSock2.h(176) : see declaration of 'timeval'
I searched for those error and but some hints that it is due to the change in order of the header files. But could not get the exact answer. May I know , How can I overcome this problem?
Following is the order of header file inclusions.
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
Thanks in advance.
See here - if you include windows.h explicitly with winsock2.h you have to add a #define WIN32_LEAN_AND_MEAN to make this work.
For historical reasons, the Windows.h
header defaults to including the
Winsock.h header file for Windows
Sockets 1.1. The declarations in the
Winsock.h header file will conflict
with the declarations in the
Winsock2.h header file required by
Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents the Winsock.h from being included by the Windows.h header.

Compilation error in visual C++

I am using Visual studio 2010 for building C project. My project contains a number of header files,source file and parsers. It uses lex and bason files. I am getting a single error during the compilation and íé the following
abc.y:error C2065: 'INPUT' : undeclared identifier
I tried the solutions I am getting like including
#define WIN32_WINNT >= 0x0501
in my main.c file before the inclusion of any of the header files.I am not able to get rid of this error. Could you please let me know what Can be the reasons for this error?
EDIT
The snippet of code that is showing error is:
list_Cons(0, list_List((POINTER)INPUT)
The surprising thing is that If i alter INPUT into INPUT1, I get the same error. It is stoic to change.
Presumably you read this and this.
#define WIN32_WINNT >= 0x0501 wont work. You should try using #define WIN32_WINNT 0x0501 instead.
Also, check that you are actually #including winuser.h
A C++ compiler cannot process a *.y file. For that you need a yacc / bison program, which does not come included with Visual Studio 2010.
For myself I use CMake which can generate MSVC projects along with other build types. You can tell it that a .y needs to be processed outwith the C/C++ files and it will instruct MSVC to invoke whatever external tools are necessary to preprocess the non-C/C++ parts.

GetCurrentHwProfile in Code::Blocks (C)

I am trying to get the GUID from windows in C in the Code::Blocks IDE with the following code:
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
void getHWID()
{
HW_PROFILE_INFO hwProfileInfo;
if(GetCurrentHwProfile(&hwProfileInfo))
{
printf("Hardware GUID: %s\n", hwProfileInfo.szHwProfileGuid);
printf("Hardware Profile: %s\n", hwProfileInfo.szHwProfileName);
}
}
I keep getting these errors even though I am linking and including all the files required:
In function 'getHWID': warning:
implicit declaration of function
'GetCurrentHwProfile'
In function getHWID': undefined
reference toGetCurrentHwProfile'
||=== Build finished: 1 errors, 1
warnings ===|
Let me know if anyone has had this problem or know how to fix it. Also if I right click HW_PROFILE_INFO or GetCurrentHwProfile and click Find Declaration it says not found.
I would like to get this to work, but I am also open to other simple ways to get this done.
edit: I've included Winbase.h now and it found a declaration for HW_PROFILE_INFO but I still get a undefined reference error for GetCurrentHwProfile
Have you configured Code::Blocks to include the correct SDK (I believe this function is part of the Windows SDK)? I suggest using Microsoft Visual Studio to write Windows code.
EDIT: I'm not sure if this is all you need to do, but there is a section in the their wiki about how to use the Microsoft's compiler.
Change
GetCurrentHwProfile
to
GetCurrentHwProfileA
or add
#ifdef UNICODE
#define GetCurrentHwProfile GetCurrentHwProfileW
#else
#define GetCurrentHwProfile GetCurrentHwProfileA
#endif
have a look GetCurrentHwProfile was not declared in this scope using MinGW's g++ compiler

Resources