Solved

c language help - concate

Posted on 2014-03-01
6
320 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ check and remove last word from a string 5 171
chcp 65001 File encoding 66 246
Want to delete all my personal data 13 145
Using popen() and gunzip() to open file in HTTPServer 6 59
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…
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 and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

770 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