How to concatenate string and int in C?
Strings are hard work in C.
#include <stdio.h>
int main()
{
int i;
char buf[12];
for (i = 0; i < 100; i++) {
snprintf(buf, 12, "pre_%d_suff", i); // puts string into buffer
printf("%s\n", buf); // outputs so you can see it
}
}
The 12
is enough bytes to store the text "pre_"
, the text "_suff"
, a string of up to two characters ("99"
) and the NULL terminator that goes on the end of C string buffers.
This will tell you how to use snprintf
, but I suggest a good C book!
concatenate two integers into a char * in C
You can just do it with one sprintf
:
char str[100];
sprintf(str, "%d:%d", a, b);
How to convert an int to string in C?
EDIT: As pointed out in the comment, itoa()
is not a standard, so better use sprintf() approach suggested in the rivaling answer!
You can use itoa()
function to convert your integer value to a string.
Here is an example:
int num = 321;
char snum[5];
// convert 123 to string [buf]
itoa(num, snum, 10);
// print our string
printf("%s\n", snum);
If you want to output your structure into a file there is no need to convert any value beforehand. You can just use the printf format specification to indicate how to output your values and use any of the operators from printf family to output your data.
How to add/append character to a string?
You probably want something like this:
#include <stdio.h>
#include <string.h>
char box[100] = "";
int main()
{
FILE* fp = fopen("somefilename", "r");
int c;
for (c = getc(fp); c != EOF; c = getc(fp)) {
char tempC[2];
tempC[0] = c;
tempC[1] = 0;
strncat(box, tempC, 1);
}
}
This compiles but it is terrible code:
- there is no check if
fopen
succeeds - Using
strncat
here is very inefficient. As an exercise change this without usingstrncat
or otherstr...
functions. - usage of a global variable without need
Concatenate int to string using C Preprocessor
Classical C preprocessor question....
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define MAJOR_VER 2
#define MINOR_VER 6
#define MY_FILE "/home/user/.myapp" STR(MAJOR_VER) STR(MINOR_VER)
The extra level of indirection will allow the preprocessor to expand the macros before they are converted to strings.
How to add integer in a string?
Do something like
#include <string>
// ...
std::string s( "February " );
s += std::to_string( day );
Here is a demonstrative program
#include <iostream>
#include <string>
int main()
{
std::string s( "February " );
int day = 20;
s += std::to_string( day );
std::cout << s << '\n';
}
Its output is
February 20
Another approach is to use a string stream.
Here is one more demonstrative program.
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::ostringstream oss;
int day = 20;
oss << "February " << day;
std::string s = oss.str();
std::cout << s << '\n';
}
Its output is the same as shown above.
Related Topics
Splitting a String by a Character
How to Avoid Precompiled Headers
How to Call MAChine Code Stored in Char Array
Using Struct Keyword in Variable Declaration in C++
Parse Quoted Strings with Boost::Spirit
Std::Unique_Lock<Std::Mutex> or Std::Lock_Guard<Std::Mutex>
Explain C++ Sfinae to a Non-C++ Programmer
Fast Cross-Platform C/C++ Image Processing Libraries
How to Use a Mask to Iterate Files in a Directory with Boost
May Std::Vector Make Use of Small Buffer Optimization
Linux Equivalent for Conio.H Getch()
C++ Templates, Undefined Reference
Issuing System Commands in Linux from C, C++
Difference Between a Virtual Function and a Pure Virtual Function