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 7 years ago.
Improve this question
How can a string like "EINVAL" be converted into EINVAL?
I have to convert a lot of macro string to their value, what can be done?
Here is the simplest method I can think of:
#include <errno.h>
if (!strcmp(str, "EINVAL"))
value = EINVAL;
No preprocessor trick can convert the string literal to the corresponding symbol.
But you can use the preprocessor to simplify a sequence of such tests:
value = 0;
#define conv(s) do { if (!strcmp(str, #s)) value = s; } while (0)
conv(EINVAL);
conv(ENOMEM);
conv(ERANGE);
conv(EINTR);
#undef conv
A usual, be careful with the preprocessor...
Related
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 9 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).
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 2 years ago.
Improve this question
Write a C program to convert a number in scientific notation to its equivalent decimal form :
Given
8.3e+2 output = 830
2.E-1 output = 0.2
4.3E2 output = 430
Is there any inbuilt library or function in C/C++ which can do this?
or one will have to write his own code, in that case, any easy way to achieve this?
try this
#include <iostream>
#include <string>
int32_t main(int32_t argc, char *argv[]) {
std::string str = "8.3e+2";
std::cout << std::stod(str) << std::endl;
return EXIT_SUCCESS;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am trying to create a macro wrapper around a function so I could make the code more intuitive on reading. Something like instead of calling send_message_to_destination(m, d) to write send(m)to(d).
#include <stdio.h>
void send_data_to(int data, int dest)
{
printf("Send %d to %d\n", data, dest);
}
#define send(data)to(destination) send_data_to(data, destination)
int main() {
int data = 5;
int dest = 10;
send(data)to(dest);
}
It is possible to do so?
Do you think this would makes the code more readable or intuitive ?
I agree with the comments (not a good idea), however, you can use something like this. Remember the pre-processor just does a text replace with your macros.
#define to(destination) destination)
#define send(data) send_data_to(data,
No there is no such way to do so with MACROS
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 7 years ago.
Improve this question
#define SRC_ASCLIN_ASCLIN0_RX (*(volatile Ifx_SRC_SRCR*)0xF0038084u)
Here SRC_ASCLIN_ASCLIN0_RX means ASCLIN(Async/Sync serial LIN Comm) Receive Service Request.
I know that the macro is used to point at the address 0xF0038084u. But I want real time examples.
Am working on UART development on Infineon microcontroller.
The macro, when expanded by the preprocessor, cast the integer literal as an address, a pointer to Ifx_SRC_SRCR, then dereferences the pointer so you can get or set the value of the memory stored at that specific address.
So you could write e.g.
Ifx_SRC_SRCR value = SRC_ASCLIN_ASCLIN0_RX;
or
SRC_ASCLIN_ASCLIN0_RX = some_other_value;
It basically equivalent to doing e.g.
int an_integer = 6;
int *pointer_to_an_integer = &an_integer;
*pointer_to_an_integer = 10; // Equivalent to SRC_ASCLIN_ASCLIN0_RX = some_other_value above
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 9 years ago.
Improve this question
an interview question on glassdoor is as follows. With my knowledge, it is hard to deduce anything out of it. What could be an appropriate question?
A macro that computes a size_t number. Putting in a loop, it casts -1
to a size_t number, making the loop impossible to start.
as suggested by Michael Aaron Safyan, following might be the case
operates in the reverse:
for (size_t i = 0; i > ((size_t) -1); i--) {}
For explanation see the answer
The issue is that size_t is unsigned, so casting -1 to it will produce the maximum-valued size_t. One would fix this case by using a signed type (such as int or ssize_t).