0

It compiles correctly, but when I run it and input a number it outputs some random string of seven numbers. I have no idea what to do with this. For example I put in 5 and it gives me 2751724 then I do it again and get 3537324. I don't know if I have some settings off or what but this seems pretty simple to me.

#include "stdafx.h"
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int number;

    printf("Input number: ");
    scanf_s("%d", &number);
    printf("Number is: %d \n", &number);

    return 0;
}
7
  • 3
    just print number not &number, &number is the adresss in memory where number is Commented Feb 16, 2015 at 1:15
  • You can printing the wrong thing in printf("Number is: %d \n", &number);. Hint: what does & do here? Commented Feb 16, 2015 at 1:15
  • Oh thanks to the both of you. I got the syntax messed up in my head. Commented Feb 16, 2015 at 1:18
  • I'm sure this is a duplicate but search doesn't find anything since they have all been downvoted to death :) Commented Feb 16, 2015 at 1:29
  • 2
    when calling any of the scanf family of functions, always check the returned value to assure the input/conversion operation was successful Commented Feb 16, 2015 at 1:31

2 Answers 2

4

Using the & operator gets the address of a variable in memory (aka a pointer). By using this functions can alter the value of that variable directly without having to return a value. You should read up about pointers and how to use them.

In short, printf("Number is: %d \n", number).

Sign up to request clarification or add additional context in comments.

Comments

1

when you do like this:

printf("Number is: %d \n", &number);

you print the address of number instead of the value of number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.