Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't scanf read the value correctly?

Tags:

c

unix

printf

scanf

Can someone tell me what is going wrong here?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define ERROR 0
#define MAX_INPUT_LINE 80
#define print(x) {fputs(x,stdout);}
#define SUCCESS 1

int main (long argc, char *argv[])
{
   int mode;
   printf("1 for hexidecimal or 2 for binary");
   scanf("%d", mode);

   printf("\n\n\nThe value of mode is %d\n", mode);
   return 0;
}

When I enter 2 for binary, I get this:

The value of mode is 2665564

Obviously I should get 2, what am I doing wrong?? Is it my compiler, is it bevcause I am using Cygwin? Why is mode not 2??

like image 639
mosawi Avatar asked Aug 14 '26 15:08

mosawi


1 Answers

your scanf is wrong. should be:

scanf("%d", &mode);

The & tells the compiler to send scanf a pointer to mode (i.e. its address) and not the actual value of mode. That way scanf can put update mode with the new scanned value.

like image 56
Nathan Fellman Avatar answered Aug 16 '26 03:08

Nathan Fellman