C stringification: convert constant to integer [closed] - c

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;
}

Related

unsigned integer and integer pointer 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 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

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

C errors causing huge stress on me [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 8 years ago.
Improve this question
I've been playing around with the code below just trying to get it to compile with the Borland compiler (It's the compiler I have to use - I'd much prefer to use GCC on Linux but here we are) but having no luck - can anyone see any obvious flaws?
Code
#include<stdio.h>
#define SIZE 10
float money() {
float wages[SIZE], rise, total; // where i is initaliser
float percent = 0.2;
int i = 0, j;
for(i = 0; i<SIZE; i++) {
wages[i] = j; // Initializing each element seperately
}
printf("Please enter the wages for each staff member\n");
scanf("%d*%c", &wages[i]);
rise = wages[i] * percent;
total = rise + wages[i];
printf("The new wage is $%f and appears in the %d array slot\n", total, j);
return(0);
}
int main() {
money();
return(0);
}
Compiler output
Warning W8004 payrise.c 8: 'i' is assigned a value that is never used in function money
Warning W8065 payrise.c 23: Call to function 'money' with no prototype in function main
These are both warnings; in this case the code referred to by the warnings is correct but it could have its style improved. You should be able to run the program even though it showed warnings; since there were no errors. (BTW you should have help files that came with the compiler; these will tell you what the warnings mean if you find the warning text unclear).
The first warning is because you have:
int i = 0;
for (i = 0;
i.e. the first setting of = 0 is redundant because you immediately set it to 0 again.
The second warning is because you did not provide a prototype for money. You should declare the function as:
float money(void)
This makes it be a prototype, which has the effect that the compiler will flag an error if you later try to pass an argument to the function.
However, your code has other bugs which the compiler did not diagnose (or if it did, you didn't let us know).
Firstly you use j without initializing it. You should give it a value before using it.
Secondly, scanf("%d*%c", &wages[i]); is wrong. The format specifier for float is "%f", not "%d*%c". Also , i is out of range at this point (did you mean to have this code inside the loop?)

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.

c programming Ascii values [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}

Resources