Search code examples
cscanf

The scanf() function for age doesn't work


#include <stdio.h>

int main()
{
    int age;

    printf("How old are you?");
    scanf("\d", &age);

    printf("\nYou are %d years old", age);

    return 0;
}

I think the ampersand in the scanf() function doesn't work. Is there a way to solve this?


Solution

  • You just wrote the wrong format of scanf() function. there should be %d instead of \d because for taking input inside scanf() you have to use %.

    #include <stdio.h>
    
    int main() {
      int age;
    
      printf("How old are you?");
      scanf("%d", &age); // change **%** instead of **\**
    
      printf("\nYou are %d years old", age);
    
      return 0;
    }