warning: multi-character character constant [-Wmultichar]| [duplicate] - c

This question already has answers here:
Multi-character constant warnings
(6 answers)
Closed 8 years ago.
/* Beginning 2.0 */
#include<stdio.h>
main()
{
printf(" %d signifies the %c of %f",9,'rise',17.0);
return 0;
}
Hello people
When I compile this, the compiler giving the following warning:
warning: multi-character character constant [-Wmultichar]|
And the output only prints e instead of rise.
Are multiple characters not allowed in C?
How can I print the whole word (rise)?
Please help me out.

Try: printf(" %d signifies the %s of %f",9,"rise",17.0);.
C distinguishes between a character (which is one character) and a character string (which can contain an arbitrary number of characters). You use single quotes ('') to signify a character literal, but double quotes to signify a character string literal.
Likewise, you specify %c to convert a single character, but %s to convert a string.

Use %s and "" for a character string:
printf(" %d signifies the %s of %f",9,"rise",17.0);
^^ ^ ^
For 'rise', this is valid ISO 9899:1999 C. It compiles without warning under gcc with -Wall, and a “multi-character character constant” warning with -pedantic.
According to the standard (§6.4.4.4.10),
The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined.

Related

Why "$" is printed when I try to run the C code below? [duplicate]

This question already has an answer here:
What is the difference between %c and %s in C [duplicate]
(1 answer)
Closed 4 years ago.
#include <stdio.h>
int main()
{
printf("%c\n","\"\"\"\"\"");
}
This prints $ but not """""
Why "$" is printed when I try to run the C code above?
You pass a string to printf, not a char. Try '"'
What happens in your code is that the string is passed to printf, but printf expects a char. printf now tries to interpret the address of this string as a char. In general, this is undefined behavior.
%c is the format specifier for a single character; there is probably some conversion going on. You want %s (for strings).
It actually prints some garbage, because your input is not a single character, is a sequence of characters. Try using %s instead of %c

Scanning with %c or %s

I had to do a program for college in which I should separate, between a certain amount of people, those who liked and the ones who disliked something.
so I did this:
char like[100];
printf("Like? Y or N \n");
scanf ("%c", like);
The program compiled, but didn't work the way it should. The user was not able to write "y or n" when asked "Like?"
So I tried this:
char like[100];
printf("Like? Y or N \n");
scanf ("%s", like);
And it worked. But I don't know why it worked. Can somebody please explain me the difference between %c and %s in a scanf?
First, please do some basic research before coming here - questions like this can usually be answered with a quick Google search or checking your handy C reference manual.
char inputChar; // stores a single character
char inputString[100] = {0}; // stores a string up to 99 characters long
scanf( " %c", &inputChar ); // read the next non-whitespace character into inputChar
// ^ Note & operator in expression
scanf( "%s", inputString ); // read the next *sequence* of non-whitespace characters into inputString
// ^ Note no & operator in expression
You would use %c when you want to read a single character from the input stream and store it to a char object. The %c conversion specifier will not skip over any leading whitespace, so if you want to read the next non-whitespace character, you need a blank before the %c specifier in your format string, as shown above.
You would use %s when you want to read a sequence of non-whitespace characters from the input stream and store them to an array of char. Your target array must be large enough to store the input string plus a terminating 0-valued character. The %s conversion specifier skips over any leading whitespace and stops reading at the first whitespace character following the non-whitespace characters.
Both %c and %s expect their corresponding argument to have type char * (pointer to char); however, in the first case, it's assumed that the pointer points to a single object, whereas in the second case, it's assumed that the pointer points to the first element of an array. For inputChar, we must use the unary & operator to obtain the pointer value. For inputString, we don't, because under most circumstances an expression of type "array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.
Your code works fine as it is, but it's a bit confusing to read a single character and store it to an array.
Using %s without an explicit field width is risky; if someone types in more than 100 non-whitespace characters, scanf will happily store those extra characters to memory following inputString, potentially clobbering something important. It's generally safer to write something like
scanf( "%99s", inputString ); // reads no more than 99 characters into inputString
or to use fgets() to read input strings instead:
fgets( inputString, sizeof inputString, stdin );
Please check §7.21.6.2 of the online draft of the C language standard for a complete description of all of the conversion specifiers for the *scanf functions.
%s is used for string of characters and reads subsequent characters until it finds a whitespace(blank, newline or tab). Whereas %c is used for single character and reads the next character. If there are more than 1 character, including any whitespace character, it is read and stored in next iteration.

What does an asterisk in a scanf format specifier mean? [duplicate]

This question already has answers here:
What is the difference between %*c%c and %c as a format specifier to scanf?
(3 answers)
Closed 7 years ago.
So I stumbled across this code and I haven't been able to figure out what the purpose of it is, or how it works:
int word_count;
scanf("%d%*c", &word_count);
My first thought was that %*d was referencing a char pointer or disallowing word_count from taking char variables.
Can someone please shed some light on this?
*c means, that a char will be read but won't be assigned, for example for the input "30a" it will assign 30 to word_count, but 'a' will be ignored.
The * in "%*c" stands for assignment-suppressing character *: If this option is present, the function does not assign the result of the conversion to any receiving argument.1 So the character will be read but not assigned to any variable.
Footnotes:
1. fscanf
To quote the C11 standard, chapter §7.21.6.2, fscanf()
[...] Each conversion specification is introduced by the character %.
After the %, the following appear in sequence:
— An optional assignment-suppressing character *.
— [...]
— A conversion specifier character
and regarding the behavior,
[..] Unless assignment suppression was indicated by a *, the
result of the conversion is placed in the object pointed to by the first argument following
the format argument that has not already received a conversion result. [...]
That means, in case of a format specifier like "%*c", a char will be read from the stdin but the scanned value won't get stored or assigned to anything. So, you don't need to supply a corresponding parameter.
So, in this case,
scanf("%d%*c", &word_count);
is a perfectly valid statement.
For example, What it does in a *nix environment is to clear the input buffer from the newline which is stored due to pressing ENTER key after the input.

I have come across this code snippet as shown below. Can anyone suggest what is the meaning of a in scanf function

Can anyone suggest what is the meaning of a in the following call to scanf?
scanf("%d a %f",&i,&f)
Characters preceded by a '%' in a call to scanf represent variables.
For instance %d represents an integer variable whereas %f represents a floating-point variable.
Characters which are not preceded by a % (or a \, which indicates an escape sequence) are taken literally, so, in your case, the scanf string "%d a %f" would match "233 a 4.5" but would not match "233 b 4.5".
(To be more accurate, a whitespace character matches any contiguous sequence of whitespace characters.)
scanf("%d a %f",&i,&f)
Means you have to type in data , in this format 25 a 33.3
Then when you print it using
printf("i=%d f=%f",i,f);
and then you get the output as
i = 25 , f = 33.3
You are not getting the value of the variable f because of the & in the line scanf("%d a %f",&i,&f)
The & means you are getting the address of the variable f in the memory. You should remove the '&'s to get the actual value of the variables.
And for the a:
Non-whitespace character, except format specifier (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a % character) causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.
Which means you are formatting the input as so:
type in a decimal integer(%d)
then a space
then the character 'a'
another space
then the floating point number(%f).
Reference: http://www.cplusplus.com/reference/cstdio/scanf/

What does %s and %d mean in printf in the C language? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I don't understand what the %s and d% do in this C code:
for (i=0;i<sizeof(code)/sizeof(char*); i++) {
printf("%s%d%s%d\n", "Length of String ", i, " is ", strlen(code[i]));
str = code[i];
printf("%s%d%s%c\n","The first character in string ", i, " is ", str[0]);
}
I'm new to the C language and my background is in Java.
What do the %s%d%s%d symbols denote?
Why are there so many of them?
Is the comma used here for concatenation instead of a +?
The printf() family of functions uses % character as a placeholder. When a % is encountered, printf reads the characters following the % to determine what to do:
%s - Take the next argument and print it as a string
%d - Take the next argument and print it as an int
See this Wikipedia article for a nice picture: printf format string
The \n at the end of the string is for a newline/carriage-return character.
% notation is called a format specifier. For example, %d tells printf() to print an integer. %s to print a string (char *) etc. You should really look it up here: http://google.com/search?q=printf+format+specifiers
No, commas are not used for string concatenation. Commas are for separating arguments passed to a function.
The first argument denotes placeholders for the variables / parameters that follow.
For example, %s indicates that you're expecting a String to be your first print parameter. Java also has a printf, which is very similar.
"%s%d%s%d\n" is the format string; it tells the printf function how to format and display the output. Anything in the format string that doesn't have a % immediately in front of it is displayed as is.
%s and %d are conversion specifiers; they tell printf how to interpret the remaining arguments. %s tells printf that the corresponding argument is to be treated as a string (in C terms, a 0-terminated sequence of char); the type of the corresponding argument must be char *. %d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int. Since you're coming from a Java background, it's important to note that printf (like other variadic functions) is relying on you to tell it what the types of the remaining arguments are. If the format string were "%d%s%d%s\n", printf would attempt to treat "Length of string" as an integer value and i as a string, with tragic results.
%s is for string
%d is for decimal (or int)
%c is for character
It appears to be chewing through an array of characters, and printing out whatever string exists starting at each subsequent position. The strings will stop at the first null in each case.
The commas are just separating the arguments to a function that takes a variable number of args; this number corresponds to the number of % args in the format descriptor at the front.
%d is print as an int
%s is print as a string
%f is print as floating point
It should be noted that it is incorrect to say that this is different from Java. Printf stands for print format, if you do a formatted print in Java, this is exactly the same usage. This may allow you to solve interesting and new problems in both C and Java!
The first argument to printf is a string of identifiers.
%s refers to a string
%d refers to an integer
%c refers to a character.
Therefore: %s%d%s%c\n prints the string "The first character in sting ", %d prints i, %s prints " is ", and %c prints str[0].
%(letter) denotes the format type of the replacement text. %s specifies a string, %d an integer, and %c a char.
%s%d%s%d\n is a format string. It is used to specify how the information is formatted on an output. here the format string is supposed to print string followed by a digit followed by a string and then again a digit. The last symbol \n represents carriage return which marks the end of a line. In C, strings cannot be concatenated by + or , although you can combine different outputs on a single line by using the appropriate format strings (the use of format strings is to format output info.).

Resources