LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2012, 12:42 AM   #1
AnshulMaheshwari
LQ Newbie
 
Registered: Sep 2012
Location: Pune ,India
Distribution: Open Suse 12.1
Posts: 17

Rep: Reputation: 0
Difference between #define and declearing


HI Guys

This is my program source code that give me warning

Code:
#define a 0x8000

main()
{
   short int var = ~a;
}
gcc 4.2 gives me warning
Quote:
overflow in implicit constant conversion [-Woverflow]
There is another code that doesn't give me warning

Code:
int a = 0x8000;
main()
{
   short int var = ~a; 
}
This Compile successsfully
 
Old 09-27-2012, 02:26 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
And what is your question?

In the first case ~a is a constant value (0xffff7fff, type int), in the second it is an expression. The warning you got in the first case refers to conversion of a constant, so it doesn't apply to the second case.

Last edited by NevemTeve; 09-27-2012 at 02:36 AM.
 
Old 09-27-2012, 04:16 AM   #3
AnshulMaheshwari
LQ Newbie
 
Registered: Sep 2012
Location: Pune ,India
Distribution: Open Suse 12.1
Posts: 17

Original Poster
Rep: Reputation: 0
My Question was Why compiler doesn't give warning in second case ??


Code:
const int a = 0x8000;
main()
{
  short int var = ~a; 
}
According to Nevem Teve this code should have given me warning but even this is not giving me warning..

Last edited by AnshulMaheshwari; 09-27-2012 at 04:17 AM. Reason: typo
 
Old 09-27-2012, 05:14 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Let me repeat: the warning is about constant-overflow. There is no such thing in the second case.

Edit: in this case 'constant' doesn't mean "variable with const modifier", it means literal value, or an expression that can be evaluated compile time, eg 1+2, 'Z'-'A', "string", ~0x8000 etc

Last edited by NevemTeve; 09-27-2012 at 05:16 AM.
 
Old 10-01-2012, 05:06 AM   #5
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
If you want to be warned about converting "int" to "short int", you can use "-Wconversion" flag for gcc.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Making a Difference; Selling a Difference LXer Syndicated Linux News 0 09-23-2010 05:00 AM
shell script to find the difference betwwn two file and place the difference to other kittunot4u Linux - General 3 07-19-2010 04:26 AM
Help with a #define Wynd Programming 4 04-22-2007 03:30 PM
define jhon Linux - Networking 2 08-30-2004 07:42 PM
using #define Jo_Nak Programming 4 06-11-2003 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration