0

I am new to C and writing a simple program to display the byte representation of data. When I compile, the Command Prompt screen flashes for 1/2 a second and disappears. In simpler words, the output doesn't show up. Following is my code:

#include <stdio.h>
typdef unsigned char* pointer;

void show_int(int);
void show_bytes(pointer, int);
int main()
{
show_int(100);
}

void show_int(int x)
{
show_bytes((ponter) &x, sizeof(int));
}

void show_bytes(pointer start, int len)
{ 
  int i;
  for(i=0;i<len;i++)
  {
    printf("0x%p\t0x%.2x\n", start + i, start[i]);
  }
}
jmishra
  • 2,086
  • 2
  • 24
  • 38

3 Answers3

3

The program likely is displaying your output, then quickly exiting. You'll want to delay before exiting. If you're on Windows, use Sleep(milliseconds); if you're on Unix, use sleep(seconds). Note not only the different units, but the different capitalization.

Cannoliopsida
  • 3,044
  • 5
  • 36
  • 61
  • Thanks! I had to `#include ` to make `Sleep(..)` work and that seems silly (I'll be careful to call it that because I am new to C) since I am working on a Windows machine already – jmishra Jun 16 '12 at 02:55
  • 1
    Since you're using the C runtime for input/output, you could add call to getchar() at the end of main to prevent the program from ending before you've seen the output. Press Enter/Return to end. – Robin Caron Jun 16 '12 at 03:46
1
show_bytes((ponter) &x, sizeof(int));

I guess that shall be (pointer) instead?

Follow Akroy's advice too. Unless you're using an IDE like CodeBlocks or something so.

TheNavigat
  • 864
  • 10
  • 30
1

firstly, show_bytes((ponter) &x, sizeof(int)); is right!

secondary, I think this question is that the output box disappear quickly on Windows console, if I am right, you can run your program in cmd.

For example, your program is in d:\project\test.exe

start -> run -> cmd -> 

Step 1: change dir to your work place,

cmd:\ cd /d d:\project\

Step 2: execute it

cmd:\test.exe

or you can execute it directly with full path: cmd:\d:\project\test.exe.

Mysticial
  • 464,885
  • 45
  • 335
  • 332