LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-30-2018, 01:30 PM   #1
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Rep: Reputation: 13
need max int when using stdint.h


I am using stdint.h to name integers of a specific size and to let the compiler pick the fastest size for fastest program speed. For example
Code:
uint_fast16_t  x;
uint_fast32_t  y;
INT_MAX may no longer be correct. I looked in limits.h but did not find the name uint_fast_16_t and subsequently the max int value as it might really be a 32 or 64 bit integer.

Is there a DEFINE or macro somewhere that specifies the size of uint_fast16_t at compile time?
If not, then I presume the only I must do something like:

Code:
this_size = sizeof( x );
followed by a couple of IF statements to declare/set a maximum value.
 
Old 04-30-2018, 01:36 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Would this work?

Code:
x = -1;
y = -1;

Last edited by dugan; 04-30-2018 at 01:38 PM.
 
1 members found this post helpful.
Old 04-30-2018, 02:22 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
http://pubs.opengroup.org/onlinepubs...ag_13_48_03_02
 
Old 04-30-2018, 05:02 PM   #4
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 205

Original Poster
Rep: Reputation: 13
NevemTeve: I checked that link but do not understand part of what it is trying to tell me.
Quote:
The macro INTN_C( value) shall expand to an integer constant expression corresponding to the type int_least N _t.
I tried a few variations and the best I got was that it returned the value of what I placed in "value".

However, going to dugan's post, I did a simple test and that is the way to go. When I first through about it, subtracting one from zero in an unsigned int is not legal. But that is not correct thinking. The answer is not mathematically accurate. But it does provide right value for my needs. It is up to the programmer to be aware of the limitations of unsigned ints. Thanks for the kind tip that I overlooked.
 
Old 05-01-2018, 12:46 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That text says there are macros like INT_FAST16_MIN, INT_FAST32_MAX, etc in stdint.h

Edit: On my PC (x86_64):
gcc/clang -m32:
Code:
INT_FAST16_MIN=-2147483648
INT_FAST16_MAX= 2147483647
INT_FAST32_MIN=-2147483648
INT_FAST32_MAX= 2147483647
gcc/clang -m64:
Code:
INT_FAST16_MIN=-9223372036854775808
INT_FAST16_MAX= 9223372036854775807
INT_FAST32_MIN=-9223372036854775808
INT_FAST32_MAX= 9223372036854775807

Last edited by NevemTeve; 05-01-2018 at 04:18 AM.
 
Old 05-01-2018, 08:06 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by bkelly View Post
When I first through about it, subtracting one from zero in an unsigned int is not legal. But that is not correct thinking. The answer is not mathematically accurate.
By the way, the C standard defines unsigned integers to work in 2's complement, so you get the mathetically accurate result modulo 2^n. Signed integers are not defined that way, so if you go out of range, anything can happen.
 
  


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
LKM trouble with compile stdint.h dzaku Programming 2 10-20-2014 10:53 AM
[SOLVED] max int doughyi8u Programming 1 03-21-2013 07:57 PM
Max int size now available? mongrel666 Programming 4 05-02-2011 10:06 PM

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

All times are GMT -5. The time now is 10:57 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