Every row is like a mountain!% C and% s, C language symbols that only programmers understand!

Time:2021-10-20

Original link: https://blog.csdn.net/zhgl7688

This article mainly introduces the difference and division between% C and% s in C language. I hope it will be helpful to you. Let’s follow Xiaobian and have a look!

 

%The C format corresponds to a single character, and the% s format corresponds to a string.

Example:

char a;

char b[20];

scanf(“%c”,&a); // Only one character can be entered.

scanf(“%s”,b); // You can enter a string of no more than 20 characters.

%The corresponding type of C is char, and the corresponding type of% s is char *, that is, string

When used as input, both parameters should be passed to char *

%C the input function will assign a value to only one byte space. And% s will assign a value until a blank character is encountered in the input

When used as output,% C passes char type and outputs a character.% s passes char * type parameter and outputs until \ 0

%C can only output or input one character,% s outputs a string of characters and char a; string s;

Scanf (‘% C’, & A) when entering; Here & can’t be less

Scanf (‘% s’, s); There can be no & sign here

conversion specifier

%A (% a) floating point numbers, hexadecimal digits, and P – (P -) notation (C99)

%c       character

%d       Signed decimal integer

%f       Floating point numbers (including float and doulbe)

%e(%E)     Floating point index output [e – (E -) notation]

%g(%G)     Floating point numbers do not show meaningless zero “0”

%i       Signed decimal integer (same as% d)

%u       Unsigned decimal integer

%o       Octal integer   e.g.   0123

%x(%X)     Hexadecimal integer 0f (0f)   e.g.   0x1234

%p       Pointer

%s       character string

  %%      “%”

Supplementary knowledge: scanf (“% C”, & C) eats carriage return or space characters in C language

2 ` sign

Left alignment: “-” e.g. “% – 20s”

Right alignment: “+” e.g. “% + 20s”

3. Format string (format)

[flag] [output minimum width] [precision] [length] type

“% – MD”: left aligned. If M is less than the actual value, it will be output according to the actual value.

‘% m.ns’: output m bits, take n bits of string (from left) and fill in spaces on the left. When n > m or m is omitted, M = n

‘% m.nf’: output floating-point number. M is the width and N is the digit to the right of the decimal point

“%3.1f” :   Input 3852.99 output 3853.0

Supplementary knowledge: scanf (“% C”, & C) eats carriage return or space characters in C language

I’ll stop talking nonsense. Let’s look at the code directly~

#include

void main(){

int a;

char b;

printf(“input a integer\n”);

scanf(“%d”,&a);

//We need to absorb carriage returns here. There are two methods below. The second one I don’t understand. Who can change the code and tell me how to use the second one

printf(“input a char\n”);

scanf(“%c”,&b);

printf(“%d,%c\n”,a,b);

}

/*

(1) Clear with getchar()

(2) Limit with ‘% 1s’

(3) “Eat” with spaces or “% * C” in the format string

(4) Clear all the rest with the function fflush (stdin)

*/

The second method is used below

#include

int main(){

int a;

char b;

printf(“input a integer\n”);

scanf(“%d”,&a);

//We need to absorb carriage returns here. There are two methods below. The second one I don’t understand. Who can change the code and tell me how to use the second one

printf(“input a char\n”);

scanf(“%1s”,&b);

printf(“%d %c\n”,a,b);

system(“pause”);

return 0;

}

Environment (DEV C + +)

test result

 

You can see that the value of a is abnormal. Why?

This should have something to do with the compiler because there is a problem with the input mode of% 1s.

There are two solutions:

First:

char b;

int a;

Change the declaration of these two variables, ha ha, as the teacher said before. But I feel that it is not the solution. If there are other variables, will this method harm others.

Second:

#include

int main(){

int a;

char b[2];

printf(“input a integer\n”);

scanf(“%d”,&a);

//We need to absorb carriage returns here. There are two methods below. The second one I don’t understand. Who can change the code and tell me how to use the second one

printf(“input a char\n”);

scanf(“%1s”,&b);

printf(“%d %c\n”,a,b[0]);

system(“pause”);

return 0;

}

B sound clearly in the form of character creation, ha ha, this is good….

The result is:

 

The above detailed explanation of the difference and division between% C and% s in C language is all the content shared by Xiaobian. If you have any questions, please leave a message in the comment area.

 

 

Finally, if you want to be a programmer and want to master programming quickly, join quicklyLearn Penguin circle

There are senior professional software development engineers to answer all your doubts online ~ introduction to programming language “so easy”

Programming learning books:

 

Programming learning video: