Author Topic: Variable size constraints in C  (Read 4659 times)

0 Members and 1 Guest are viewing this topic.

Offline WattsUpTopic starter

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
Variable size constraints in C
« on: January 24, 2015, 06:21:16 pm »
Hi everyone,
  Is anyone able to inform me of the size constraints for some of the basic variable types in C. Also, I would appreciate any information on volatile variables, another type I know very little about. I have googled around, as well as searched on this forum, but I haven't managed to turn up anything useful. Thanks,
Ben
I'm trying out something new. Perspective Reviews. Comments and suggestions are appreciated over PM.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Variable size constraints in C
« Reply #1 on: January 24, 2015, 06:27:09 pm »
Quote
I haven't managed to turn up anything useful.

Could it be t hat C places no such constraint and it is compiler / implementation specific?
================================
https://dannyelectronics.wordpress.com/
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Variable size constraints in C
« Reply #2 on: January 24, 2015, 06:31:34 pm »
"volatile": the compiler assumes that at any given point in time, the variable could have changed, whether or not it has a reason to believe it has. If you read from the variable twice, or write to the variable twice, the compiler will always actually execute two individual reads or writes, rather than trusting that the value it already has is good.

Used for variables that are shared between threads (where thread B could have modified it in between two reads by thread A), variables that are shared with an interrupt handler (same thing, but replace "thread B" with "interrupt handler"), and variables that represent memory-mapped hardware in a microcontroller (since the variable isn't actually a memory location, there's no guarantee it will behave like memory).

And yeah, the sizes of the variable types is not well defined. Depends on the implementation. If you need reliable integer types, I suggest stdint.h
« Last Edit: January 24, 2015, 06:34:12 pm by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline tomizett

  • Newbie
  • Posts: 9
Re: Variable size constraints in C
« Reply #3 on: January 24, 2015, 06:38:20 pm »
If you need the definative answer on your compiler, you can do:
printf("Type int is %i bytes", sizeof(int)) ;
 

Offline paf

  • Regular Contributor
  • *
  • Posts: 91
Re: Variable size constraints in C
« Reply #4 on: January 24, 2015, 07:07:26 pm »
All the variable size information is in the  limits.h header file. 

More info at:
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
 

Offline WattsUpTopic starter

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
Re: Variable size constraints in C
« Reply #5 on: January 24, 2015, 08:00:54 pm »
Thanks for all of your replies, they have been very helpful.Can you tell me why you use different variables if there is no defined size constraint? Thanks again,
Ben
I'm trying out something new. Perspective Reviews. Comments and suggestions are appreciated over PM.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Variable size constraints in C
« Reply #6 on: January 24, 2015, 08:05:18 pm »
Thanks for all of your replies, they have been very helpful.Can you tell me why you use different variables if there is no defined size constraint? Thanks again,
Ben

There is a defined size constraint, it's just not well defined. It depends on the platform.

As paf said, check limits.h
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline WattsUpTopic starter

  • Regular Contributor
  • *
  • Posts: 59
  • Country: gb
Re: Variable size constraints in C
« Reply #7 on: January 24, 2015, 08:28:48 pm »
OK thank you, I misunderstood what was said.
Thanks everyone,
Ben
I'm trying out something new. Perspective Reviews. Comments and suggestions are appreciated over PM.
 

Offline Dave Turner

  • Frequent Contributor
  • **
  • Posts: 447
  • Country: gb
Re: Variable size constraints in C
« Reply #8 on: January 25, 2015, 11:38:08 pm »
History - C has been around for a long time. I originally learnt C in the 70's. byte, int and long (if my memory serves correctly) were known 'invariable' quantities. (Yeah not for long). Systems and manufacturing methods improved rapidly 16, 32, 64 bit words etc. Hence the expanded C definitions which include uint etc explicit definitions.

Portability rules - at least in theory!
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11885
  • Country: us
