Variable size constraints in C - Page 1
Forum section: electronics / beginners

author: WattsUp
author_rank: Regular Contributor
timestamp: January 24, 2015, 06:21:16 pm
content: 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

author: dannyf
author_rank: Super Contributor
timestamp: January 24, 2015, 06:27:09 pm
content: QuoteCould it be t hat C places no such constraint and it is compiler / implementation specific?

author: c4757p
author_rank: Super Contributor
timestamp: January 24, 2015, 06:31:34 pm
content: "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 suggeststdint.h

author: tomizett
author_rank: Newbie
timestamp: January 24, 2015, 06:38:20 pm
content: If you need the definative answer on your compiler, you can do:printf("Type int is %i bytes", sizeof(int)) ;

author: paf
author_rank: Regular Contributor
timestamp: January 24, 2015, 07:07:26 pm
content: 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

author: WattsUp
author_rank: Regular Contributor
timestamp: January 24, 2015, 08:00:54 pm
content: 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

author: c4757p
author_rank: Super Contributor
timestamp: January 24, 2015, 08:05:18 pm
content: Quote from: WattsUp on January 24, 2015, 08:00:54 pmThere is a defined size constraint, it's just notwelldefined. It depends on the platform.As paf said, check limits.h

author: WattsUp
author_rank: Regular Contributor
timestamp: January 24, 2015, 08:28:48 pm
content: OK thank you, I misunderstood what was said.Thanks everyone,Ben

author: Dave Turner
author_rank: Frequent Contributor
timestamp: January 25, 2015, 11:38:08 pm
content: 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!

author: IanB
author_rank: Super Contributor
timestamp: January 25, 2015, 11:43:23 pm
content: Actually, the C standard says that data types like int and long areat leastbig 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.

author: rsjsouza
author_rank: Super Contributor
timestamp: January 26, 2015, 04:41:06 am
content: 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.

author: zapta
author_rank: Super Contributor
timestamp: January 26, 2015, 04:53:49 am
content: Quote from: WattsUp on January 24, 2015, 08:00:54 pmOne 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.

author: rsjsouza
author_rank: Super Contributor
timestamp: January 26, 2015, 12:20:44 pm
content: Quote from: zapta on January 26, 2015, 04:53:49 amzapta, 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_tDespite the "_t" seems somewhat silly (as it is present in all datatypes), over time it becomes second nature.

author: Simon
author_rank: Unknown
timestamp: January 26, 2015, 12:46:27 pm
content: 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

author: IanB
author_rank: Super Contributor
timestamp: January 26, 2015, 03:31:37 pm
content: 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.

author: zapta
author_rank: Super Contributor
timestamp: January 26, 2015, 05:54:11 pm
content: Quote from: rsjsouza on January 26, 2015, 12:20:44 pmYes, 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.