Home > Back-end >  The same variable separately with printf output and several variables together with printf is not th
The same variable separately with printf output and several variables together with printf is not th

Time:09-15

Defines an int type variable k, and assign a value of 97, when used alone % c format output (printf (" % k=c \ n ", k);) Display properly for a, but the k and several other variables with a printf output, the result is changed, can't really understand why

 
#include

Int main ()
{
Float the I, j;
Int k;
Char m;

I=1.0/3.0;
J=0.33333333;
K=97;
M=111;
Printf (" I=% f \ n ", I);
Printf (" j=% d \ n ", j);
Printf (" % k=c \ n ", k);
Printf (" m=c \ % n, m);
Printf (" I=% f j k==% d % % c m=c \ n ", I, j, k, m);

return 0;
}

After the operation is such, to solve!

CodePudding user response:

I think this is a float with you j, but use % d to print on,
Print parameter is a variable, function internally by printing format to find parameters, separate print print (" % d \ n, j); When only one parameter j, even if the type does not match, also does not affect the parameters of the search (only the length of the intercept parameter different); And together to print, print search parameters, because of the type of % d and j, in the right length, interception parameters affect the next parameter search,
For example, j memory information of 01010111
According to the interception of 0101
% dLeft behind 0111
Continue to capture % c k parameter, directly will take the remaining 0111, so k is wrong
Continue to take % c m parameters, truly achieved k information, so m print out the information for a k

CodePudding user response:

Data type problems
Printf (" I=% f \ n ", I);
Printf (" j=% d \ n ", (int), j);
Printf (" % k=c \ n ", k) (char);
Printf (" m=c \ % n, m);
Printf (" I=% f j k==% d % % c m=c \ n ", I, j (int), k (char), m);

CodePudding user response:

reference 1st floor qybao response:
I think this is a float with you j, but use % d to print on,
Print parameter is a variable, function internally by printing format to find parameters, separate print print (" % d \ n, j); When only one parameter j, even if the type does not match, also does not affect the parameters of the search (only the length of the intercept parameter different); And together to print, print search parameters, because of the type of % d and j, in the right length, interception parameters affect the next parameter search,
For example, j memory information of 01010111
According to the interception of 0101
% dLeft behind 0111
Continue to capture % c k parameter, directly will take the remaining 0111, so k is wrong
Continue to take % c m parameters, truly achieved k information, so m print out the information for a k

Thank you answer
The original printf is not so simple

CodePudding user response:

reference 1st floor qybao response:
I think this is a float with you j, but use % d to print on,
Print parameter is a variable, function internally by printing format to find parameters, separate print print (" % d \ n, j); When only one parameter j, even if the type does not match, also does not affect the parameters of the search (only the length of the intercept parameter different); And together to print, print search parameters, because of the type of % d and j, in the right length, interception parameters affect the next parameter search,
For example, j memory information of 01010111
According to the interception of 0101
% dLeft behind 0111
Continue to capture % c k parameter, directly will take the remaining 0111, so k is wrong
Continue to take % c m parameters, truly achieved k information, so m print out the information for a k

Again for the great god, or on the cases, delete the last line of the printf, direct assignment and m=0, the output is m=a is how to return a responsibility? Assignment 1, 2, 3 and so on can and ASCII table, it happened that 0 wrong
  • Related