Re: Variable size constraints in C
« Reply #9 on: January 25, 2015, 11:43:23 pm »
Actually, the C standard says that data types like int and long are at least big enough to hold a certain range of values. Some implementations may make them bigger (for efficiency, say), but they are not allowed to make them smaller.
 

Offline rsjsouza

  • Super Contributor
  • ***
  • Posts: 5986
  • Country: us
  • Eternally curious
    • Vbe - vídeo blog eletrônico
Re: Variable size constraints in C
« Reply #10 on: January 26, 2015, 04:41:06 am »
Also, don't fall for a well known fallacy that a char is always 8 bits. It is the smallest addressable unit of the processor. I've seen architectures with 13 bits and some modern DSPs are 16 bits.
Vbe - vídeo blog eletrônico http://videos.vbeletronico.com

Oh, the "whys" of the datasheets... The information is there not to be an axiomatic truth, but instead each speck of data must be slowly inhaled while carefully performing a deep search inside oneself to find the true metaphysical sense...
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us
Re: Variable size constraints in C
« Reply #11 on: January 26, 2015, 04:53:49 am »
Thanks for all of your replies, they have been very helpful.Can you tell me why you use different variables if there is no defined size constraint? Thanks again,
Ben

One common practice is to define variables of fixed size. For example a int32 that is a signed integer and is always of size 32 bits, or uint8 which is an unsigned 'byte'.

This is done using preprocessor conditions that select a definition for each of the supported platforms and compilers.


 

Offline rsjsouza

  • Super Contributor
  • ***
  • Posts: 5986
  • Country: us
  • Eternally curious
    • Vbe - vídeo blog eletrônico
Re: Variable size constraints in C
« Reply #12 on: January 26, 2015, 12:20:44 pm »
One common practice is to define variables of fixed size. For example a int32 that is a signed integer and is always of size 32 bits, or uint8 which is an unsigned 'byte'.

zapta, typically these definitions are already done in the file c4757p mentioned: the <stdint.h>. The only difference is they are named differently than what you mentioned: for example, a 16-bit signed integer is int16_t or a 32-bit unsigned integer is uint32_t

Despite the "_t" seems somewhat silly (as it is present in all datatypes), over time it becomes second nature.
Vbe - vídeo blog eletrônico http://videos.vbeletronico.com

Oh, the "whys" of the datasheets... The information is there not to be an axiomatic truth, but instead each speck of data must be slowly inhaled while carefully performing a deep search inside oneself to find the true metaphysical sense...
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17815
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Variable size constraints in C
« Reply #13 on: January 26, 2015, 12:46:27 pm »
Because of the variety of processors that now run C from 8 bit to 128 bit and beyond the variables sizes are defined by the compiler for your environment and often the conventional names have been ditched, for example i soon learnt with AVR's and atmel studio to use uint16_t rather than "uint" because on that platform and int can be 8, 16, 32, 64........ bits
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11885
  • Country: us
Re: Variable size constraints in C
« Reply #14 on: January 26, 2015, 03:31:37 pm »
The size of an int in a standard conforming environment will be at least 16 bits, but it may be 32 bits or even 64 bits if that size is more natural for the hardware. If you force the use of int16_t on 32 bit hardware you may slow your program down. So you should not routinely avoid using the ordinary type declarations in favor of specific length types. Use the ordinary declarations for general computation where they are big enough, and use things like int16_t only where you have specific requirements on memory layout.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us
Re: Variable size constraints in C
« Reply #15 on: January 26, 2015, 05:54:11 pm »
One common practice is to define variables of fixed size. For example a int32 that is a signed integer and is always of size 32 bits, or uint8 which is an unsigned 'byte'.

zapta, typically these definitions are already done in the file c4757p mentioned: the <stdint.h>. The only difference is they are named differently than what you mentioned: for example, a 16-bit signed integer is int16_t or a 32-bit unsigned integer is uint32_t

Despite the "_t" seems somewhat silly (as it is present in all datatypes), over time it becomes second nature.

Yes, you are right, I see that they became standard in C99. I typically alias them without the _t suffix.  I also use the generic 'int' of obviously small numbers.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf