unexpected output in C program

Pages: 12
I am new to C programming i am learning pointers so i wrote this, i expected the value of c to be 25.345 but it is showing some other big number pls explain...

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
main( )
{
    float a = 25.345, c ;
    long unsigned *b ;

    b = &a ;
    c = *b;

    printf ("%f %lu %f", a, b, c);
}
You are adressing variable of one type through pointer to another type. THis is Undefined Behavior and anything can happen.
I also tried float *b as pointer variable still getting weird output.
At least tell me is there any way to view the float value stored at any address.
Everything should work.
http://ideone.com/1UmY8q
I also wanted to know the address so i did float *b and %d for b in printf and it worked thanks for help :)
Using %d for pointers is incorrect. You got lucky and your program was compiled in 32bit mode. Next time you might be not so lucky. use %p format specifiers for pointers.
so can i use %p for address anywhere irrespective of the type of pointer variable.
Yes, you can.
i thought it would show 16

1
2
3
4
5
6
7
8
9
#define PRODUCT(x) ( x * x )
main( )
{
      int i = 3, j ;

      j = PRODUCT( i + 1 ) ;

      printf ( "\n%d", j ) ;
}


but it shows 7 pls explain
but it shows 7 pls explain

A good reason not to use macros.

j = PRODUCT( i + 1) expands to: j = i + 1 * i + 1
Last edited on
Or, at least, use them properly:
1
2
3
4
//Remember, parentheses around each argument 
//and parentheses around whole expression.
#define PRODUCT(x) ( (x) * (x) )
//Remember to never use that macro with operations with side effects, like i++ 
ok so its 3+(1*3)+1 thanks :)
can array be declared like this
1
2
3
4
5
int size ;

scanf ( " %d ", &size ) ;

int arr[size] ;
You can in C99 and later. Is not allowed in C90.
I tried this code and got unexpected output can somebody pls explain

1
2
3
4
5
6
7
8
#include <stdio.h>
main( )
       {
               char str1[ ] = {'h','e','l','l','o'};
               char str2[ ] = {'s','t','r','i','n','g'};
               printf ( "\n%s", str1 ) ;
               printf ( "\n%s", str2 ) ;
       }


output:

hello( (
stringhello( (
You marked this topic as solved, do you still need help?
yes same thread new problem
printf %s specifier expect a c-string: a null-terminated character array. You gave it a character array which is not null terminated. UB.
thanks :)
Pages: 12