unsigned integer and integer pointer in c [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a code where I pass 300000(value of 5 min in ms)
in function.
BOOL setsomethingfunc(tpUINT8 value) //tpUINT8 is integer pointer
{
bufVal=atoi(value); //bufVal is type UINT8
}
Now when I print bufVal it comes as 224. I'm not able to understand how the value conversion happened. Can someone please explain ?
So, Integer pointer has - 300000 and when converted to int it becomes 224.
I've worked on a minimal reproducible example for the scenrio -
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main()
{
uint8_t backoff;
char* value = "300000";
backoff=atoi(value);
printf("value = %s\n", value);
printf("backoff value = %d\n", backoff);
return (0);
}
Output is as -
value = 300000
backoff value = 224
Can someone please help me understand how this conversion happened ?

BOOL setsomethingfunc(UINT8 value) your function should take pointer not the integer. It has to be BOOL setsomethingfunc(UINT8 *value)
Use standard C types. uint8_t for 8 bits unsigned integer and bool from stdbool.h. Many old code (or the code which development started long time ago like Linix kernel) use those non standard types for historical reasons. You should learn the modern C

Related

full lenght Hexa value is not converting in full length string in C? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I m working on an IR project when we pressed the button the value are store in struct
typedef struct
{
decode_protocol_t decode_protocol;
unsigned long hexavalue;
unsigned int address;
unsigned int magnitude;
int bits;
int rawlen;
int rawbuf[500];
char *protocolString;
} receive_cmd_t;
receive_cmd_t results;
To print the hex value.
printf("0x%lX", results->value);
Output: 0xA1DE11EE
Now I just want to convert this A1DE11EE value into the string format.
For that, I used snprintf function. Here is my code.
unsigned long value = A1DE11EE;
char hex[sizeof(value)*10];
snprintf(hex, sizeof(hex), "%lX",value);
printf("HEX value:%s\n", hex);
I was expecting HEX value: A1DE11EE. What I got HEX value: A1D
I tried to increase the hex buffer but still, it's giving the same output.
I did not understand why it's happening?
And also how I convert back into hexadecimal 0xA1DE11EE.
Sorry for giving less explanation of the issue.
Just solving the compiler error (assignment to value) and warning (snprintf misspelled as snprinf) is enough to make the program work
#include <stdio.h>
int main() {
unsigned long value = 0xA1DE11EE; // note the 0x
char hex[sizeof(value)*10];
snprintf(hex, sizeof(hex), "%lX",value); // snprintf was misspelled as snprinf
printf("HEX value:%s\n", hex);
return 0;
}
This will print
HEX value:A1DE11EE

Return type of unsigned int function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a .so file which has C functions for RFID read card, and I'm calling the C functions in my python program.
The C function type is unsigned int get_card() which has unsigned int card as a variable, and returns the value in unsigned int format.
But when I call that C function in my python program, and print the returned value, I'm getting a negative value.
Can you please help on this?
This is because your python code “doesn’t knows” that the value is unsigned int, so if the number in binary starts with the number one, for example “10” that is equal to 2 in decimal, python will read it as “-0” in decimal. So You need to create in python an unsigned int variable and copy the return value in the new variable, then you can print it. Hope it helps you

C stringification: convert constant to integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to convert defined constants to their integer values so I came up with this macro (STR_TO_CONST) for doing so...
#define STRFY_VAL(s) #s
#define STRFY_KEY(s) STRFY_VAL(s)
#define STR_TO_CONST(s) atoi(STRFY_KEY(s))
It works, but I'm wondering if there are any potential problems with it as I've never encountered this macro before despite having searched considerably for something like it.
The reason you never encountered this is that it's utterly pointless, but let's explain by example. Say you have the following:
#define FINALANSWER 42
// ...
int x = 2 * STR_TO_CONST(FINALANSWER);
now, this is semantically no different from:
int x = 2 * FINALANSWER;
That's because preprocessor macros are ultimately just textual replacement happening before you actually compile. Therefore, FINALANSWER is just as good as an integer constant as 42 is.
Your "solution" to a non-existing problem just adds overhead in that it adds a new string constant to your code and an unnecessary function call as well.
I'm wondering if there are any potential problems with it (?)
Yes. Using atoi() to initialize a global results in something like "error: initializer element is not constant"
int x = STR_TO_CONST(123); // error
int y = 123; // no error
int main(void) {
return x + y;
}
Hide warnings. Only 1 line generated a useful warning
"warning: overflow in implicit constant conversion [-Woverflow]"
int main(void) {
int a = STR_TO_CONST(123456789012345); // no warning
int b = 123456789012345; // warning
return a + b;
}
Range issue. With 32-bit int, the below will likely exceed atoi() range resulting in undefined behavior with no warning.
int main(void) {
long long z = STR_TO_CONST(123456789012345);
return !z;
}

Code output explanation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Question 1.
#include <stdio.h>
int main(void)
{
int c;
while((c=getchar())!='\0')
{
putchar(c);
}
}
Input
Hello C.
Tell me about you.
Output
Hello C.
Tell me about you.
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
and it continues with status-time limit exceeded.
Question 2.
#include <stdio.h>
int main(void)
{
float a;
a=46.43253;
printf("\n%d",a);
printf("\n%f",a);
return 0;
}
Output
536870912
46.432529
Output- 536870912
46.432529
In general using incorrect format specifier triggers undefined behavior - which is what you have when you use %d in printf for printing float. In this case, you can expect any output usually.
However, it may also be the case that since you have specified to read the float number as integer (e.g. by using %d specifier), it simply interpreted the result as integer - hence the strange number (since floats and integers are stored differently).
If you are interested why the second printf prints a number slightly different from yours, this may help you.
This block is fine:
float a;
a=46.43253;
This block is also fine:
printf("\n%f",a);
The problem is with this block:
printf("\n%d",a);
Particularly this part:
"\n%d"
Please keep in mind you declared a float and using the integer syntax to output it. That's why you are getting the negative output
If it is a case where you don't want to change the "%d," then simply cast it as a float before output

Format specifier in C not clear [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
Why the output is;
6.412
What is the effect of .* here ?
The . means that the next characters indicate the precision to use. The * means to read the value from the argument list; in your case, it will read a. The value is 3, so the next argument is printed to 3 decimal places.
In printf function, the format %[flags][width][.precision][length]specifier of this question is .precision, it has two choices number or *.
When *, it means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
For more details, see http://www.cplusplus.com/reference/cstdio/printf/
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
It substiutes the value of a to the *,implying a precision.

Resources