-5

Recentely I have seen that if I use printf with 'foo' I get a warning.

printf('numero');

warning: character constant too long for its type [enabled by default] warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast. /usr/include/stdio.h:362:12: note: expected ‘const char * restrict’ but argument is of type ‘int’ extern int printf (const char *__restrict __format, ...); warning: format not a string literal and no format arguments [-Wformat-security]

And when I use "" I don't get any warnings printf("numero");

So, what's the difference between '' and ""?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
salajadin
  • 11
  • 1
  • 5
  • http://publications.gbdirect.co.uk/c_book/chapter2/integral_types.html. Also, see point 8 of http://c-faq.com/ – jcoppens Jun 27 '15 at 13:14

5 Answers5

7

In c the string delimiter is ", the ' is used for character constants.

The double quotes " will generate a string, a sequence of bytes with a terminating '\0' byte.

Example:

const char *string = "Example";
/* the sequence -> ['E', 'x', 'a', 'm', 'p', 'l', 'e', '\0'] is generated */

The ' will generate an integer, in the case of a single character it's the ascii value that it represents, in case of multiple characters it's implementation defined.

Example:

char A = 'A'; /* the ascii value of 'A', 0x41 or 65 decimal */

Multicharacter strings, will also generate an integer, but it's value changes depending on the c implementation/compiler.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
7

There is a difference between character literals and string literals.

To define a character literal you need to use single quotes. For example

'A' is a character lietral. In C it has type int and even called like integer character constant. Its value is a numerical value of the internal representation of the character. You also may use multibyte character constants as for example 'AB' but its value is implementation defined.

To define a string literal you need to use double quotes. For example "A" is a string literal. It has type of a character array of two characters (including the terminating zero) char[2]. You can imagine it like

char s[2] = { 'A', '\0' };

In expressions character arrays are converted to pointers to their first elements. Thus in an expression string literal is converted to type char *. You can imagine it like

char s[2] = { 'A', '\0' };
char *p = s;

The first parameter of function printf has type const char *. Thus a string literal used as the argument may be used in function printf.

For example

printf( "A" );

It is the same as

printf( p );

where p is defined as it is shown above.

An integer character constant has type int. Using it as the argument can result in undefined behaviour because its value will be interpretated by function printf as an address of a string. So this statement

printf( 'A' );

is invalid. The printf will consider the internal value of the constant for example 65 (if to consider the ASCII table) as some memory address and will try to output what is stored at this address.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
5

In , '' is used for character constants and "" for string, unlike where both can be used interchangeably.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 3
    Not just python, many other high level languages have this bug ... sorry I mean, feature. The only reason I know to justify this, is that you don't need to escape one of the qoute types if you wrap it with the other one, is that a good reason? – Iharob Al Asimi Jun 27 '15 at 12:37
  • @iharob; Yeah that could be a reason. – haccks Jun 27 '15 at 12:39
  • 3
    @iharob I think the reason is that these languages don't have a type for single characters, only strings. So why not making use of the single quotes as well? – Yu Hao Jun 27 '15 at 12:44
  • @YuHao; That's also a valid reason. – haccks Jun 27 '15 at 12:49
  • @YuHao because it's confusing? And because it can cause consistency problems. See, that's why I love c, because it has one of each feature so you don't need to remember a lot of things. – Iharob Al Asimi Jun 27 '15 at 12:50
  • @AnttiHaapala Those are different ways to write zero. Like different ways to write one `a/a` or `sin(x)^2 + cos(x)^2`. It turns out I am learning to write compilers, and this is rather a common thing. But choosing to use both kinds of quotes for strings is simply stupid. I struggle in javascritp to leave one kind of quote for when I want to search for a given value in a string for instance I don't need to worry about the other quote kind in the REGEX that I use. There are tools for that. I suggest looking/learning Go lang. It's simplicity is simply TOO ELEGANT TO EXIST. – Iharob Al Asimi May 22 '19 at 21:40
  • Oh, and I hate inconsistency. It makes me want to kill myself. – Iharob Al Asimi May 22 '19 at 21:41
  • 1
    @IharobAlAsimi but strings in javascript are no longer a regular language thanks to string interpolation and such! :D – Antti Haapala -- Слава Україні May 23 '19 at 05:15
  • @AnttiHaapala Good observation. – Iharob Al Asimi May 23 '19 at 10:42
4

'' is used to denote characters while "" is used to denote strings.

printf expects a const char*(a string) as its first argument. You need to use "" for that. If you use '', the compiler will complain and will tell you that printf expects a const char* as its first argument and not a char.

FYI, adding more than one character into '' (like 'numero') will make a multi-character literal whose value is implementation defined.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

In c, ' is used for character constants, where as " is used for a string

The printf function in c needs a string, so your code printf('numero'); will result an unexpected behavior.

Instead, use printf("numero");

You can read this small tutorial for more help

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Shri
  • 834
  • 1
  • 12
  • 36