0

I'm trying understand shifting and masking and I found this code from here...

https://answers.yahoo.com/question/index?qid=20120812164745AAn4i6L

When I run the program, I have to enter an input before I see "enter a digit...". The code seems ok to me so not sure why the print statement doesn't appear first. Am I missing something? I'm fairly new to C but it looks ok to me

#include <stdio.h>

int main(void) {

    unsigned int hex, h1, h2, h3, h4;
    printf("enter a eight-digit hex value: \n");
    scanf("%x", &hex);
    /* bit masking */
    h1= hex & 0x000000ff;
    h2= hex & 0x0000ff00;
    h3= hex & 0x00ff0000;
    h4= hex & 0xff000000;
    /*shift bits to right as indicated */

    printf("%x\n", h1);
    printf("%x\n", h2);
    printf("%x\n", h3);
    printf("%x\n", h4);

    h2 >>= 8;
    h3 >>= 16;
    h4 >>= 24;
    /* print output */
    printf("\n 0x%08x is composed of %02x %02x %02x %02x\n",
           hex, h4, h3, h2, h1);
    return 0;
}
ajay
  • 9,402
  • 8
  • 44
  • 71
cpd1
  • 777
  • 11
  • 31
  • I compiled and ran it and it works fine for me (prompts for number and then prints output). – James King Apr 13 '14 at 03:20
  • You know what - when I run it in a command window it seems to be ok. In eclipse, it runs differently. I'm used to using Intellij so not sure if I'm best off using a command window instead of Eclipse's console – cpd1 Apr 13 '14 at 03:21
  • 1
    If you're using Eclipse, read [printf not printing on console](http://stackoverflow.com/questions/13035075/printf-not-printing-on-console). – PM 77-1 Apr 13 '14 at 03:22
  • What compiler are you using? Any compiler flags? I too compiled this and it ran fine/as expected. This indicates it's not a code problem (unless the code you're running doesn't match what you included above), but rather a problem with your environment i.e. compiler or OS. – alpartis Apr 13 '14 at 03:23
  • @alpartis - This is apparently a [bug in Eclipse](https://bugs.eclipse.org/bugs/show_bug.cgi?id=173732). – PM 77-1 Apr 13 '14 at 03:25
  • Yeah, looks like it is. Also marked as "Wont fix" which is weird. I guess I'll just use a command window to run it. – cpd1 Apr 13 '14 at 03:26
  • Try `fflush(stdout);` after `printf` statement. It may not be a good solution but it might be a quick fix. – Visal Chhourm Apr 13 '14 at 03:28
  • @PM77-1 - yep. But then I ask myself, "What good is a program running inside an IDE? Real users never execute programs in an IDE, so why try to learn that way?" Maybe as a productive gray-beard, I'm not supposed to understand. – alpartis Apr 13 '14 at 03:29
  • @alpartis - IDEs is a way to increase productivity even further for those who know how to use them. They have a multitude of useful plug-ins. As any kind of software they can have bugs. – PM 77-1 Apr 13 '14 at 03:34
  • "What good is a program running inside an IDE?" -- It's good to be able to run your program inside an IDE when developing and debugging it -- IDEs provide breakpoints and numerous other tools. "Real users never execute programs in an IDE" -- yes, they don't develop and debug programs, but that's irrelevant. "so why try to learn that way?" -- it's not about learning, it's about developing. "Maybe as a productive gray-beard" -- as opposed to what? "I'm not supposed to understand." -- Maybe you're not willing to. – Jim Balter Apr 13 '14 at 03:40

1 Answers1

1

By default, stdout is buffered if it isn't directed to a terminal. If you're running inside an IDE, it might not properly emulate a terminal. So either explicitly make stdout unbuffered or line-buffered with setbuf, or call fflush(stdout) after the printf that you need to be seen before input is read.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
  • `stdout` is line buffered when it refers to a terminal. The man page of `setbuf` says `If a stream refers to a terminal (as stdout normally does) it is line buffered.` – ajay Apr 13 '14 at 04:02
  • "stdout is line buffered when it refers to a terminal" -- I know that and didn't say otherwise, and it really isn't germane. The problem here is that stdout is getting buffered rather than line-buffered or unbuffered (as stderr is). – Jim Balter Apr 13 '14 at 04:18