I am trying to post this to the C board. If I am in the wrong place, or this is not an appropriate question please let me know so can delete or move this. Thank you very much.
I am learning C as my first programming language as I want my philosophical foundations to be based on C and how it works. I am running a test to see how scanf() works, and it is not working as I would like it to but, most importantly, I can’t figure out why. If I replace scanf() with cs50’s get_int it works as expected. With scanf() it says I entered 0. With get_int it displays the value entered by input.
#include <cs50.h>
#include <stdio.h>
// test result: scanf does not do what I want here.
int main()
{
int num1;
num1 = scanf("enter a number: ");
printf("you entered %d n", num1);
return 0;
}
This code compiles and runs, however it printf()’s 0 rather than the input that is given.
#include <cs50.h>
#include <stdio.h>
// test result: scanf does not do what I want here.
int main()
{
int num1;
num1 = get_int("enter a number: ");
printf("you entered %d n", num1);
return 0;
}
This code also compiles, however it prints the correct value as given by the input.
I also tried the following;
#include <cs50.h>
#include <stdio.h>
// test result: scanf does not do what I want here.
int main()
{
int num1;
num1 = scanf("enter a number: ", &num1);
printf("you entered %d n", num1);
return 0;
}
but it throws the following error in terminal:
test.c:7:38: error: data argument not used by format string [-Werror,-Wformat-extra-args]
7 | num1 = scanf(“enter a number: “, &num1);
I even tried casting num1 to num2 and printf() num2 but that threw an error as well.
This is just a test. I would like to understand what is happening and why it works this way. Obvs I can get it to print the proper result in such a situation, but I need to understand how scanf() works and I can’t figure this out.
Again, if this is not the appropriate board for this question please let me know so I can delete or move it. I am trying to post to the C board. Thank you very much for your time, and I hope you be well and truly blessed.
simspawn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
9
From cppreference:
int scanf( const char *restrict format, ... );
where the return value is
the number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.
and not the input value you expect.
A correct use would be:
puts("enter a number: ");
int input_value;
int num_arg = scanf("%d", &input_value);
if (num_arg > 0) {
printf("you entered %d number(s): %dn", num_arg, input_value);
} else {
puts("some error occurred");
}
4
The issue with your code lies in the misuse of scanf()
:
-
Incorrect Format String:
scanf("enter a number: ");
is invalid.scanf()
expects a format specifier (e.g.,%d
for integers) and does not display prompts. -
Missing Pointer:
scanf("%d", num1);
is incorrect becausenum1
is not a pointer. You must pass&num1
.
#include <stdio.h>
int main() {
int num1;
printf("Enter a number: ");
if (scanf("%d", &num1) == 1)
printf("You entered %dn", num1);
else
printf("Invalid input.n");
return 0;
}
1