0

I am very new to C and am finding that strings in C are giving me quite the headache. I have a program that prints out some data from my struct that holds information on an object. I have an array of these structs and I want to print the data with a nice message. here is the kicker, I am using UDP socket programming and my server handles this information and sends a char array (string) back in which I then print this. Is there an easy way I can achieve this

printf("Economy capacity: %d\n", database[index].ecap);

but instead of printing it, assign this statement to a char array? this does not work

strcpy(output,"Premium capacity: %d\n", database[index].pcap);
1
  • I think you are trying to describe sprintf or preferably snprintf Commented Oct 22, 2014 at 0:37

2 Answers 2

1

strcpy is not the function you want to use. You want to use snprintf

char output[256];
snprintf(output,256,"Premium capacity: %d\n", database[index].pcap);
Sign up to request clarification or add additional context in comments.

Comments

0

You want snprintf(). Just like printf() but writes to a char array.

1 Comment

This is exactly what I needed. Very first C project coming from java….thats all I am going to say about that haha

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.