Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the datatype of string literal in C++?

Tags:

I'm confused about the datatype of a string literal. Is it a const char * or a const char?

like image 950
Vihaan Verma Avatar asked Sep 20 '12 17:09

Vihaan Verma


People also ask

What data type is string literal?

String literals are specified by one or more characters enclosed in single quotes. The default data type for string literals is varchar, but a string literal can be assigned to any character data type or to money or date data type without using a data type conversion function.

What are the two types of string literals?

A string literal with the prefix L is a wide string literal. A string literal without the prefix L is an ordinary or narrow string literal. The type of narrow string literal is array of char .

How many types of string literals are there?

There are three sets of literal types available in TypeScript today: strings, numbers, and booleans; by using literal types you can allow an exact value which a string, number, or boolean must have.

Is a string literal or variable?

A string literal is where you specify the contents of a string in a program. Here 'A string' is a string literal. The variable a is a string variable, or, better put in Python, a variable that points to a string. String literals can use single or double quote delimiters.


1 Answers

It is a const char[N] (which is the same thing as char const[N]), where N is the length of the string plus one for the terminating NUL (or just the length of the string if you define "length of a string" as already including the NUL).

This is why you can do sizeof("hello") - 1 to get the number of characters in the string (including any embedded NULs); if it was a pointer, it wouldn't work because it would always be the size of pointer on your system (minus one).

like image 103
Seth Carnegie Avatar answered Oct 20 '22 20:10

Seth Carnegie