% + Difference [C / C ++] printf () and the 2f% .2f of

problem:

Discussed printf () in +%. 2f and both .2% Degree of "+" differences, i.e.,

printf("%+.2f",x);
printf("%.2f",x);

Code:

int main()
{
    double a=0.004;
    double b=0.005;
    double c=-0.004;
    double d=-0.005;
    printf("a=%.2f  b=%.2f  c=%.2f  d=%.2f\n",a,b,c,d);
    printf("a=%+.2f  b=%+.2f  c=%+.2f  d=%+.2f\n",a,b,c,d);
    return 0;
}

result:
scanf(%+.2f)

analysis:
If the number is positive, i.e. the output of the +
If the number is negative, i.e., the output of the first - and then outputs the absolute value of number

Published 82 original articles · won praise 1 · views 1685

Guess you like

Origin blog.csdn.net/qq_34451909/article/details/104834783