1
#include<stdio.h>

void function(void);


void main (void)
{
    int number;
    printf("call? yes(1) no(-1)");
    scanf("%d", &number);
    while(number>=0)
    {
        function();
        printf("call? yes (1) no (-1)");
        scanf("%d", &number);

    }
}


void function(void)
{
    static int multiple = 2;
    printf("%d", &multiple);
    multiple = multiple*2;

}

hi this is my code for finding 2 multiplied by a number . i wanted it to print out 2,4,8,16 etc. however im getting some big number when i run this code. i just started learning c so any comment is appreciated

gsamaras
  • 71,951
  • 46
  • 188
  • 305
andy kim
  • 33
  • 1
  • 7
  • 2
    `printf("%d", &multiple);` Remove `&` --> `printf("%d\n", multiple);` – BLUEPIXY Sep 06 '17 at 07:39
  • 1
    You can use -g flag in gcc in order to debug why your getting big number. Debugging your program will help you to know what's causing the problem..by the way you don't use & printf for static int type. – danglingpointer Sep 06 '17 at 07:40
  • Start with `gcc -Wall -Wextra` - that will help diagnose the mistake. – Toby Speight Sep 06 '17 at 11:49

2 Answers2

3

In the printf(), the address of multiple is printed. Change the printf("%d", &multiple); to printf("%d\n", multiple);.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nerdy
  • 1,016
  • 2
  • 11
  • 27
2

Remove the & from your print.

Change this:

printf("%d", &multiple);

to this:

printf("%d", multiple);

PS: What should main() return in C and C++?

gsamaras
  • 71,951
  • 46
  • 188
  • 305