C++ char type Terminate char array with \0 and output with cout

Description

C++ char type Terminate char array with \0 and output with cout

#include <iostream>
using namespace std;
int main()//  w w w.j ava 2 s.c o 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;
}



PreviousNext

Related