Solved

c language help - concate

Posted on 2014-03-01
6
308 Views
Last Modified: 2014-03-01
Hi,

I am newbie c language learner. Please let me know how can I define a string and concatenate some integer values with string and print it too. I also need to send that string data to a server using a API.

I tried below but it shown strange characters in print and I guess it will send same strange characters when I sent it to server using API. Please guide.

thx

char str[200];
int q,w,e;
strcpy (str," ");
		strcat (str,"X:");
	   	strcat (str,q);
		strcat (str," Y:");
		strcat (str,w);
		strcat (str," Z:");
		strcat (str,e);
	
	sprintf("Data: '%s'\n", str); // show strange characters...

Open in new window

0
Comment
Question by:Johny12345
  • 4
  • 2
6 Comments
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
You cannot cat an integer directly. You could use sprintf to create the entire string.
http://www.cplusplus.com/reference/cstdio/sprintf/?kw=sprintf
You already know how to use %s to include a string. Here is their sample program that demonstrates how to include integers in the buffer.
/* sprintf example */
#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  return 0;
}

Open in new window

0
 

Author Comment

by:Johny12345
Comment Utility
thx for your reply. I need your more help. Please let me know what happens at:

char buffer [50];      // You have defined character with max limit of 50 characters

  int n, a=5, b=3;    // defined 3 integers

  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);  
// what will be value of n and what will be value of buffer
// Also, you have defined n as int so how it can help string??

  printf ("[%s] is a string %d chars long\n",buffer,n);

Please reply soon.

thx
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
Comment Utility
>> what will be value of n and what will be value of buffer
>> Also, you have defined n as int so how it can help string??
What helps the string is buffer - it contains the entire string that is output by the sample program.
Return Value
On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.
 On failure, a negative number is returned.
I would put the sample program and write out the value of n to make sure you agree with the documentation.
The output of the sample program is:
[5 plus 3 is 8] is a string 13 chars long

Open in new window

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 32

Expert Comment

by:phoffric
Comment Utility
>> char buffer [50];      // You have defined character with max limit of 50 characters
Not 50 characters; only 49, since one byte is needed for the string's terminating null byte.

For n, the int return value, you should test for an error condition since "On failure, a negative number is returned".
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
I'll be back in less than 24 hours to see if you have any follow up questions.

Paul
0
 

Author Closing Comment

by:Johny12345
Comment Utility
thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now