Solved

c language help - concate

Posted on 2014-03-01
6
333 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust Mfcapp 29 178
SQL handling single and double quotes 3 99
c++ substatte a varabe for a string in a LPCTSTR statment 8 95
windows 10 pro lost profile. 10 74
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

697 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