Solved

c language help - concate

Posted on 2014-03-01
6
328 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
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

856 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