Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:07:49+00:00 2026-06-12T21:07:49+00:00

I want to know how scanf function is implemented. (Just for fun of course)

  • 0

I want to know how scanf function is implemented. (Just for fun of course) Number of arguments is variable, so it’s certainly implemented by va_list, va_arg macros.

It also throws some warnings, when number of arguments does not match with format string. This could be done by parsing format string and comparing it with number of arguments. No magic.

The only thing that I can’t see how implemented, is type checking. When type of an argument (pointer to a data) does not match corresponding declaration in format literal, scanf produces a warning. How can one check type of data that a pointer points to?

Example:

#include<stdio.h>
int main()
{
    char buffer1[32], buffer2[32];
    int n; 
    double x;
    scanf("%s %s %d",buffer1, buffer2, &x); // warning
    scanf("%s %s %d",buffer1, buffer2, &n); // ok
}

Output:

warning: format ‘%d’ expects argument of type ‘int *’,
but argument 4 has type ‘double *’ [-Wformat]

AFAIK the C library is not a part of C language/Compiler, so there is nothing language-related in <stdio.h>. I’m assuming the warning is produced by implementation of scanf, not by compiler [?]. (Maybe using #warning)

If I want to do something similar in some code, how do I know which data type a pointer is pointing to?

Note: I have downloaded source code of GNU C library and looked at scanf.c. I can’t find my way through the very complicated code. There is a lot of #ifndef s and calls to other functions with strange names and structure…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:07:51+00:00Added an answer on June 12, 2026 at 9:07 pm

    This check is handled by the gcc compiler, specifically for scanf/printf functions.

    It’s such a common error that it’s worth adding special case code to the compiler for these functions.

    see the GCC -WFormat flag here: http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/Warning-Options.html

    -Wformat : Check calls to printf and scanf, etc., to make sure that the arguments supplied have types appropriate to the format string
    specified, and that the conversions specified in the format string
    make sense. This includes standard functions, and others specified by
    format attributes (see Function Attributes), in the printf, scanf,
    strftime and strfmon (an X/Open extension, not in the C standard)
    families.

    These checks are not implemented by all compilers, so it’s certainly not something to rely on.

    With GCC you can use Function Attributes ‘format’ and ‘format-arg’ to tell the compiler to apply the same checks to your functions.

    format (archetype, string-index, first-to-check) The format attribute
    specifies that a function takes printf, scanf, strftime or strfmon
    style arguments which should be type-checked against a format string.
    For example, the declaration:

    extern int my_printf (void *my_object, const char *my_format, ...)  
     __attribute__ ((format (printf, 2, 3)));
    

    …causes the compiler to check the arguments in calls to my_printf for consistency with the printf style format string argument
    my_format.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know the disadvantages of scanf() . In many sites, I have
I want to limit the scanf function so when I enter for example a
i want know how i can manage multiple twitter account on iOS in my
i want know if is possible, to get a specific element value of a
please help me. I want know about what types of flags to run an
I have an android app it displays web view.I want know the app idle
I'm making a chat responder for a game and i want know if there
want to know why String behaves like value type while using ==. String s1
Want to know what is the difference between ipv4 and ipv6 and how does
Want to know ways to store OBJECTS of a Class in some persistent storage.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.