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);