How to use the "errno" in STM32F103CBT6?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-21 1:48 AM
Hello!
I'm using some of the string conversion functions, who's declarations are present in the "stdint.h" file. at present I'm using "strtol" and "strtod". According to the library "the value of the macro ERANGE is stored in errno, If the correct value would underflow, zero is returned". So, mainly I wanted to know if the "ERANGE" is set in he "errno" or not after performing sting conversions.
Hence, I wanted to know how do access the variable "errno"? also, what are the steps or things should I keep in mind while accessing this variable?
Regards,
Aatif Shaikh
- Labels:
-
STM32F1 Series
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-21 2:11 AM
In a POSIX/clib environment, errno is an integer variable with external linkage residing somewhere in clib.a.
It is a "side effect" interface used by clib functions.
> So, mainly I wanted to know if the "ERANGE" is set in he "errno" or not after performing sting conversions.
If it is not documented for you library, you need to look into the (library) source code that is supposed to set errno.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-21 2:49 AM
Thanks for your reply.
I'm using standard peripheral library. In the header file "errno.h" I found the reference of the "errno variable". Now, when I'm able to use it, but still how can differentiate between these operations, which are state in the code snippet.
CODE :
printf("1. -Return value = %d -Errno=%d\n\r",strtol("0",NULL,10),errno);
printf("2. -Return value = %d -Errno=%d\n\r",strtol("A",NULL,10),errno);
OUTPUT :
1. -Return value = 0 -Errno=0
2. -Return value = 0 -Errno=0
In both above operation (1 and 2) i'm getting the same result , thus can anyone tell me how do I identify that the 2nd operation "strtol("A",NULL,10)" is giving a wrong value?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-21 3:12 AM
Many clib function like fopen() return a specific error in 'errno' beside the normal return value (fopen returns NULL in case of error).
In your case, you would need to look into the implementation of the strtol() function.
> thus can anyone tell me how do I identify that the 2nd operation "strtol("A",NULL,10)" is giving ,e a wrong value?
Because "A" is not an element of the character set for decimal numbers (base 10).
Would be for hexadecimal, i.e. base = 16.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-21 3:29 AM
Thanks,
I found a very useful link https://stackoverflow.com/questions/8871711/atoi-how-to-identify-the-difference-between-zero-and-error
Is there a way to use "strtonum"?
if yes, then all my problem will come to an end.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-21 5:34 AM
atoi() ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-21 9:20 PM
I'm aware of "atoi", but unfortunately "atoi" is even more dangerous, cause it crashes the system if it doesn't get any proper numeric string. Whereas "strtol" at least doesn't proceed if a non numeric string passes to it. Therefor, "strtonum" seems to be the best option for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-22 12:17 AM
Then, use the second argument of the strtol() function.
If you pass a non-NULL argument, it will point to the character where number parsing stopped, i.e. the first "invalid" character upon return.
Or (and ?), set the base argument to 16 instead of 10 to convert hexadecimal characters. This would give you 10 (decimal) for the number string "A".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-11-22 12:24 AM
An (incomplete) example:
char numberstring[] = "my number";
char *stoppointer;
int number;
number = (int) strtol (numberstring, &stoppointer, 10);
Afterwards, stoppointer supposedly points to the character where parsing stopped.
Which is the very first char ('m') in this case, meaning nothing was converted.
Assuming a proper implementation of the clib function, of course.
