-1

Im following a youtube tutorial by Bucky in C programming. Every time i use scanf and enter the input i get an error pop up. i typed these exact same codes from the video tutorial but mine does not work.

HERE'S THE CODE:

int main()
{
int age;

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

if (age>= 18){
printf("You may enter this website!");
}
if(age<18){
printf("nothing to see here!");
}
return 0;
}

It only works after compiling and running. But after i input the age, an error window pops up and says "A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available."

I'm sure the codes are correct but What is causing this? Help me please so i can move forward.

oscar
  • 1
  • 1

1 Answers1

0

You placed quote in incorrect place in scanf:

scanf("%d, &age");

should be:

scanf("%d", &age);

C language is limited in a ways to implement functions with variable numbers of arguments (which scanf is, since it's can read variable number of variables from terminal), so this is why such errors are possible.

Modern compilers will warn you about incorrect scanf usage, e.g. GCC will give a warning:

1.c: In function ‘main’:
1.c:8:1: warning: format ‘%d’ expects a matching ‘int *’ argument [-Wformat=]
 scanf("%d, &age");
 ^

By the way, your example misses

#include <stdio.h>
rutsky
  • 3,900
  • 2
  • 31
  • 30
  • wow you're right and it worked!! i think i need some sleep. thanks a lot, i would upvote you but it wont let me. thanks again – oscar Mar 07 '15 at 21:20