inttypes.h header issue - c

nb: This question has been reduced many times, due to comments.
Below now is presented minimum amount of code which generated the error. inttypes.h file was downloaded from here: ffMPEG "inttypes.h not found" error), which was thought to be the issue in the beginning.
//tlvlist.c
static int32_t test(somestruct *a);
/* Private method, adds tlv object to the list which contains raw binary data. */
int32_t int32_t test(somestruct *a)
{
/* Some checks */
if(a == NULL || bytes == NULL)
return -1;
/* Check if list is full */
if(a->used == MAX_LIST_SIZE)
return -1;
/* Index to first free element in the list */
int iIndex = a->used;
// ...
return 0;
}
errors:
tlvlist.c
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(21): error C2143: syntax error : missing ';' before 'type'
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(23): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(24): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(28): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(29): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(32): error C2065: 'iIndex' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Evidently, when compiling a C file in MSVS you need to have all variable declarations at the start of the function before any statement. For example:
int32_t Tlvlist_AddRawt(Tlvlist *a, uint8_t type, uint16_t size, const void *bytes)
{
/* Index to first free element in the list */
int iIndex;
/* Some checks */
if(a == NULL || bytes == NULL)
return -1;
iIndex = a->used;
...
}
I believe this is old C89 format and most C compilers now use C99 (or greater) which would permit variable declarations anywhere in the function. Renaming the file as CPP is another option for MSVS without moving the variable declarations to the top of a function, though it may raise other issues in the code.

Related

How can i use alignas in VisualStudio in C

I am doing my project in C and faced the problem of using alignas in VisualStudio
If we believe this example, then I just have to write alignas(16) const uint16_t _decode_table[] = {...}; But errors appear during compilation https://ibb.co/02w8crP
static unsigned table_walk(unsigned cur_idx, unsigned entry_idx, unsigned* out_kind) {
alignas(16) const uint16_t _decode_table[] = {
#define FD_DECODE_TABLE_DATA
#include <fadec-decode-private.inc>
#undef FD_DECODE_TABLE_DATA
};
unsigned entry = _decode_table[cur_idx + entry_idx];
*out_kind = entry & ENTRY_MASK;
return (entry & ~ENTRY_MASK) >> 1;
}
I get this error
error C2143: syntax error: missing ';' before 'const'
error C2065: '_decode_table': undeclared identifier
error C2109: subscript requires array or pointer type
So this is how I can use alignas if I get an error when compiling in Visual Studio?.

sigset_t with 64bit target using microsoft cl.exe from visual studio 2019

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

Compiling C code in Visual Studio 2013 with complex.h library

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

Code compiles fine on GCC but loads of error on VC2010

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

Using winsock for socket programming in c

I am writing a program in C that uses winsock and I am using the command fcntl to make the receive call nonblocking and I am getting the following errors.
warning C4013: 'fcntl' undefined; assuming extern returning int
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'F_GETFL' : undeclared identifier
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'O_NDELAY' : undeclared identifier
error C2065: 'EWOULDBLOCK' : undeclared identifierenter code here
I am including the winsock2.h header file in my code as follows
#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
Please help me out.
Thanks in advance.
I think on Windows you need to use ioctlsocket rather than fcntl().
To make non-blocking:
unsigned long on = 1;
if (0 != ioctlsocket(socket_fd, FIONBIO, &on))
{
/* Handle failure. */
}
To make blocking:
unsigned long off = 0;
if (0 != ioctlsocket(socket_fd, FIONBIO, &off))
{
/* Handle failure. */
}
Instead of EWOULDBLOCK use WSAEWOULDBLOCK.

Resources