5 Replies - 322 Views - Last Post: 22 July 2011 - 09:54 AM Rate Topic: -----

#1 exIM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-July 11

different output of same printf statments.

Posted 22 July 2011 - 07:18 AM

#include	<stdio.h>
#include	<stdlib.h>

int main ( int argc, char *argv[] )
{
	struct strct 
	{
		char a;
		int b;
	};
	
	struct strct b;
	
	b.a = 'a';

	printf ( "%c,",b );
	printf ( "%c\n",b.a );
	//------------------------	
	printf ( "%c,%c\n",b,b.a);

	return EXIT_SUCCESS;
}
output:
a,a
a,	



i dont understand why the two output lines be different. Why?

Is This A Good Question/Topic? 0
  • +

Replies To: different output of same printf statments.

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: different output of same printf statments.

Posted 22 July 2011 - 07:19 AM

What is the output you're getting, and what is the output you're expecting?
Your code doesn't even compile for me.

This code also looks really dodgy:
printf ( "%c,",b );
What's happening behind the scenes is this:
char out;
out = *(char*)&b;
printf("%c,", out);
This could seriously mess with the output results.

This post has been edited by PlasticineGuy: 22 July 2011 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

#3 exIM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-July 11

Re: different output of same printf statments.

Posted 22 July 2011 - 07:43 AM

i though i would get
output:
a,a
a,a



but i did not, instead i get this
output:
a,a
a,



please explain me what kind of variable b is. are we not supposed to read b ?

intermediate to advanced tutorial on struct would be very helpful.
or/and
intermediate to advanced tutorial on c, (not gui tutorial, i really want to understand how c works like you explained in "behind the scene").

thanks for the help.

This post has been edited by exIM: 22 July 2011 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: different output of same printf statments.

Posted 22 July 2011 - 07:53 AM

b is a struct. You're not supposed to just read b because it's not a char; it's a struct. Passing a struct as a char will cause the function to freak out and do pointer conversions you might not want it to.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: different output of same printf statments.

Posted 22 July 2011 - 08:18 AM

Simply put you are getting the suprising result because you are performing undefined operations.

printf's %c format specifier works with chars, and it expects a char on the stack.

b is not a char. it is a struct and structs are generally padded which means that b is not the 1 byte expected by a char but probably 2, 4, or even as many as 16 bytes. try the following version of your program:
#include	<stdio.h>
#include	<stdlib.h>

int main ( int argc, char *argv[] )
{
    struct strct 
    {
        char a;
        int b;
    };
    
    struct strct b;
    
    b.a = 'a';

    //undefined behavior since b is not a char
    //printf ( "%c,",b );
    printf ( "%c\n",b.a );
    //------------------------	
    //undefined behavior since b is not a char
    //printf ( "%c,%c\n",b,b.a);
    
    printf ( "%d, %d", sizeof(b ), sizeof(b.a));

    return EXIT_SUCCESS;
}


For me the output was:
> "C:\CProjects\Forum Help\240584-different-output-of-same-printf-statments.exe " 
a
8, 1


Surprising? the struct 8 is 8 bytes long and member a only uses up 1 byte, so there are 7 bytes (well on that compiler anyway).

When you do undefined things the compiler can do anything it wants. It could contact SkyNet and start the WWIII and it would be perfectly within its rights. Undefined behavior is just that: undefined. Every compiler will do something a little different.
Was This Post Helpful? 1
  • +
  • -

#6 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1579
  • View blog
  • Posts: 3,007
  • Joined: 30-May 10

Re: different output of same printf statments.

Posted 22 July 2011 - 09:54 AM

How about getting a compiler that can tell you when you screw up over printf/scanf ?
$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:16: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘struct strct’
foo.c:19: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘struct strct’



Getting two different answers for doing "the same thing", but getting two different answers is a dead giveaway sign that you're doing something wrong.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1