In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.
The printf() and scanf() functions are required for output and input respectively in C. Both of these functions are library functions and are defined in the stdio. h header file.
The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but can be unreliable because it doesn't handle human errors very well. But for simple programs it's good enough and easy to use. scanf("%d", &b);
In C language, scanf() function is used to read formatted input from stdin.
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.
The main difference between them is: scanf() reads input until it encounters whitespace, newline or End Of File(EOF) whereas gets() reads input until it encounters newline or End Of File(EOF), gets() does not stop reading input when it encounters whitespace instead it takes whitespace as a string.
sprintf stands for “String print”. Instead of printing on console, it store output on char buffer which are specified in sprintf.
So far as is traceable, "scanf" stands for "scan format", because it scans the input for valid tokens and parses them according to a specified format.
Because it needs the address to place the value it reads. If you declare you variable as a pointer, the scanf will not need the & .
The most basic printing functions would be puts and putchar which print a string and char respectively. f is for formatted. printf (unlike puts or putchar ) prints formatted output, hence printf. For example it can print an int in hexadecimal, or a float rounded to three decimal places, or a string left padded.
%d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value.
The void main() indicates that the main() function will not return any value, but the int main() indicates that the main() can return integer type data. When our program is simple, and it is not going to terminate before reaching the last line of the code, or the code is error free, then we can use the void main().
%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.
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().
It will print the number incorrectly but it will run till the end without crashing.
scanf will store the data it receives in the memory location where the variable is stored. In order to do this, it needs the memory location of the variable. Preceding the variable name with an & indicates the address of the variable, rather than the variable itself.
scanf returns EOF if end of file (or an input error) occurs before any values are stored. If any values are stored, it returns the number of items stored; that is, it returns the number of times a value is assigned by one of the scanf argument pointers.
Reading a Text File
Each line read from the file can be processed using the command sscanf (string scanf). sscanf is the same as scanf except it works on a string, rather than the input stream. The format for sscanf is: sscanf(%lt;buffer to scan>, <format string>, arg1, arg2, ...
The only difference between sprintf() and printf() is that sprintf() writes data into a character array, while printf() writes data to stdout, the standard output device.
printf() function stands for 'print formatted', which prints output on the standard console, whereas sprintf() stands for 'string print formatted', which actually does not print anything but loads the buffer with the character stream.
Warning: The sprintf function can be dangerous because it can potentially output more characters than can fit in the allocation size of the string s . Remember that the field width given in a conversion specification is only a minimum value. To avoid this problem, you can use snprintf or asprintf , described below.
In case of a string (character array), the variable itself points to the first element of the array in question. Thus, there is no need to use the '&' operator to pass the address.
Tl;Dr: Both functions do the same thing but use a different backend I/O. scanf is from a previous form of c and causes more work if you change a variable type later. cin doesn't require the variable type to be added to the call to avoid this issue. scanf is typically faster than cin due to syncing.
The scanf function reads input from the console and parses it. It has no means of printing anything.
%d (Decimal Integer) Format Specifier. %c (Character) Format Specifier.