Skip to content

Instantly share code, notes, and snippets.

@Costava
Created March 26, 2021 00:20
Show Gist options
  • Save Costava/d7489fa216a869a3cc8aa690a03cd773 to your computer and use it in GitHub Desktop.
Save Costava/d7489fa216a869a3cc8aa690a03cd773 to your computer and use it in GitHub Desktop.
// Tested with gcc 10.2.0 and -std=c99
// Author: github.com/Costava
int main(void) {
// gcc will NOT warn about this line from -Wall or -Wextra
// -Wsign-conversion is needed:
/// warning: unsigned conversion from ‘int’ to ‘unsigned int’ changes
// value from ‘-1’ to ‘4294967295’ [-Wsign-conversion]
const unsigned int foo0 = -1;
// Even with -Wall, -Wextra, and -Wsign-conversion,
// this line does not cause a warning
const unsigned int foo1 = -1u;
// Info on integer constant suffixes:
// https://en.cppreference.com/w/c/language/integer_constant
const unsigned int bar = foo0 + foo1 + 5;
// Even though bar is known at compile time to be in range of int type,
// -Wsign-conversion still warns for this line:
// warning: conversion to ‘int’ from ‘unsigned int’ may change the sign of
// the result [-Wsign-conversion]
return bar;// Returns 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment