I am surprised, after writing and running following C++ code below on Red Hat Linux.
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char natureofGoods[17];
char *name="sadasdasdasdas171212";
strcpy(natureofGoods,name);
cout<<natureofGoods<<endl;
}
I would wait here as output "sadasdasdasdas17" because natureofGoods
has 17 characters size. But I took as output whole string. I mean "sadasdasdasdas171212asdadsfsf"
If I run this code on Visual Studio, then my program crashes with a Debug Message as I am waiting. Why does not strcpy
cut from 17. Character of name and afterwards copy into natureofGoods
?
How can natureofGoods
storage more caracter than its size?