C++ examples for Language Basics:Console
Terminate char array with \0 and output with cout
#include <iostream> using namespace std; int main()// w ww . j a v a 2s . co m { char petname[20]; // Reserve space for the pet's name. petname[0] = 'A'; // Assign values one element at a time. petname[1] = 'l'; petname[2] = 'f'; petname[3] = 'a'; petname[4] = 'l'; petname[5] = 'f'; petname[6] = 'a'; petname[7] = '\0'; // Needed to ensure this is a string! cout << petname; // Now the pet's name prints properly. return 0; }