tags:

views:

164

answers:

3

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.

+6  A: 

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.

Jon Purdy
would like to use snprintf, can that be used in C?
Tommy
If he wants to ever ship his code, he now has to open it due to GPL linkage. MSVC provides similar functionality, so I think it would be better for him to create a wrapper around both snprintf / sprintf_s and call his wrapper code.
SB
@SB: Good point. It was just an example to get things rolling.
Jon Purdy
+1  A: 

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

SB
+1  A: 

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).

Greg Hewgill
I would strongly recommend *not* aliasing one to the other like that. `_snprintf` does not have standard C `snprintf` semantics (specifically, it does *not* guarantee NUL termination, and it will return -1 instead of the necessary buffer size if given an insufficiently large buffer).
jamesdlin