Thread: Inexpected output

  1. #1
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Inexpected output

    Unexpected output

    #include<stdio.h>
    main()
    {
    int a,b[100];


    a=60;


    printf("size of 01 is %d\n",sizeof(01));
    printf("size of 0x11 is %d\n",sizeof(0x11));

    printf("%d characters print\n",printf("%d integers read\n",scanf("%d%d",&a,&b)));
    printf("now %d charas print \n",printf("%d",a));
    printf("%d characters in hello world \n",printf("hello world"));
    }
    /*output
    size of 01 is 4
    size of 0x11 is 4
    5
    6
    2 integers read
    17 characters print
    5now 1 charas print
    hello world11 characters in hello world
    Press any key to continue . . .
    */
    my question is why 5 is printed in the line "now 1 charas print"
    and why size of 0x11 and 01 are 4.....
    I know since i am using g++ size of integer is 4
    ...Since i cannot edit the title the spelling mistake remains there...sorry for that.....
    Last edited by linuxlover; 03-25-2011 at 10:33 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    47th post? Still no code tag?
    Why are you using g++. It's C++ compiler.


    Code:
    int a,b[100];
    is b an int?
    gcc -Wall will give you warning for that. Always enable warning and pay attention to them! from now on!
    Btw, you could write clearly.
    Code:
    int ret = scanf(...);
    ret = printf( ... );
    ret = printf("%d printed\n",ret);
    ...
    Last edited by Bayint Naung; 03-25-2011 at 10:43 PM.

  3. #3
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by Bayint Naung View Post
    47th post? Still no code tag?
    Why are you using g++. It's C++ compiler.


    Code:
    int a,b[100];
    is b an int?
    b is an integer array ...am i right?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Still don't get it? Pay attention to the line.
    Code:
    scanf("%d%d",&a,&b);
    Enable compiler warning.

  5. #5
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    scanf("%d%d",&a,&b)

    ok..
    is scanf("%d%d",&a,*b) correct since b contains
    &b[0]
    Last edited by linuxlover; 03-25-2011 at 10:49 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
       b = &b[0]          // in expression
      *b = *&b[0];
       *& cancel
      *b = b[0];
    So you are getting first element of b. which is of type int. Why don't you go and try out and listen to what compiler says?

  7. #7
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by Bayint Naung View Post
    Code:
       b = &b[0]          // in expression
      *b = *&b[0];
       *& cancel
      *b = b[0];
    So you are getting first element of b. which is of type int. Why don't you go and try out and listen to what compiler says?
    So i maywrite scanf("%d%d",&a,b);
    here i am not getting any warnings...but same error remains......
    Last edited by linuxlover; 03-25-2011 at 10:56 PM.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    printf("now %d charas print \n",printf("%d",a));
    There you are printing a.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

Tags for this Thread