Skip to content

Instantly share code, notes, and snippets.

@niklas-ourmachinery
Created April 8, 2019 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niklas-ourmachinery/cd546a651f3bb02b77213d99d0e53406 to your computer and use it in GitHub Desktop.
Save niklas-ourmachinery/cd546a651f3bb02b77213d99d0e53406 to your computer and use it in GitHub Desktop.
Comma operator initialization bug in MSVC compiler
#include <inttypes.h>
#include <stdio.h>
typedef struct
{
uint64_t a;
uint64_t b;
} s_t;
s_t s = {
0x7bd9d3ae0409903aULL,
(0, 0x7bd9d3ae0409903aULL),
};
int main(int argc, char **argv)
{
s_t t = {
0x7bd9d3ae0409903aULL,
(0, 0x7bd9d3ae0409903aULL),
};
printf("s.a = %" PRIx64 "\n", s.a);
printf("s.b = %" PRIx64 "\n", s.b);
printf("t.a = %" PRIx64 "\n", t.a);
printf("t.b = %" PRIx64 "\n", t.b);
}
/*
When compiled as .c this produces the output:
s.a = 7bd9d3ae0409903a
s.b = 409903a
t.a = 7bd9d3ae0409903a
t.b = 7bd9d3ae0409903a
When compiled as .cpp this produces the expected output:
s.a = 7bd9d3ae0409903a
s.b = 7bd9d3ae0409903a
t.a = 7bd9d3ae0409903a
t.b = 7bd9d3ae0409903a
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment