Skip to content

Instantly share code, notes, and snippets.

@morido
Last active March 4, 2022 07:51
Show Gist options
  • Save morido/8637535 to your computer and use it in GitHub Desktop.
Save morido/8637535 to your computer and use it in GitHub Desktop.
Example of weakness of -Werror=return-type in gcc
#include <stdio.h>
#include <stdbool.h>
bool f(char achar) {
switch (achar)
{
case 'q':
return true;
break;
case 'w':
return false;
break;
}
//We shouldn't be here
}
int main() {
printf("Enter a character\n");
char input = getchar();
/*
if ( !( input == 'q' || input == 'w') ) {
input = 'w';
}
*/
bool output = f(input);
if ( output )
{
printf("You entered q\n");
}
else
{
printf("You entered w\n");
}
return 0;
}
@mcepl
Copy link

mcepl commented Mar 3, 2022

So, what happens when the user enters d?

@morido
Copy link
Author

morido commented Mar 4, 2022

So, what happens when the user enters d?

Well, undefined behaviour as per my comment from 2014.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment