0

So this is all of my programcode:

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

typedef struct {
    unsigned char adr;
    unsigned char type;
} stru;

int main (int argc, char *argv[]){

    while(1){

    stru data;

    data.adr = 5;
    data.type = 4;

    printf("Address: %c, Type: %c \n", data.adr, data.type);

    }

    return 0;   
}

I compiled it with "gcc testprog.c -o testprog".

The Output is the following:

Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
Address: , Type:
^CAddress: , Type:
PuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTYPuTTY^C

I'm running Ubuntu 12.04 on a Virtual Box and access it via PuTTY SSH client I recently installed the 0MQ Library, but I don't think that's causing the error.

If someone has a solution to this, please let me know. Thank you.

2
  • Please don't typedef the struct.
    – user82238
    Sep 26, 2012 at 16:56
  • 1
    @BlankXavier: Please don't recommend ugliness. :)
    – cHao
    Sep 26, 2012 at 16:59

3 Answers 3

4

I think you want to use %u in your format string rather than %c.

%c interprets the input as an ASCII character value, so printing 4 or 5 with %c is going to print out something in the control character range. Printing out control characters is probably what's causing all the weird stuff at the bottom of your output.

%u expects an unsigned integer as input, and it will convert the integer to a string before printing.

I would recommend that you read a tutorial or reference on printf-style format strings to familiarize yourself with the differences between the many format specifiers.

1
  • 3
    Outputting a Ctrl-E (character code 5) is what causes the PuTTYPuTTYPuTTY junk. PuTTY replies to each Ctrl-E with its name (or with whatever other string you configure it to respond with, apparently...but the default is its name).
    – cHao
    Sep 26, 2012 at 16:54
0

The infinite loops looks very pointless.

And if you want to treat the unsigned char as a small integer, you need to print it with a suitable formatting code, like %u.

1
  • of course. the whole program is pretty much pointless. was just too figure out what I did wrong. thanks guys for the help
    – PogoMips
    Sep 26, 2012 at 17:43
0

You are currently printing the values 4 and 5 as characters with the %c specifier. Since 4 and 5 are not printable Ascii characters, the terminal doesn't display anything. If you want to see the values '4' and '5' printed, modify your code to printf("Address: %u, Type: %u \n", data.adr, data.type); since these are unsigned values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.