From: Jakub Jelinek <jakub@redhat.com>
To: esok127@gmail.com
Cc: gcc@gcc.gnu.org, gcc-help@gcc.gnu.org
Subject: Re: gcc 13.2 is missing warnings?
Date: Thu, 19 Oct 2023 13:50:19 +0200 [thread overview]
Message-ID: <ZTEX+m2qZZfD/akb@tucnak> (raw)
In-Reply-To: <CAPiqgzOztjTL+n6j=i4Diux4bydGw4zj7jRXZGpoDs=xzVosEg@mail.gmail.com>
On Thu, Oct 19, 2023 at 07:39:43AM -0400, Eric Sokolowsky via Gcc wrote:
> I am using gcc 13.2 on Fedora 38. Consider the following program.
>
> #include <stdio.h>
> int main(int argc, char **argv)
> {
> printf("Enter a number: ");
> int num = 0;
> scanf("%d", &num);
>
> switch (num)
> {
> case 1:
> int a = num + 3;
> printf("The new number is %d.\n", a);
> break;
> case 2:
> int b = num - 4;
> printf("The new number is %d.\n", b);
> break;
> default:
> int c = num * 3;
> printf("The new number is %d.\n", c);
> break;
> }
> }
>
> I would expect that gcc would complain about the declaration of
> variables (a, b, and c) within the case statements. When I run "gcc
> -Wall t.c" I get no warnings. When I run "g++ -Wall t.c" I get
> warnings and errors as expected. I do get warnings when using MinGW on
> Windows (gcc version 6.3 specifically). Did something change in 13.2?
C isn't C++.
In particular, the above is valid C23, which is why it is accepted as an
extension in older C language versions starting with GCC 11.
It is warned about with -pedantic/-Wpedantic and errored on with
-pedantic-errors/-Werror=pedantic unless -std=c2x or -std=gnu2x is used.
The C++ case is completely different. There labels are allowed before
declarations already in C++98, but it is invalid to cross initialization
of some variable using the jump to case 2 or default labels above.
If you rewrite it as:
case 1:
int a;
a = num + 3;
printf("The new number is %d.\n", a);
break;
case 2:
int b;
b = num - 4;
printf("The new number is %d.\n", b);
break;
default:
int c;
c = num * 3;
printf("The new number is %d.\n", c);
break;
it is valid C++ and it won't be diagnosed.
Note, this should have been posted to gcc-help instead.
Jakub
parent reply other threads:[~2023-10-19 11:55 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <CAPiqgzOztjTL+n6j=i4Diux4bydGw4zj7jRXZGpoDs=xzVosEg@mail.gmail.com>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZTEX+m2qZZfD/akb@tucnak \
--to=jakub@redhat.com \
--cc=esok127@gmail.com \
--cc=gcc-help@gcc.gnu.org \
--cc=gcc@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).