Some strange behaviour of printf.

D

DeltaOne

#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?
 
K

Krishanu Debnath

DeltaOne said:
#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?

Because first conv specifier expects a integer value, but var is not.
So you are invoking UB, after that anything can happen.

Krishanu
 
J

John Carson

DeltaOne said:
#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?


Because you gave it var in place of an int and thereby invoked undefined
behaviour. Serves you right.

Any function that allows a variable number of arguments of variable type is
not going to be good at keeping its data in the right boxes. It depends on
you not to feed it the wrong stuff.
 
P

Peter Julian

DeltaOne said:
#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?

Thats undefined behaviour in C++ since its not the address of &var.i that is
passed (only the type). Printf is being told to extract an integer at &var
and then another integer from the same object with complete disregard for
access specifiers.

use std::cout instead. Note that you can't output var unless you provide
operator<<(...). Thats by design.

#include<iostream>

struct Test
{
int i;
int j;
};

int main()
{
Test var;
var.i=10;
var.j=20;

// std::cout << "var = " << var; // no operator defined...
std::cout << "i = " << var.i;
std::cout << "\nj = " << var.j;

return 0;
}

result:
i = 10
j = 20

If you still need to be convinced of printf's deficiencies, consider:

#include<stdio.h>

struct Test
{
Test(int ii, int jj) : i(ii), j(jj) { }
private:
int i;
int j;
};

main()
{
Test var(10, 20);

printf("i==%d i==%d \n",var,var);

return 0;
}

result:
i==10 i==20

Again: undefined behaviour and just wrong.
 
U

ulrich

#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?

imho, you are not giving the address of var, and of var.i
to do so, you'd need to write:
printf("i==%d i==%d \n", &var, &var.i);

however, the result will still not what you expect...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top