1

I don't know if this loop is good practice.

Is there a better way to write to a buffer from another buffer in C?

char temp_clientRecv[512] = {0};
for (int i=0; i<512; i++);
fprintf(stdout,"%c" ,temp_clientRecv[i]);
3
  • 7
    The semicolon at the end of the for loop line is a mistake. Commented Aug 28, 2024 at 2:34
  • 1
    Your code intents to write to stdout, not to a buffer. Commented Aug 28, 2024 at 8:35
  • @jonathan leffler yeah was pretty late when I asked this question and overlooked this. my intend was to have the fprintf function inside the for loop. Commented Aug 28, 2024 at 12:55

2 Answers 2

3
  1. It will not compile as i is defined in the for loop scope and will not be declared outside this scope.

  2. You do not write from one buffer to another only send the content of the temp_clientRecv out. If the output stream is buffered or not is out of the scope of C language. It depends on hardware, OS and implementation.

  3. fprintf is an overkill.

fwrite(temp_clientRecv, 1, sizeof(temp_clientRecv), stdout);
Sign up to request clarification or add additional context in comments.

11 Comments

"fprintf is an overkill." --> I used to think that too years ago, yet modern compilers often emit efficient code more like putchar(temp_clientRecv[i]).
@chux-ReinstateMonica The compiler isn't allowed to do that optimization in most cases since it changes the meaning of the code. For example in this case there's an implicit conversion from char to int due to the dysfunctional behavior of variadic functions. There may be sign extension during the conversion and in case of run-time values, the compiler won't be able to tell. Whereas putchar thanks to its dysfunctional API already takes an int as parameter and how that parameter ended up as int is none of putchar's business.
@chux-ReinstateMonica As one can quickly tell, all these functions from stdio.h are so horribly designed that the best advice is simply to stay clear from them in any production code and forget you ever heard of what will go down in history as the by far worst library ever designed for any system in any programming language.
@Lundin Wouldn't char be converted to int exactly the same way for both putchar and printf %c?
@Lundin putchar existed before function prototypes were a thing, so its parameter has to be an int for backwards compatibility. I don't really see a difference between converting a char to an int to match a parameter type, and converting a char to an int due to default argument promotions. Both putchar and printf %c convert the int to an unsigned char.
|
0

If you are copying buffer to buffer in RAM without alteration in the data then you should use following code snippet in your code.

#include <string.h>

memcpy(void *dest_str, const void * src_str, size_t n);

This will copy data bytewise.

1 Comment

I think memcpy is defined in string.h. There is no need to define it again. Although I think OP meant to write from a buffer to a stream, given the context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.