cancel
Showing results for 
Search instead for 
Did you mean: 

How to use the "errno" in STM32F103CBT6?

Aatif Shaikh1
Associate III

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

8 REPLIES 8
Ozone
Lead II

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.

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?

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.

https://linux.die.net/man/3/strtol

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.

atoi() ?

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.

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".

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.