How to use types in embedded C for portable bare metal software [closed]

Multi tool use
I'm writing my first bare metal application in C for a specific micro controller. I want to make it as much portable as possible and I'm not sure what is the proper way for using the C types. I have two options in mind:
- Using the typical C standard types such as
char
,int
,long
etc. - Using the types of
stdint.h
for integer types, such asuint8_t
,int32_t
etc., andchar
anddouble
for the rest of the types.
Which option is the best one?
c types embedded bare-metal hardware-programming
closed as primarily opinion-based by old_timer, TylerH, lagom, Billal Begueradj, Lundin Nov 19 '18 at 15:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
I'm writing my first bare metal application in C for a specific micro controller. I want to make it as much portable as possible and I'm not sure what is the proper way for using the C types. I have two options in mind:
- Using the typical C standard types such as
char
,int
,long
etc. - Using the types of
stdint.h
for integer types, such asuint8_t
,int32_t
etc., andchar
anddouble
for the rest of the types.
Which option is the best one?
c types embedded bare-metal hardware-programming
closed as primarily opinion-based by old_timer, TylerH, lagom, Billal Begueradj, Lundin Nov 19 '18 at 15:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
what is the third option?
– Pete Kirkham
Nov 14 '18 at 10:58
2
uint32_t vs int as a convention for everyday programming may apply here.
– chux
Nov 14 '18 at 11:01
1
If you want to write portable code (or any C code really), make sure you understand the misfeature of C that is the usual arithmetic conversions and its integer promotion rules. They are as important as types themselves.
– user694733
Nov 14 '18 at 11:26
1
Possible dupe: Reasons to use (or not) stdint
– Lundin
Nov 14 '18 at 11:30
stdint.h simply translates into the native/actual C types char int long. within a tool for a specific target, the change is rare. but if porting to another platform (of which much if your code wont port anyway) the stdint approach is good. stdint approach has the problem of a C library which kinda doesnt work with baremetal and depending on the tool can be a PITA (also the problem of how does the C library insure it works with that compiler (sometimes it doesnt)).
– old_timer
Nov 15 '18 at 15:44
|
show 2 more comments
I'm writing my first bare metal application in C for a specific micro controller. I want to make it as much portable as possible and I'm not sure what is the proper way for using the C types. I have two options in mind:
- Using the typical C standard types such as
char
,int
,long
etc. - Using the types of
stdint.h
for integer types, such asuint8_t
,int32_t
etc., andchar
anddouble
for the rest of the types.
Which option is the best one?
c types embedded bare-metal hardware-programming
I'm writing my first bare metal application in C for a specific micro controller. I want to make it as much portable as possible and I'm not sure what is the proper way for using the C types. I have two options in mind:
- Using the typical C standard types such as
char
,int
,long
etc. - Using the types of
stdint.h
for integer types, such asuint8_t
,int32_t
etc., andchar
anddouble
for the rest of the types.
Which option is the best one?
c types embedded bare-metal hardware-programming
c types embedded bare-metal hardware-programming
edited Nov 14 '18 at 12:14
Dan
asked Nov 14 '18 at 10:52


