Chan
Chan

Reputation: 11

How to declare a c++ string with space

How do i declare a variable having spaces like the one below

std::string First Name="harry";

in visual studio 2010 it shows error this declaration has no storage or class specified.

Upvotes: 0

Views: 489

Answers (2)

DevT
DevT

Reputation: 4933

You can not have variable names with spaces. So you have to define them using underscore or use caps as below.

std::string First_Name="harry";
std::string FirstName="harry";

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521914

You cannot have variable names containing whitespace. In fact, white space is what the compiler uses to determine when a variable name, literal, operator, etc., begins and another one ends.

As @DevT mentioned in his comment, one option for simulating whitespace in variable names is to use underscores instead. So you could try the following:

std::string First_Name = "harry";

Upvotes: 3

Related Questions