overexchange overexchange - 1 year ago 96
C Question

Displaying character array

In the below code,

int main(){
char string[] = {1, 2, 3};
char* my_pointer = string;
printf("%c", *my_pointer);

Am expecting the ASCII character
to be displayed on screen.

ASCII character
with binary representation as

Answer Source

You need to give it characters when initializing:

char string[] = {'1', '2', '3'};

or you can initialize using a string

char string[] = "123";

When you use numbers like

  char string[] = {1, 2, 3};

it will print the ASCII character corresponding to the number 1 (which is most likely not generating any output).

If you want to initialize using numbers, you must do:

  char string[] = {49, 50, 51};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download