473,652 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1667
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**********@w ipro.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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**********@w ipro.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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<iostre am>

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**********@w ipro.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
3425
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 print 2.
6
2927
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
193
9519
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 speak only of features in the spirit of C; something like object-orientation, though a nice feature, does not belong in C. Something like being able to #define a #define would be very handy, though, e.g: #define DECLARE_FOO(bar) #define...
31
2610
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
1819
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
2864
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 byte); int main (int argc, const char * argv)
8
1482
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
1599
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
20
2222
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 ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
8367
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8703
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.