Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to specify data type each time in C to printf() and scanf()?

As you can see from the code snippet below, I have declared one char variable and one int variable. When the code gets compiled, it must identify the data types of variables str and i.

Why do I need to tell again during scanning my variable that it's a string or integer variable by specifying %s or %d to scanf? Isn't the compiler mature enough to identify that when I declared my variables?

#include <stdio.h>  int main () {   char str [80];   int i;    printf ("Enter your family name: ");   scanf ("%s",str);     printf ("Enter your age: ");   scanf ("%d",&i);    return 0; } 
like image 504
Akki619 Avatar asked Aug 13 '13 07:08

Akki619


People also ask

What is the purpose of printf () and scanf () in C program?

The printf() and scanf() functions are used for input and output in C language. Both functions are inbuilt library functions, defined in stdio. h (header file).

What is difference between printf () and scanf ()?

Format specifier string: Note: The major difference between printf and scanf is, In printf() we pass variable values whereas in scanf() we pass the variable address.

Why do we use scanf and not in printf?

Use of & in scanf() but not in printf() As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf().

Why do we use scanf in C?

In the C programming language, scanf is a function that reads formatted data from stdin (i.e, the standard input stream, which is usually the keyboard, unless redirected) and then writes the results into the arguments given.


2 Answers

Because there's no portable way for a variable argument functions like scanf and printf to know the types of the variable arguments, not even how many arguments are passed.

See C FAQ: How can I discover how many arguments a function was actually called with?


This is the reason there must be at least one fixed argument to determine the number, and maybe the types, of the variable arguments. And this argument (the standard calls it parmN, see C11(ISO/IEC 9899:201x) §7.16 Variable arguments ) plays this special role, and will be passed to the macro va_start. In another word, you can't have a function with a prototype like this in standard C:

void foo(...); 
like image 106
Yu Hao Avatar answered Oct 20 '22 18:10

Yu Hao


The reason why the compiler can not provide the necessary information is simply, because the compiler is not involved here. The prototype of the functions doesn't specify the types, because these functions have variable types. So the actual data types are not determined at compile time, but at runtime. The function then takes one argument from the stack, after the other. These values don't have any type information associated with it, so the only way, the function knows how to interpret the data is, by using the caller provided information, which is the format string.

The functions themselves don't know which data types are passed in, nor do they know the number of arguments passed, so there is no way that printf can decide this on it's own.

In C++ you can use operator overloading, but this is an entire different mechanism. Because here the compiler chooses the appropriate function based on the datatypes and available overloaded function.

To illustrate this, printf, when compiled looks like this:

 push value1  ...  push valueN  push format_string  call _printf 

And the prototype of printf is this:

int printf ( const char * format, ... ); 

So there is no type information carried over, except what is provided in the format string.

like image 23
Devolus Avatar answered Oct 20 '22 16:10

Devolus