C Programming, Combining chars February 11, 2010 10:44PM | Registered: 15 years ago Posts: 70 |
Re: C Programming, Combining chars February 11, 2010 11:06PM | Admin Registered: 15 years ago Posts: 5,132 |
#include(iostream) #include(string) std::string basestring = ""; char1 = 'H'; char2 = 'i'; basestring += char1; basestring += char2; int main(){ std::cout << basestring; int pause; std::cin >> pause; return 0; }
Re: C Programming, Combining chars February 11, 2010 11:09PM | Registered: 14 years ago Posts: 5,075 |
string sChar = char1 char2 char3;
Re: C Programming, Combining chars February 12, 2010 12:38AM | Registered: 15 years ago Posts: 43 |
Re: C Programming, Combining chars February 12, 2010 11:00AM | Registered: 15 years ago Posts: 384 |
char char1= "H"; char char2="i" char baseString[MAX_PATH]; char baseString[0]=char1; char baseString[1]=char2; char baseString[2]="\n";
Re: C Programming, Combining chars February 12, 2010 03:14PM | Registered: 14 years ago Posts: 379 |
#include <stdio.h> int main() { char c1 = 'c'; char c2 = 'd'; char c3 = 'c'; char resultBuffer[32]; resultBuffer[0] = c1; resultBuffer[1] = c2; resultBuffer[2] = c3; resultBuffer[3] = 0; //Don't forget to end the new 'string' with a NUL char. printf("%s\n", resultBuffer); }
Re: C Programming, Combining chars February 12, 2010 03:32PM | Registered: 14 years ago Posts: 5,075 |
Re: C Programming, Combining chars February 13, 2010 02:59PM | Registered: 15 years ago Posts: 384 |
Woops, by "\n" I meant "\0" or '\0' actually. Thanks for telling me that chars are single quotes! However, what's the difference between "H" and 'H' then? My guess is that "H" includes the null terminator. Am I right?Quote
Daid
profetylen is heading the right way. But also fails at the C syntax. Chars are single quotes, not double. And you lack the NUL terminator.
Re: C Programming, Combining chars February 14, 2010 02:24AM | Registered: 15 years ago Posts: 70 |
Quote
SifJarQuote
Daid
SifJar fails at basic C syntax. 'string' is not a C type. Nor does putting some variable after each other make them magically work.
Apologies, like I said I am a coding n00b.
Re: C Programming, Combining chars February 16, 2010 11:03AM | Registered: 14 years ago Posts: 379 |
'H' is of the type [char] and has the value 72Quote
profetylenWoops, by "\n" I meant "\0" or '\0' actually. Thanks for telling me that chars are single quotes! However, what's the difference between "H" and 'H' then? My guess is that "H" includes the null terminator. Am I right?Quote
Daid
profetylen is heading the right way. But also fails at the C syntax. Chars are single quotes, not double. And you lack the NUL terminator.
#includeThis code will compile, and it will run. And there is a chance that it won't even crash. But it is wrong, as it writes 11 chars into a buffer only holding 4 chars.int main() { char resultBuffer[4]; //Make a 'string' that contains a max of 4 [char]s, including the 0 strcpy(resultBuffer, "1234567890"); printf("%s\n", resultBuffer); }
Re: C Programming, Combining chars February 16, 2010 11:08AM | Registered: 15 years ago Posts: 384 |