DanDan
702621
702621
closed as primarily opinion-based by old_timer, TylerH, lagom, Billal Begueradj, Lundin Nov 19 '18 at 15:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as primarily opinion-based by old_timer, TylerH, lagom, Billal Begueradj, Lundin Nov 19 '18 at 15:49
Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.
what is the third option?
– Pete Kirkham
Nov 14 '18 at 10:58
2
uint32_t vs int as a convention for everyday programming may apply here.
– chux
Nov 14 '18 at 11:01
1
If you want to write portable code (or any C code really), make sure you understand the misfeature of C that is the usual arithmetic conversions and its integer promotion rules. They are as important as types themselves.
– user694733
Nov 14 '18 at 11:26
1
Possible dupe: Reasons to use (or not) stdint
– Lundin
Nov 14 '18 at 11:30
stdint.h simply translates into the native/actual C types char int long. within a tool for a specific target, the change is rare. but if porting to another platform (of which much if your code wont port anyway) the stdint approach is good. stdint approach has the problem of a C library which kinda doesnt work with baremetal and depending on the tool can be a PITA (also the problem of how does the C library insure it works with that compiler (sometimes it doesnt)).
– old_timer
Nov 15 '18 at 15:44
|
show 2 more comments
what is the third option?
– Pete Kirkham
Nov 14 '18 at 10:58
2
uint32_t vs int as a convention for everyday programming may apply here.
– chux
Nov 14 '18 at 11:01
1
If you want to write portable code (or any C code really), make sure you understand the misfeature of C that is the usual arithmetic conversions and its integer promotion rules. They are as important as types themselves.
– user694733
Nov 14 '18 at 11:26
1
Possible dupe: Reasons to use (or not) stdint
– Lundin
Nov 14 '18 at 11:30
stdint.h simply translates into the native/actual C types char int long. within a tool for a specific target, the change is rare. but if porting to another platform (of which much if your code wont port anyway) the stdint approach is good. stdint approach has the problem of a C library which kinda doesnt work with baremetal and depending on the tool can be a PITA (also the problem of how does the C library insure it works with that compiler (sometimes it doesnt)).
– old_timer
Nov 15 '18 at 15:44
what is the third option?
– Pete Kirkham
Nov 14 '18 at 10:58
what is the third option?
– Pete Kirkham
Nov 14 '18 at 10:58
2
2
uint32_t vs int as a convention for everyday programming may apply here.
– chux
Nov 14 '18 at 11:01
uint32_t vs int as a convention for everyday programming may apply here.
– chux
Nov 14 '18 at 11:01
1
1
If you want to write portable code (or any C code really), make sure you understand the misfeature of C that is the usual arithmetic conversions and its integer promotion rules. They are as important as types themselves.
– user694733
Nov 14 '18 at 11:26
If you want to write portable code (or any C code really), make sure you understand the misfeature of C that is the usual arithmetic conversions and its integer promotion rules. They are as important as types themselves.
– user694733
Nov 14 '18 at 11:26
1
1
Possible dupe: Reasons to use (or not) stdint
– Lundin
Nov 14 '18 at 11:30
Possible dupe: Reasons to use (or not) stdint
– Lundin
Nov 14 '18 at 11:30
stdint.h simply translates into the native/actual C types char int long. within a tool for a specific target, the change is rare. but if porting to another platform (of which much if your code wont port anyway) the stdint approach is good. stdint approach has the problem of a C library which kinda doesnt work with baremetal and depending on the tool can be a PITA (also the problem of how does the C library insure it works with that compiler (sometimes it doesnt)).
– old_timer
Nov 15 '18 at 15:44
stdint.h simply translates into the native/actual C types char int long. within a tool for a specific target, the change is rare. but if porting to another platform (of which much if your code wont port anyway) the stdint approach is good. stdint approach has the problem of a C library which kinda doesnt work with baremetal and depending on the tool can be a PITA (also the problem of how does the C library insure it works with that compiler (sometimes it doesnt)).
– old_timer
Nov 15 '18 at 15:44
|
show 2 more comments
1 Answer
1
active
oldest
votes
Using the well defined integer types of stdint.h
makes it much easier to port your code to another controller.
for example:
the size of type int
depends on your machine (16 or 32 bit)
And please remember:
If you wat to work with the MISRA standard, you are not allowed to use the standard types like int
.
Mike, I would add that it is only for integer types that must be of a well-defined width. For normal integer calculations, the normal int could probably be used so you can take benefit of a controler with a larger native int type.
– Paul Ogilvie
Nov 14 '18 at 11:38
@Paul Ogilivie of course, yes!
– Mike
Nov 14 '18 at 11:40
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using the well defined integer types of stdint.h
makes it much easier to port your code to another controller.
for example:
the size of type int
depends on your machine (16 or 32 bit)
And please remember:
If you wat to work with the MISRA standard, you are not allowed to use the standard types like int
.
Mike, I would add that it is only for integer types that must be of a well-defined width. For normal integer calculations, the normal int could probably be used so you can take benefit of a controler with a larger native int type.
– Paul Ogilvie
Nov 14 '18 at 11:38
@Paul Ogilivie of course, yes!
– Mike
Nov 14 '18 at 11:40
add a comment |
Using the well defined integer types of stdint.h
makes it much easier to port your code to another controller.
for example:
the size of type int
depends on your machine (16 or 32 bit)
And please remember:
If you wat to work with the MISRA standard, you are not allowed to use the standard types like int
.
Mike, I would add that it is only for integer types that must be of a well-defined width. For normal integer calculations, the normal int could probably be used so you can take benefit of a controler with a larger native int type.
– Paul Ogilvie
Nov 14 '18 at 11:38
@Paul Ogilivie of course, yes!
– Mike
Nov 14 '18 at 11:40
add a comment |
Using the well defined integer types of stdint.h
makes it much easier to port your code to another controller.
for example:
the size of type int
depends on your machine (16 or 32 bit)
And please remember:
If you wat to work with the MISRA standard, you are not allowed to use the standard types like int
.
Using the well defined integer types of stdint.h
makes it much easier to port your code to another controller.
for example:
the size of type int
depends on your machine (16 or 32 bit)
And please remember:
If you wat to work with the MISRA standard, you are not allowed to use the standard types like int
.
edited Nov 14 '18 at 11:53
answered Nov 14 '18 at 11:18


