Recentely I have seen that if I use printf
with \'foo\' I get a warning.
printf(\'numero\');
warning: character con
''
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.
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
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.
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.
In c, ''
is used for character constants and ""
for string, unlike python where both can be used interchangeably.