From owner-freebsd-questions@FreeBSD.ORG Thu Dec 2 07:13:01 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2673E16A4CE for ; Thu, 2 Dec 2004 07:13:01 +0000 (GMT) Received: from auk2.snu.ac.kr (auk2.snu.ac.kr [147.46.100.32]) by mx1.FreeBSD.org (Postfix) with ESMTP id BC6C143D46 for ; Thu, 2 Dec 2004 07:13:00 +0000 (GMT) (envelope-from spamrefuse@yahoo.com) Received: from [147.46.44.181] (spamrefuse@yahoo.com) by auk2.snu.ac.kr (Terrace Internet Messaging Server) with ESMTP id 2004120216:12:41:907260.10200.2980740016 for ; Thu, 02 Dec 2004 16:12:41 +0900 (KST) Message-ID: <41AEC075.50207@yahoo.com> Date: Thu, 02 Dec 2004 16:12:53 +0900 From: Rob User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.3) Gecko/20041113 X-Accept-Language: en-us, en MIME-Version: 1.0 To: FreeBSD Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-TERRACE-SPAMMARK: NO (SR:5.85) (by Terrace) Subject: gcc violates const-ness of variable? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Dec 2004 07:13:01 -0000 Hi, This should probably be discussed in the GNU gcc mailinglist. But I'm more familiar here, and I first want to share it here with other FreeBSD users. I'm surprised about this piece of code: #include int main() { const int n = 0; scanf("%d", &n); printf("%d\n", n); return 0; } With gcc compiler, the constant variable 'n' will be overwritten by the scanf statement. With g++ it (silently) is not. So, I get following: $ gcc -W -Wall code.c code.c: In function `main': code.c:5: warning: writing into constant object (arg 2) $ ./a.out 9 9 $ g++ -W -Wall code.c code.c: In function `int main()': code.c:5: warning: writing into constant object (arg 2) $ ./a.out 9 0 Is this a bug in gcc, or a feature? Rob.