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 3 months ago.
Improve this question
Hi I was wondering if there is an alternative of str.substring() in string.h. If not, what is an efficient alternative?
Assuming that we all agree that using c++ it is safer and more professional to use the standard library tools.
That said, if we're talking about C and not C++, this should be one of the ways to extract a substring with the "string.h" library:
#include <stdio.h>
#include <string.h>
int main() {
char test[] = "abcdef";
char subtext[3];
memcpy(subtext, &test[1], 2); //Result: "bc"
subtext[2] = '\0';
printf("%s", subtext);
return 0;
}
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 1 year ago.
Improve this question
If printf("AAAA%n", &a) puts 4 into a, how can I put 0x403b76 into a?
Well, you could do this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
const int size = 0x403b76;
char *s = malloc(size+3);
memset(s, 'a', size);
strcpy(&s[size], "%n");
int a;
printf(s, &a);
printf("\n%x\n", a);
}
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 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
What does the following code segment do?
#include <stdio.h>
#include <stdlib.h>
int main(){
int num=0;
while(malloc(1<<10)) ++num;
}
First of all, 1<<10 is an expression that can be calculated at compile time, and it equals 1024. So your code is equivalent to
#include <stdlib.h>
int main()
{
int num=0;
while(malloc(1024)) ++num;
}
So what it does is to allocate chunks of 1024 bytes of memory until it fails to do so. Each time, the value of num is increased by one.
Overflowing the variable num will cause undefined behavior because it is signed. However, since you are not using the variable, it is likely to be optimized away.
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 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
I really don't understands this code:
#include <stdio.h>
int main (int argument c, char *argument v[])
{
return 0;
}
What does this code mean? How does it converts to other formation of coding?
This is (almost) the simplest C/C++ program. (It works for both languages.) It does nothing other than return 0, which signifies successful execution.
It should read
int main(int argc, char **argv)