-1

I'm fairly new to C programming, so I figured I'd try writing a simple program to print two int numbers. I prompt the user for both numbers, and then just print both using printf

However, upon running the program, I get a result which is really bizarre.

For instance...

Enter first int: 5
Enter second int: 3
First int: 2130567168
Second int: 2686756

My code is below...

#include <stdio.h>

int main()
{
    int x, y;

    printf("Enter first number: ");
    scanf("%i", x);

    printf("Enter second number: ");
    scanf("%i", y);

    printf("%i\n%i%\n",x,y);

    return 0;
}
Delfino
  • 967
  • 4
  • 21
  • 46

3 Answers3

5

This is because you forgot the & in the scanf statement. So change it to this:

scanf("%i", &y);
scanf("%i", &x);
          //^ See here

Also you have one % too much in your printf statement. Because % is for format specifiers, so if you want to print the symbol % you have to write it 2 times:

printf("%i\n%i%\n",x,y);
            //^ Is a format specifier so if you want to print the symbol, write it 2 times

Side note:

If you use the specifier %i in your scanf statement and you enter something like this: 035 the output would become 29, because it would be interpreted as octal number. So if you don't want that you can change the specifier to %d and 035 becomes 35

Rizier123
  • 58,877
  • 16
  • 101
  • 156
2

Change the code like this.

scanf("%i", &y);
scanf("%i", &x);

While storing the value to the variable you have to give the address of that variable. If you are using the arrays you don't need to give that. In printf statement , use the needed specifiers.

printf("%i\n%i\n",x,y);
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
2

You forgot the '&' in the scanf statement:

scanf("%i", &y);
scanf("%i", &x);
      //^ this