Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-06-2012, 12:35 PM   #1
Registered: Jul 2005
Location: Montreal Canada
Distribution: Fedora 26 (Russian RFRemix DVD version)
Posts: 291
Blog Entries: 1

Rep: Reputation: 58
Determining if a system is 32bit or 64bit via C language.

I need to do something like this

If (sizeof(long) == 8)
typedef unsigned int uintxx;
typedef unsigned long uintxx;

The problem is that a typedef may not be part of a if/else expression.

Is there a defined compiler library constant that I may use for linux gcc compiler.

Is there a preprocessor variable I could test?

I found this, which does not work with gcc

#if (!(defined __LP64__ || defined __LLP64__)||( defined _WIN32 && !defined _WIN64 ))
// we are compiling for a 32-bit system
typedef unsigned long uintxx;
// we are compiling for a 64-bit system
typedef unsigned int uintxx;
Old 09-06-2012, 05:32 PM   #2
Senior Member
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
The problem is that a typedef may not be part of a if/else expression.
Yes, because types must be fully defined during compilation, without that compiler doesn't known how many memory should reserve for variable, and "if" statement is executing when program is already compiled and running. Anyway, determining architecture by size of long type is not a reliable way, this size is dependend on compiler and can change in the future. Better method is using predefined macros, in gcc you have that
#ifdef __x86_64
  // 64bit
#elif defined __i386
  // 32bit
  // Unknown
Also if you need an exactly bit length variable, you can use types created for that, there are int64_t, int32_t, int16_t or int8_t, with specified bit length - they are defined in <stdint.h>

Last edited by eSelix; 09-06-2012 at 05:33 PM.
1 members found this post helpful.
Old 09-06-2012, 06:23 PM   #3
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,505
Blog Entries: 4

Rep: Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952
The "#ifdef" approach is the only one that will actually work, because it determines at compile time which block of source-code will actually be submitted to the compiler.

However, "don't make work for yourself." Every language system out there has "platform independence" capabilities and there's a lot of very detailed documentation about both the relevant issues and their "don't ask why, just do this..." solutions. There's also a boat-load of existing open source software out there which shows you, for your particular environment whatever-it-is, exactly how it's correctly done.

"Actum Ne Agas ... Do Not Do A Thing Already Done."

Last edited by sundialsvcs; 09-06-2012 at 06:24 PM.
Old 09-07-2012, 07:36 AM   #4
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,274

Rep: Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994Reputation: 994
Note: For a start, what is 32 or 64 bit? Size of int? or that of long / long long / ptr?

Different possibilities are possible, eg:

short=int=ptr=16 long=32 (code: SIP16-L32 or simply L32, eg: MS-DOS 'small' modell)
short=int=16 long=ptr=32 (code: SI16-LP32 or simply LP32, eg: MS-DOS 'large' modell)
short=16, int=long=ptr=32 (code: S16-ILP32 or simply ILP32, eg: Unix32, Windows32 (older versions without any 64-bit type))
short=16, int=long=ptr=32, long long=64 (code: S16-ILP32-LL64 or simply LL64, eg: Unix32, Windows32)
short=16, int=32, long=long long=ptr=64 (code: S16-I32-LP64 or simply LP64, eg: Unix64)
short=16, int=long=32, long long=ptr=64 (code: S16-I32-LP64 or simply LLP64, eg: Windows64)

conclusion: if you want fixed-size integers, use intNN_t (uintNN_t) from stdint.h/inttypes.h, and if you want a 'pointer compatible' integer, use predefined types like 'ptrdiff_t', 'size_t', 'ssize_t', 'intptr_t', 'uintptr_t'


gcc, preprocessor variable, sizeof

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
upgrading a 32bit hardware system to 64bit frankkky Linux - Hardware 7 10-03-2010 11:12 AM
Determining 32bit vs 64bit o/s gammy Linux - Software 2 09-13-2007 03:11 PM
building 32bit app on 64bit system nori Linux - Software 1 07-20-2007 01:10 PM
Running LimeWire 32bit on a 64bit System? BiPolarPenguin Linux - Software 9 12-07-2006 12:07 PM
how do i know if system is 32bit or 64bit? greythorne SUSE / openSUSE 5 09-15-2005 08:56 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 PM.

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