MikeMike
2,0581725
2,0581725
Mike, I would add that it is only for integer types that must be of a well-defined width. For normal integer calculations, the normal int could probably be used so you can take benefit of a controler with a larger native int type.
– Paul Ogilvie
Nov 14 '18 at 11:38
@Paul Ogilivie of course, yes!
– Mike
Nov 14 '18 at 11:40
add a comment |
Mike, I would add that it is only for integer types that must be of a well-defined width. For normal integer calculations, the normal int could probably be used so you can take benefit of a controler with a larger native int type.
– Paul Ogilvie
Nov 14 '18 at 11:38
@Paul Ogilivie of course, yes!
– Mike
Nov 14 '18 at 11:40
Mike, I would add that it is only for integer types that must be of a well-defined width. For normal integer calculations, the normal int could probably be used so you can take benefit of a controler with a larger native int type.
– Paul Ogilvie
Nov 14 '18 at 11:38
Mike, I would add that it is only for integer types that must be of a well-defined width. For normal integer calculations, the normal int could probably be used so you can take benefit of a controler with a larger native int type.
– Paul Ogilvie
Nov 14 '18 at 11:38
@Paul Ogilivie of course, yes!
– Mike
Nov 14 '18 at 11:40
@Paul Ogilivie of course, yes!
– Mike
Nov 14 '18 at 11:40
add a comment |
HVDO,3SHayQRPJXpw02tK9qGMSJ1cHlrRrKShFEv EUA wlyffAdAFKcm9h
what is the third option?
– Pete Kirkham
Nov 14 '18 at 10:58
2
uint32_t vs int as a convention for everyday programming may apply here.
– chux
Nov 14 '18 at 11:01
1
If you want to write portable code (or any C code really), make sure you understand the misfeature of C that is the usual arithmetic conversions and its integer promotion rules. They are as important as types themselves.
– user694733
Nov 14 '18 at 11:26
1
Possible dupe: Reasons to use (or not) stdint
– Lundin
Nov 14 '18 at 11:30
stdint.h simply translates into the native/actual C types char int long. within a tool for a specific target, the change is rare. but if porting to another platform (of which much if your code wont port anyway) the stdint approach is good. stdint approach has the problem of a C library which kinda doesnt work with baremetal and depending on the tool can be a PITA (also the problem of how does the C library insure it works with that compiler (sometimes it doesnt)).
– old_timer
Nov 15 '18 at 15:44