EmbLogic's Blog

unexpected value printed by gcc. why ?

1 #include<stdlib.h>
2
3 int main()
4 {
5 int de=1234;
6 float j=45.1234;
7 char ch= ‘s’;
8 double s= 3.1415E+5;
9
10 printf(“de= %d \n”,de);
11 printf(“j= %f\n”,j);
12 printf(“ch= %c \n”,ch);
13 printf(“s= %e \n”,s);
14 return 0;
15
16 }
17
18 /*predicted output
19 *
20 * de= 1234
21 * j= 45.123400
22 * ch= s
23 * s= 3.141500e+05
24 *
25 * ON TERMINAL
26 *
27 * [root@rahulkumar c]# gcc formatprint.c
28 * formatprint.c: In function ‘main’:
29 * formatprint.c:10:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
30 * [root@rahulkumar c]# ./a.out
31 *
32 * OUTPUT
33 *
34 * de= 1234
35 * j= 45.123402
36 * ch= s
37 * s= 3.141500e+05
38 */
39
my question is, if assign or initialise the the value of float variable like above underlined then it take the 22 * ch= s
23 * s= 3.141500e+05
24 *
25 * ON TERMINAL
26 *
27 * [root@rahulkumar c]# gcc formatprint.c
28 * formatprint.c: In function ‘main’:
29 * formatprint.c:10:3: warning: incompatible implicit           declaration of built-in function ‘printf’ [enabled by           default]
30 * [root@rahulkumar c]# ./a.out
31 *
32 * OUTPUT
33 *
34 * de= 1234
35 * j= 45.123402
36 * ch= s
37 * s= 3.141500e+05
38 */
39
my question is, if assign or initialise the the value of float variable like above underlined marked red then it take the garbage value at output shown underlined bold. assigning variable in float with 0.0 also give garbage value at other precision value.

One Response to unexpected value printed by gcc. why ?

  1. Manoj says:

    Inside the machine the floating points are calculated in terms of CONM called computer oriented numarical methods…
    .eg.. simpson’s 1/3 rule, trapezodial rule, etc.
    .
    .
    These rules are not 100% correct. There are some errors in them. Thats why the difference between the human interpration of a floating number and computer interpratation of a floating point number has a little differnce of 0.000001 / 2
    .
    .
    This difference is called machine epsilon.
    .
    .
    find more on net.
    .

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>