0

hlo,

Im using MicroSoft vs-code, with clangd extension, and not the MS's official c/cpp one.

program.c:

...
{
  int result;
  result = (((result = open("/tmp/file.txt", O_RDONLY)) == -1) 
              ? -1
              : close(result));
}
...

In the above portion of my code, vs-code is warning me "Multiple unsequenced modifications to 'result' clang(-Wunsequenced)",

But when I compile the program, with both gcc, and clang, it works as intended, neither giving any error, nor any un-defined behavior,

It is even visible (without compile) that everything is as good, than why is the system complaining,

How may I silence it, any command?,

thanks

Harith
  • 4,663
  • 1
  • 5
  • 20
gitman-2021
  • 111
  • 8
  • 1
    "Undefined Behaviour" means "you can't rely on this program, because absolutely anything can happen, including it working as expected, not working, crashing, only working in correct moon phase and spawning some nasty [nasal demons](http://catb.org/jargon/html/N/nasal-demons.html)". If you rely on UB, that's on you - it may work, but you never have a guarantee that it actually does. – Yksisarvinen Feb 28 '23 at 12:42
  • 1
    Observing a behavior does not determine whether it is undefined. – Drew Dormann Feb 28 '23 at 12:43
  • I acknowledge "Undefined behavior" is undetectable, but, may I ask, where is the code incorrect, or should i ignore the warning – gitman-2021 Feb 28 '23 at 12:46

1 Answers1

3

This is rather an answer to your comment "where is the code incorrect, or should I ignore the warning?"

Don't ignore the warning but write correct code and not contrieved "clever" code like yours's:

Unreadable contrieved code that exposes UB (I really needed to wrap my head around this to figure out what it was supposed to do):

{
  int result;
  result = (((result = open("/tmp/file.txt", O_RDONLY)) == -1) 
              ? -1
              : close(result));
}

Readable correct code:

{
  int result = open("/tmp/file.txt", O_RDONLY);
  if (result != -1)
    result = close(result);
}

BTW: I think this code is supposed to check if the file /tmp/file.txt exists. If you are trying to do this, you might want to read this: What's the best way to check if a file exists in C?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115