Solved

c language help - concate

Posted on 2014-03-01
6
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 39897659
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
ID: 39897665
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
ID: 39897672
>> 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:phoffric
ID: 39897741
>> 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
ID: 39897836
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
ID: 39897930
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

695 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