gogoWebsite

Difference and division between %c and %s in C language

Updated to 9 hours ago

Article Catalog

    • Characters and strings
    • char and char*
    • conversion descriptor
    • symbolize
    • specification

Characters and strings

The %c format corresponds tosingle character
The %s format corresponds tostring (computer science)
Example:

char a;
char b[20];
scanf("%c",&a); //can only input one character.
scanf("%s",b); //String not exceeding 20 characters, string subscript 0~19.

char and char*

%c corresponds to type char
The corresponding type of %s is char * , i.e., string.

When used as input, both parameters should be of type char *.
The %c input function only assigns a value to a byte space. %s assigns until a blank character is encountered in the input.
When used as output, %c passes char and outputs one character. When %s passes a char* type parameter, it outputs until \0.

For input, scanf("%c", &a); the & must not be missing.
And scanf("%s",s); there can't be & sign here.

conversion descriptor

%c Characters
%d Signed decimal integers
%f Floating point(including throughfloatAnd doulbe)
%e(%E)     Floating-point exponential output[e-(E-)method of counting (e.g. decimal or Roman numbers)]
%g(%G)     Floating point numbers do not show meaningless zeros"0"
%i Signed decimal integer(together with%d Same)
%u Unsigned decimal integers
%o Octal integers0123
%x(%X)     hexadecimal integer0f(0F)          0x1234
%p Pointer
%s String

symbolize

  Left-aligned: "-" "%-2s" means left-aligned by 2 digits.
  Right-aligned: "+" "%+2s" for right-aligned 2 bits

specification

[Flag] [Minimum width of output] [...]. Accuracy] [Length] Type

"%-md" : Left-aligned, if m is less than actual, output as actual.
"%" : output m bits, take the string (from the left) n bits, left complementary space, when n>m or m omitted when m=n
"%": outputs a floating point number, m is the width, n is the number of digits to the right of the decimal point.
"%3.1f" : Input 3852.99 Output 3853.0