473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Some strange behaviour of printf.

#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?

Jul 23 '05 #1
4 1653
DeltaOne wrote:
#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
Jul 23 '05 #2
"DeltaOne" <sh**********@wipro.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com
#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.

--
John Carson

Jul 23 '05 #3

"DeltaOne" <sh**********@wipro.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
#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.


Jul 23 '05 #4
On 17 May 2005 04:48:21 -0700, DeltaOne <sh**********@wipro.com> wrote:
#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...
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dmitriy Iassenev | last post by:
hi, I found an interesting thing in operator behaviour in C++ : int i=1; printf("%d",i++ + i++); I think the value of the expression "i++ + i++" _must_ be 3, but all the compilers I tested...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
8
by: manochavishal | last post by:
Hi, I have a structure MAX_ID is 5 /**Structure for Copies*/ typedef struct NodeVideoCopy * NodeVideoCopyPtr ; typedef struct NodeVideoCopy {
31
by: gamehack | last post by:
Hi all, I've been testing out a small function and surprisingly it does not work okay. Here's the full code listing: #include "stdlib.h" #include "stdio.h" char* escaped_byte_cstr_ref(char...
8
by: siddharth.munshi | last post by:
union something { float a; int b; } u; int main() { printf("%d %d\n",&u.a,&u.b); u.a = 200;
23
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.