-1

This is my code, and I still keep on looking what's wrong, I'm a beginner and I want to learn:

#include<stdio.h>
int main()
{
    int a;

    printf("Input: ");
    scanf("%d", &a);
    printf("\n%d", &a);
    a+=2;
    printf("\n%d", &a);
    a+=4;
    printf("\n%d", &a);
    a+=2;
    printf("\n%d", &a);
    return 0;
}

Here is the output:

Input: 10

-1078169908
-1078169908
-1078169908
-1078169908
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
jack1
  • 69
  • 1
  • 4

4 Answers4

6

You don't need to pass the address of a to printf() to print the content.

Change

printf("\n%d", &a);

to

 printf("\n%d", a);

Also, you should be checking the return value of scanf(). In case scanf() fails, you'll be invoking undefined behavior by accessing an unitialized local variable.

That said, int main() should at least be int main(void) to conform to the standards.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    plus one for the undefined behaviour. –  Jan 16 '16 at 13:48
  • 1
    @Ehsan Don;t say like that. It sounds like the answer itself has UB. Cheers ;) – Sourav Ghosh Jan 16 '16 at 13:49
  • 1
    I had a "learning experience" yesterday: "An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters." C11 §6.7.6.3 14 or IOWs `int main()` and `int main(void)` are the same. So "int main() should at least be int main(void)" is not needed. – chux - Reinstate Monica Jan 16 '16 at 15:47
  • @chux The problem here is that the empty list isn't in *a function declarator that is part of a definition of that function*. Note how [this example](http://ideone.com/wpfv9a) compiles, yet [this one](http://ideone.com/UY4QOf) does not? – autistic Jan 17 '16 at 01:25
3

Just to add some points to others' answers :

& operator gives the address of a variable

so when you say :

scanf("%d",&a);

You actually tell the compiler to pass the address of variable a to scanf function in order to scanf changes the value of variable a because if you call scanf like this :

scanf("%d",a);

You are just passing the value of a to scanf so it can't change the value of a.

But when you use printf, function printf need not to change the value of variable a so it's right to invoke printf like this and it will compile successfully:

printf("%d",a);

You can read more here ( Actually this syntax is used in some languages )

Community
  • 1
  • 1
  • I think you do a fine job at explaining this, and this answer would be better without the link to a subtly different concept... – autistic Jan 17 '16 at 01:18
0

It should be like this :-

#include <stdio.h>

int main()             
{
           int a;

           printf("Input: ");
           scanf("%d", &a);
           printf("\n%d", a);
           a+=2;
           printf("\n%d", a);
           a+=4;
           printf("\n%d", a);
           a+=2;
           printf("\n%d", a);
           return 0;
}

printf () function is used for printing message on screen that is why we don't use & operator in printf () function & operator is used when you take inputs from user like in scanf () function so, what you have to do is to remove & in every printf () function rest is fine.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
0

Few tips for you:

  • When you use scanf () to get value from user and store it, always use like this: &a

  • When you print any value using printf () never put &a. Use only a.

So remove &a from printf () and write a

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Team Work
  • 35
  • 1
  • 7
  • Mostly correct. `scanf` takes a pointer, so a pointer to `char` should be passed directly, not with `&`. And, of course, you may actually want to print a pointer value, in which case you might use `&` in `printf`. – Tom Zych Jan 16 '16 at 19:25
  • The first tip is invalid. In the case of `%s`, "the corresponding argument shall be a pointer to the initial element of a character array large enough to accept the sequence and a terminating null character". `char str; scanf("%s", &a);` is silly as `&a` isn't used to express a pointer to the first element of an array larger than 1 character, and the only string that array can store is an empty one. Using `char a[N]; scanf("%s", &a);` is even worse, because `&a` is not "a pointer to the initial element", as your compiler likely warns. **In summary, `scanf("%s", &a);` is *ALWAYS* a mistake.** – autistic Jan 17 '16 at 01:15