http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx
C99 support added visual studio 2013, but I cant use complex.h in my "C" code.
#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex dc1 = 3 + 2 * I;
double complex dc2 = 4 + 5 * I;
double complex result;
result = dc1 + dc2;
printf(" ??? \n", result);
return 0;
}
I get syntax errors.
Edit: Sorry for the missing part.
error C2146: syntax error : missing ';' before identifier 'dc1'
error C2065: 'dc1' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'dc2'
error C2065: 'dc2' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'result'
error C2065: 'result' : undeclared identifier
error C2065: 'result' : undeclared identifier
error C2065: 'dc1' : undeclared identifier
error C2065: 'dc2' : undeclared identifier
error C2065: 'result' : undeclared identifier
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: identifier "result" is undefined
IntelliSense: identifier "dc1" is undefined
IntelliSense: identifier "dc2" is undefined
In case anyone is searching a year later, try
_Dcomplex dc1 = {3.0, 2.0};
for the variable declaration.
From looking inside VS2013's "complex.h" header, it seems that Microsoft decided on their own implementation for C complex numbers. You'll have to implement your own arithmetical operators using the real() and imag() functions, i.e.:
double real_part = real(dc1) + real(dc2);
double imag_part = imag(dc1) + imag(dc2);
_Dcomplex result = {real_part, imag_part};
Another way is to define like:
/*_Fcomplex */ _C_float_complex a = _FCbuild(5.0F, 1.0F);
printf( "z = %.1f% + .1fi\n", crealf(a), cimagf(a));
/*_Dcomplex*/ _C_double_complex b = _Cbuild(3.0, 2.0);
printf("z = %.1f% + .1fi\n",creal(b), cimag(b));
Related
I'm trying to compile some old C code from a decade ago and am getting errors for the sigset_t type. I've researched the issue on the net and here on SO without success. I also tried adding #define _POSIX_C_SOURCE too, but all without success. Maybe something has been deprecated since the code was working and has a new replacement now.
I'm wondering if someone can tell me what to do to fix the issue? Thank you
Here's is what it looks like on my end:
I set my command line environment for compiling 64bit by running the usual batch file (vcvars64.bat)
"c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
I am using this cl.exe compiler:
c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\bin\HostX64\x64\cl.exe
C:\ufo-dev\code>make all
cl /c /Od /nologo /MT /I. "-I..\s" "-Ic:\ufo-dev\include" ..\s\spool.c
spool.c
..\s\spool.c(85): error C2065: 'sigset_t': undeclared identifier
..\s\spool.c(85): error C2146: syntax error: missing ';' before identifier 'set'
..\s\spool.c(85): error C2065: 'set': undeclared identifier
..\s\spool.c(86): error C2065: 'sigset_t': undeclared identifier
..\s\spool.c(86): error C2146: syntax error: missing ';' before identifier 'old'
..\s\spool.c(86): error C2065: 'old': undeclared identifier
..\s\spool.c(298): error C2065: 'set': undeclared identifier
..\s\spool.c(299): error C2065: 'set': undeclared identifier
..\s\spool.c(300): error C2065: 'set': undeclared identifier
..\s\spool.c(300): error C2065: 'SIGHUP': undeclared identifier
..\s\spool.c(301): error C2065: 'set': undeclared identifier
..\s\spool.c(301): error C2065: 'SIGQUIT': undeclared identifier
..\s\spool.c(302): error C2065: 'set': undeclared identifier
..\s\spool.c(303): error C2065: 'SIG_BLOCK': undeclared identifier
..\s\spool.c(303): error C2065: 'set': undeclared identifier
..\s\spool.c(303): error C2065: 'old': undeclared identifier
..\s\spool.c(338): error C2065: 'SIG_UNBLOCK': undeclared identifier
..\s\spool.c(338): error C2065: 'set': undeclared identifier
..\s\spool.c(338): error C2065: 'old': undeclared identifier
make: *** [spool.obj] Error 2
/* typedefs in an include file that is included */
mytypedefs.h:
#define dcl_sigsett(x) sigset_t x
spool.c:
#define _POSIX_SOURCE /* enables POSIX compiler options */
#define _POSIX_C_SOURCE /* enables POSIX compiler options */
#include<mytypedefs.h>
#include <signal.h> /* signals */
spool.c(85):
dcl_sigsett (set); /* set of signals to block */
dcl_sigsett (old); /* set of signals to block */
spool.c(298):
/* : - block signals while rewriting the mailbox! */
sigemptyset (&set);
sigaddset (&set, SIGINT);
sigaddset (&set, SIGHUP);
sigaddset (&set, SIGQUIT);
sigaddset (&set, SIGTERM);
sigprocmask (SIG_BLOCK, &set, &old);
Doesn't look like it was parced correctly even. But it looks fine to me.
Maybe it doesn't like newlines? Tried without them.
Maybe it must be in UPPER CASE? I honestly have no idea.
Maybe it doesn't like ////comments?
#define fill(where_l, where_r, where_t, where_b, what_l, what_r, what_t, what_b) \
\
////lt \
*p++ = where_l; \
*p++ = where_t; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_l; \
*p++ = what_t; \
\
////rt \
*p++ = where_r; \
*p++ = where_t; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_r; \
*p++ = what_t; \
\
////lb \
*p++ = where_l; \
*p++ = where_b; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_l; \
*p++ = what_b; \
\
////rb \
*p++ = where_r; \
*p++ = where_b; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_r; \
*p++ = what_b; \
Errors:
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(570) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(570) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(570) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(571) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(571) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(571) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(572) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(572) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(572) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(573) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(573) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(573) : error C2086: 'int *p' : redefinition
Deleted comments completely, now errors are:
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(569) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(570) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(570) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(570) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(571) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(571) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(571) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(572) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(572) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(572) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(574) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(574) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(574) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(575) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(575) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(575) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(576) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(576) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(576) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(577) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(577) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(577) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(578) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(578) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(578) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(579) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(579) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(579) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(581) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(581) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(581) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(582) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(582) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(582) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(583) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(583) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(583) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(584) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(584) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(584) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(585) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(585) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(585) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(586) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(586) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(586) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(588) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(588) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(588) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(589) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(589) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(589) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(590) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(590) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(590) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(591) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(591) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(591) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(592) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(592) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(592) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(593) : error C2143: syntax error : missing ';' before '++'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(593) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(593) : error C2086: 'int *p' : redefinition
1> c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(568) : see declaration of 'p'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(641) : error C2297: '*' : illegal, right operand has type 'float *'
1>c:\_src\directxsamples\ex_tut2_showjpg\tut2_vertices.cpp(641) : error C2017: illegal escape sequence
It looks like your post is mostly code, please add some more details.
Here is how I use it:
float* p = (float*)malloc(999);
fill(i_im_x, i_im_x + 512.0f,
i_im_y, i_im_y + 512.0f,
0.0f, 1.0f,
0.0f, 1.0f);
You need to do this:
Replace all ////xx comments by /* xx */
Remove the stray space character after the \ in following line:
*p++ = where_l; \
^ stray space here
This was not obvious because you cannot see that stray space unless you put the cursor there.
Remove the final \ in the very last line of the macro:
*p++ = what_b; \
^ remove this
Working example:
#define fill(where_l, where_r, where_t, where_b, what_l, what_r, what_t, what_b) \
\
/*lt*/ \
*p++ = where_l; \
*p++ = where_t; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_l; \
*p++ = what_t; \
\
/* rt */ \
*p++ = where_r; \
*p++ = where_t; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_r; \
*p++ = what_t; \
\
/*lb*/ \
*p++ = where_l; \
*p++ = where_b; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_l; \
*p++ = what_b; \
\
/*rb*/ \
*p++ = where_r; \
*p++ = where_b; \
*p++ = 0.5f; \
*p++ = 1.0f; \
*p++ = what_r; \
*p++ = what_b;
int main(int argc, char **argv) {
float* p = (float*)malloc(999);
fill(1, 2, 3, 4, 5, 6, 7, 8)
}
I am making a program to simulate a game show, randomly. The program picks 1 of three doors for the prize to be hidden behind randomly. The computer picks a door to see if it won or not. This is looped over 10000 times to see how many times I win if I change my pick versus not changing it.
I am getting a bunch of syntax
#include <path-spec>
#include <stdio.h>
#include <time.h>
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed);
void randomize_door(Door* pJackpot);
void pick_door(Door* pPick);
Status check(Door* pPick, Door* pJackpot);
enum status {WIN=1,LOSE};
enum door { FIRST=1, SECOND, THIRD };
typedef enum door Door;
typedef enum status Status;
int main(int argc, char* argv[]){
int i = 0;
srand(time);
Status result;
Status* pResult = &result ;
Door jackpot, pick, last_pick=NULL;
Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick;
int win_unchanged = 0, win_changed=0;
int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed;
while (i < 10000){
last_pick = NULL;
randomize_door(pJackpot);
pick_door(pPick);
result = check(pPick, pJackpot);
count(result, pLast_pick, pPick, pWin_unchanged, pWin_changed);
i++;
}
printf("Wins when changed choice: %d , wins when choice is unchanged: %d", win_changed, win_unchanged);
return 0;
}
void randomize_door(Door* pJackpot){
*pJackpot = rand() % 3 + 1;
}
void pick_door(Door* pPick){
*pPick = rand() % 3 + 1;
}
Status check(Door* pPick, Door* pJackpot){
if (*pPick == *pJackpot){
return WIN;
}
else{
return LOSE;
}
}
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed) {
if (*pLast_pick == *pPick){
if (result == WIN){
*pWin_unchanged++;
}
}
else{
if (result == WIN){
*pWin_changed++;
}
}
*pLast_pick = *pPick;
}
Below are the errors and which lines its happening at. most of them are forgot } or ; which doesn't make any sense in a function header.
1>------ Build started: Project: Program4.1, Configuration: Debug Win32 ------
1> daily4.c
daily4.c(4): error C2146: syntax error : missing ')' before identifier 'result'
daily4.c(4): error C2061: syntax error : identifier 'result'
daily4.c(4): error C2059: syntax error : ';'
daily4.c(4): error C2059: syntax error : ','
daily4.c(4): error C2059: syntax error : ')'
daily4.c(6): error C2143: syntax error : missing ')' before '*'
daily4.c(6): error C2143: syntax error : missing '{' before '*'
daily4.c(6): error C2059: syntax error : ')'
daily4.c(8): error C2143: syntax error : missing ')' before '*'
daily4.c(8): error C2143: syntax error : missing '{' before '*'
daily4.c(8): error C2059: syntax error : ')'
daily4.c(10): error C2061: syntax error : identifier 'check'
daily4.c(10): error C2059: syntax error : ';'
daily4.c(10): error C2143: syntax error : missing ')' before '*'
daily4.c(10): error C2143: syntax error : missing '{' before '*'
daily4.c(10): error C2143: syntax error : missing ';' before '*'
daily4.c(10): error C2059: syntax error : ')'
daily4.c(16): error C2370: 'Door' : redefinition; different storage class
daily4.c(10) : see declaration of 'Door'
daily4.c(21): warning C4013: 'srand' undefined; assuming extern returning int
daily4.c(24): error C2146: syntax error : missing ';' before identifier 'jackpot'
daily4.c(24): error C2065: 'jackpot' : undeclared identifier
daily4.c(24): error C2065: 'pick' : undeclared identifier
daily4.c(24): error C2065: 'last_pick' : undeclared identifier
daily4.c(24): warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
daily4.c(25): error C2297: '*' : illegal, right operand has type 'int *'
daily4.c(25): error C2065: 'jackpot' : undeclared identifier
daily4.c(25): error C2065: 'pick' : undeclared identifier
daily4.c(25): warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
daily4.c(25): error C2065: 'pLast_pick' : undeclared identifier
daily4.c(25): error C2100: illegal indirection
daily4.c(30): error C2065: 'last_pick' : undeclared identifier
daily4.c(30): warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
daily4.c(31): warning C4013: 'randomize_door' undefined; assuming extern returning int
daily4.c(32): warning C4013: 'pick_door' undefined; assuming extern returning int
daily4.c(33): warning C4013: 'check' undefined; assuming extern returning int
daily4.c(34): warning C4013: 'count' undefined; assuming extern returning int
daily4.c(34): error C2065: 'pLast_pick' : undeclared identifier
daily4.c(43): error C2143: syntax error : missing ')' before '*'
daily4.c(43): error C2143: syntax error : missing '{' before '*'
daily4.c(43): error C2059: syntax error : ')'
daily4.c(43): error C2054: expected '(' to follow 'pJackpot'
daily4.c(48): error C2143: syntax error : missing ')' before '*'
daily4.c(48): error C2143: syntax error : missing '{' before '*'
daily4.c(48): error C2059: syntax error : ')'
daily4.c(48): error C2054: expected '(' to follow 'pPick'
daily4.c(53): error C2143: syntax error : missing ')' before '*'
daily4.c(53): error C2143: syntax error : missing '{' before '*'
daily4.c(53): error C2143: syntax error : missing ';' before '*'
daily4.c(53): error C2059: syntax error : ')'
daily4.c(53): error C2054: expected '(' to follow 'pJackpot'
daily4.c(62): error C2143: syntax error : missing ')' before '*'
daily4.c(62): error C2081: 'Door' : name in formal parameter list illegal
daily4.c(62): error C2143: syntax error : missing '{' before '*'
daily4.c(62): error C2143: syntax error : missing ';' before '*'
daily4.c(62): error C2059: syntax error : 'type'
daily4.c(62): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
sample to fix
#include <stdio.h>
#include <time.h>
#include <stdlib.h> //for srand
//enum and typedef defined prior to use
enum status {WIN=1,LOSE};
enum door { NONE, FIRST=1, SECOND, THIRD };//Define NONE instead of NULL
typedef enum door Door;
typedef enum status Status;
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed);
void randomize_door(Door* pJackpot);
void pick_door(Door* pPick);
Status check(Door* pPick, Door* pJackpot);
int main(int argc, char* argv[]){
int i = 0;
Status result;
Status* pResult = &result ;
Door jackpot, pick, last_pick=NONE;
Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick;
int win_unchanged = 0, win_changed=0;
int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed;
srand(time(NULL));//If you use the equivalent of the C89 compiler can not be mixed the declaration statement and execution statements.
...
I made a number guessing game in C that compiles fine with GCC 4.7.1. But it gives a lot of error with VC2010. Mostly syntax error issues. I don't know why it gives these errors as I've compiled many other C sources with VC2010 without these errors.
I compile it with VC (Command Line) with the command: cl guessgame.c
VC gives the following errors:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for
Copyright (C) Microsoft Corporation. All rights reserved.
guessgame.c
guessgame.c(8) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(9) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(10) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(11) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(12) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(13) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(14) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(20) : error C2065: 'chances' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeX' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeY' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'chances' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(25) : error C2065: 'chances' : undeclared identifier
guessgame.c(25) : error C2065: 'i' : undeclared identifier
guessgame.c(26) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'randomNumber' : undeclared identifier
guessgame.c(30) : error C2065: 'chances' : undeclared identifier
guessgame.c(30) : error C2065: 'i' : undeclared identifier
guessgame.c(36) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(39) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(41) : error C2065: 'randomNumber' : undeclared identifier
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int randomNumber = (rand() % 245)+5;
int userAns;
int randRangeX = randomNumber-((rand() % 3)+5);
int randRangeY = randomNumber+((rand() % 3)+5);
int chances = 3;
int wrong_guesses = 0;
int i;
printf("Number guessing game v1.1.\n");
printf("Copyright(c) 2013 - Ahnaf Tahmid.\n");
printf("-----------------------------------\n\n");
printf("Rules: 1) Guess the number.\n");
printf(" 2) You have %d chances.\n\n", chances);
printf("The number is between %d and %d.", randRangeX, randRangeY);
for(i = chances; i >= 1; i--)
{
printf("\nGuess %d: ", (chances-i)+1);
scanf("%d", &userAns);
while(getchar() != '\n');
if(userAns == randomNumber)
{
printf("\nGood guess! You guessed it in %d turn(s).\n", (chances-i)+1);
break;
}
else
{
printf("Aww, bad guess.\n");
wrong_guesses += 1;
}
}
if(wrong_guesses == 3)
{
printf("\nGame Over!\n\nThe answer was %d.\n", randomNumber);
}
getch();
return 0;
}
VC2010 supports C89 which requires that variables be declared at the start of their scope block. You can build with both compilers if you move the srand call after your variable declarations and delay initialising the variables which depend on rand().
int main()
{
int randomNumber;
int userAns;
int randRangeX;
int randRangeY;
int chances = 3;
int wrong_guesses = 0;
int i;
srand(time(NULL));
randomNumber = (rand() % 245)+5;
randRangeX = randomNumber-((rand() % 3)+5);
randRangeY = randomNumber+((rand() % 3)+5);
In C89, all variables have to be declared in the beginning of a block. This has been removed in C99.
But VC2010 doesn't support C99. So you have to put
srand(time(NULL));
after all the declaration of variables.
Reference:
C89 §3.6.2 Compound statement, or block
Syntax
compound-statement:
{ declaration-list opt statement-list opt }
declaration-list:
declaration
declaration-list declaration
statement-list:
statement
statement-list statement
Note that in C89, declaration-list has to be before statement -list in a block.
C99 §6.8.2 Compound statement
Syntax
compound-statement:
{ block-item-list_opt }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
In C99, declaration and statement can be mixed.
An alternative approach to solve the issue pointed out by the other answers would be to open a new scope:
int main()
{
srand(time(NULL));
{
int randomNumber = (rand() % 245)+5;
int userAns;
int randRangeX = randomNumber-((rand() % 3)+5);
int randRangeY = randomNumber+((rand() % 3)+5);
int chances = 3;
int wrong_guesses = 0;
int i;
...
This code is working fine in c++.I need the solution for C.
This is my sample.
FileName:Mail.c
This is the declaration I made in the file mail.c
#ifdef __cplusplus
typedef int (__cdecl *SetIpAddressFun)(char* , int , int );
typedef bool (__cdecl *SendMailFun)(char * , char *);
#endif
I used this function pointers in the function called func() in the same file mail.c
void func()
{
SendMailFun sendMailFuncPtr;
SetIpAddressFun setIpAddressFuncPtr;
}
EDIT
If i remove the #ifdef lines in the file mail.c then it is showing the following error.
error C2143: syntax error : missing ')' before '__cdecl'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(138) : error C2143: syntax error : missing '{' before '__cdecl'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(138) : error C2059: syntax error : ')'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(138) : error C2165: 'left-side modifier' : cannot modify pointers to data
ERROR
List of errors happened while compiling in c.
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1615) : error C2065: 'SendMailFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1615) : error C2146: syntax error : missing ';' before identifier 'sendMailFuncPtr'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1615) : error C2065: 'sendMailFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1616) : error C2065: 'SetIpAddressFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1616) : error C2146: syntax error : missing ';' before identifier 'setIpAddressFuncPtr'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1616) : error C2065: 'setIpAddressFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1625) : error C2065: 'setIpAddressFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1625) : error C2065: 'SetIpAddressFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1625) : error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1626) : error C2065: 'setIpAddressFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1626) : warning C4047: '!=' : 'void *' differs in levels of indirection from 'int'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1628) : warning C4013: 'setIpAddressFuncPtr' undefined; assuming extern returning int
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1630) : error C2065: 'sendMailFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1630) : error C2065: 'SendMailFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1630) : error C2146: syntax error : missing ';' before identifier
This is because when you declared the function
#ifdef __cplusplus
typedef int (__cdecl *SetIpAddressFun)(char* , int , int );
typedef bool (__cdecl *SendMailFun)(char * , char *);
#endif
you do it conditionally only for __cplusplus which is typically not defined when compiling C files
EDIT
I think your second problem is to do with the presence of bool which to my knowledge is not C keyword. Considering replacing it with an int
The #ifdef __cplusplus means that bit of code will only be complied when using a C++ compiler.
It's the bit that defines the function pointer type SendMailFun, so the compiler errors are complaining that SendMailFun and SendMailFun don’t exist.
You could start by removing the lines
#ifdef __cplusplus
and
#endif