How does the following print?:
#include <iostream.h>
#include <string.h>
void main()
{
char s[50];
strcpy (s, "What ");
strcat (s, "does this " );
strcat (s, "do?");
cout << s << endl;
}

Can you explain?

Recommended Answers

All 2 Replies

All it prints is a compiler error and some warnings when compiled using GCC.

Correct code is:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  char s[50];
  strcpy (s, "What ");
  strcat (s, "does this " );
  strcat (s, "do?");
  cout << s << endl;
  return 0;
}

which prints exactly what you'd expect it would...

The C-style string handling functions are declared in <cstring>, not <string>.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.