On 3/10/2014 3:20 PM, SAHIL MAHLA wrote:
> #include <stdio.h>
> void incme(double *p)
> {
> *p += 1;
> }
>
> int main(void)
> {
> int i = 1;
> double j=i;
> incme(&j);
> printf("%d,%d",i,j);
> return 0;
> }
>
> output is -- 1,0
That is one possible outcome. (Since the program has what
is called "undefined behavior," *all* outcomes are possible.)
> it should be -1,2
I can understand why you'd expect "2", but I cannot imagine
why you'd expect "-1".
> plz explain
The "%d" specifier converts `int' values, but `j' is not
an `int'. To convert a `double' value you can use "%g" (there
are other candidates, too).
--
Eric Sosman
(E-Mail Removed)d
> #include <stdio.h>
> void incme(double *p)
> {
> *p += 1;
> }
>
> int main(void)
> {
> int i = 1;
> double j=i;
> incme(&j);
> printf("%d,%d",i,j);
> return 0;
> }
>
> output is -- 1,0
That is one possible outcome. (Since the program has what
is called "undefined behavior," *all* outcomes are possible.)
> it should be -1,2
I can understand why you'd expect "2", but I cannot imagine
why you'd expect "-1".
> plz explain
The "%d" specifier converts `int' values, but `j' is not
an `int'. To convert a `double' value you can use "%g" (there
are other candidates, too).
--
Eric Sosman
(E-Mail Removed)d
Aucun commentaire:
Enregistrer un commentaire