In this code, why is scanf() not setting the input to the variable properly?

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.

New contributor

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():

  1. Incorrect Format String:
    scanf("enter a number: "); is invalid. scanf() expects a format specifier (e.g., %d for integers) and does not display prompts.

  2. Missing Pointer:
    scanf("%d", num1); is incorrect because num1 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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật