Home > OS >  print char pointer value in c
print char pointer value in c

Time:02-10

-: was trying to learn pointers in c :-

I couldn't print the value at the character pointer, rest of the program works fine. It just prints a blank space, didn't get any error though.

#include<stdio.h>

void main() {
    int num, *num_ptr;
    char ch, *ch_ptr;

    printf("Enter a number and a single character : ");
    scanf("%d%c",&num,&ch);

    num_ptr = &num;
    ch_ptr = &ch;

    //printf("\ncontent at num_ptr = %p \n",num_ptr);
    //printf("content at ch_ptr = %p\n",ch_ptr);
    //printf("value of the content at num_ptr = %d\n",*num_ptr);

    /* error part */
        printf("value of the content at ch_ptr = %c\n",*ch_ptr);
    /* error part */

    //printf("\n");
    //printf("num_ptr   2 = %p\n",num_ptr 2);
    //printf("ch_ptr   2 = %p\n",ch_ptr 2);
    //printf("\n");
    //printf("num_ptr   1 = %p\n",  num_ptr);
    //printf("num_ptr - 1 = %p\n",--num_ptr);
    //printf("ch_ptr   1 = %p\n",  ch_ptr);
    //printf("ch_ptr - 1 = %p\n",--ch_ptr);
    //printf("\n");
    //printf("num_ptr   5 = %p\n",num_ptr 5);
    //printf("ch_ptr - 5 = %p\n\n",--ch_ptr-5);
}

program output

CodePudding user response:

Your input is 21 t, which means that scanf will read 21 into num, and the in-between whitespace into ch, which is what you specify in the first argument to scanf. You either separate the format specifiers by a blank space, i.e. %d %c, or enter the input without the whitespace, hence 21t.

  • Related