LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-12-2010, 01:54 AM   #1
ishandutta2007
LQ Newbie
 
Registered: May 2009
Location: kolkata,india
Distribution: fedora
Posts: 6

Rep: Reputation: 0
Question Is not 'long long' of 8 bytes in gcc?


Both 'long long' and 'double' are 64 bit data types in gcc but
why can't I insert a value greater than 2^31-1 (ie 32bit)
in a long long type variable.



secondly in the following code,

long long v=1000;
while(v++>=0){
printf("\n%lld",a[0]);
}

the program continues to print negative value of v
even after 2^31-1.Why?
 
Old 01-12-2010, 02:12 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ishandutta2007 View Post
Both 'long long' and 'double' are 64 bit data types in gcc but
why can't I insert a value greater than 2^31-1 (ie 32bit)
in a long long type variable.



secondly in the following code,

long long v=1000;
while(v++>=0){
printf("\n%lld",a[0]);
}

the program continues to print negative value of v
even after 2^31-1.Why?
Where did you describe 'a' ?

Publish your full source code using CODE tags and publish gcc output when you compile your code like

Code:
gcc -Wall -Wextra your_source.c
 
0 members found this post helpful.
Old 01-12-2010, 03:26 AM   #3
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
`long long int` was introduced by ISO 9899/1999, pass "-std=c99" to gcc.
 
Old 01-12-2010, 08:28 AM   #4
ishandutta2007
LQ Newbie
 
Registered: May 2009
Location: kolkata,india
Distribution: fedora
Posts: 6

Original Poster
Rep: Reputation: 0
correction:

Both 'long long' and 'double' are 64 bit data types in gcc but
why can't I insert a value greater than 2^31-1 (ie 32bit)
in a long long type variable.



secondly in the following code,
Code:
long long v=1000;
while(v++>=0){
printf("\n%lld",v);
}
the program continues to print negative value of v
even after 2^31-1.Why?

Last edited by ishandutta2007; 01-12-2010 at 08:31 AM.
 
Old 01-12-2010, 09:34 AM   #5
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by carbonfiber View Post
`long long int` was introduced by ISO 9899/1999, pass "-std=c99" to gcc.
long long int is also supported by the GNU extensions (gnu89 & gnu99) whereof gnu89 is used by default IF no -std= option is given on the command-line. But yes, -std=c99 should be safe


I can't reproduce this error, neither on a 32-bit nor a 64-bit system. Some thoughts:

* maybe you are using an old C library
* maybe you are using an old & buggy compiler
 
Old 01-12-2010, 11:52 AM   #6
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Quote:
Originally Posted by ForzaItalia2006 View Post
long long int is also supported by the GNU extensions (gnu89 & gnu99)
Thank you for the information. I suppose I really should read the GCC manual one of these days.

I don't really understand what the OP means by "can't insert a value greater than 2^31-1..". How are you attempting to do this and what makes you declare that you failed?

Last edited by carbonfiber; 01-12-2010 at 11:54 AM.
 
Old 01-12-2010, 12:17 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
BTW when you are trying to get a variable of the exact bit size, use the stdint types instead.
 
Old 01-12-2010, 07:49 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Using the following code:
Code:
#include <stdio.h>
int main()
{
   long long v=9223372036854775800LL;
   while(v++>=0)
   {
      printf("\n%lld",v);
   }
   return 0;
}
I get (as I would expect) the following output
Code:
9223372036854775801
9223372036854775802
9223372036854775803
9223372036854775804
9223372036854775805
9223372036854775806
9223372036854775807
-9223372036854775808
The last number is negative because the comparison is done before the increment. If you want no negative number to be displayed then change the condition of your while loop to:

Code:
while(++v>=0)
 
Old 01-12-2010, 07:57 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,140
Blog Entries: 4

Rep: Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227Reputation: 3227
Lightbulb

As a rule of thumb, don't assume that a particular data type is of a particular size. gcc runs on multiple dozens of hardware platforms, and it must make an "appropriate" decision for each.

There are standard data-type definitions available which stipulate that the variable will be of "at least" a certain size and/or numeric capacity. If your application needs to make such assertions, it should use one of those types. But if it does not, then it should leave all such matters to the discretion of the compiler.
 
Old 01-12-2010, 08:20 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
For GCC the long long int data type is assumed to be at least 64bits in length. I can't talk for other compilers.
 
Old 01-12-2010, 08:24 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by ishandutta2007 View Post
why can't I insert a value greater than 2^31-1 (ie 32bit)
in a long long type variable.
By this did you mean that you are having difficulty assigning a literal greater than 32bits? if so see my code (post 8) and notice the suffix LL which is essential to add numbers greater than 32 bits. Refer to the link I provided (post 10) from the gcc documentation on the long long data type.
 
1 members found this post helpful.
Old 01-12-2010, 09:51 PM   #12
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by graemef View Post
For GCC the long long int data type is assumed to be at least 64bits in length. I can't talk for other compilers.
The C99 standard requires that "long long" be at least 64 bits.
 
Old 01-12-2010, 11:00 PM   #13
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
ishandutta2007 -

I don't know if you caught what graemef said, but he probably hit the nail right on the head:
Quote:
By this did you mean that you are having difficulty assigning a literal greater than 32bits? if so see my code (post 8) and notice the suffix LL which is essential to add numbers greater than 32 bits. Refer to the link I provided (post 10) from the gcc documentation on the long long data type.
Every time you use a long long in an expression with an "int", or with a literal that doesn't explicitly use the "LL" suffix, you risk the entire expression being DOWNCAST to 32-bits.

I'm pretty sure that if you review all your code, you'll confirm that's what's happening.

IMHO .. PSM
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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: This week at LWN: Long discussions about long names LXer Syndicated Linux News 0 05-19-2009 10:50 PM
Unable to use long long number types? Aziz Programming 4 03-28-2009 05:22 PM
Long standing USB problem with XP & Linux...long cbjhawks Linux - Hardware 4 02-23-2009 08:32 AM
printf unsigned long long int? blackzone Programming 9 03-04-2008 12:41 PM
long filename & long directory tree slack66 Slackware 1 09-20-2006 09:56 AM

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

All times are GMT -5. The time now is 08:55 PM.

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