printf() function outputs data to the standard output i.e. to the console . while scanf() function reads data from the standard input i.e. input devices.
The scanf("%d",&number) statement reads integer number from the console and stores the given value in number variable. The printf("cube of number is:%d ",number*number*number) statement prints the cube of number on the console.
The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1. Table 2.1. Special control (or escape sequence) characters.
The printf() function is used to display output and the scanf() function is used to take input from users. The printf() and scanf() functions are commonly used functions in C Language. These functions are inbuilt library functions in header files of C programming.
In C programming language, scanf is a function that stands for Scan Formatted String. It reads data from stdin (standard input stream i.e. usually keyboard) and then writes the result into the given arguments. It accepts character, string, and numeric data from the user using standard input.
The “%d” in scanf allows the function to recognise user input as being of an integer data type, which matches the data type of our variable number. The ampersand (&) allows us to pass the address of variable number which is the place in memory where we store the information that scanf read.
%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 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 returns the number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned. The return value is EOF for an attempt to read at end-of-file if no conversion was performed.
Printf() function is used to print the “character”, string, float, integer, octal, and hexadecimal values onto the output screen. We use printf() function with a %d format specifier to display the value of an integer variable.
%d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int .
Syntax. int printf (const char* c-string, ...); Return Value: If the function successfully executes, it returns the total number of characters written to the standard output.
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().
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.
It is an edit conversion code. The edit conversion code %[^\n] can be used as an alternative of gets. C supports this format specification with a scanf() function.
The scanf() function reads data from the standard input stream stdin into the locations given by each entry in argument-list . Each argument must be a pointer to a variable with a type that corresponds to a type specifier in format-string .
In C language, scanf() function is used to read formatted input from stdin.
The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.
The %s means, "insert the first argument, a string, right here." The %d indicates that the second argument (an integer) should be placed there. There are different %-codes for different variable types, as well as options to limit the length of the variables and whatnot. Control Character.
in "%*d", the first argument is defined as the total width of the output, the second argument is taken as normal integer. for the below program int x=6,p=10; printf("%*d",x,p); output: " 10" the first argument ta passed for *, that defines the total width of the output... in this case, width is passed as 6.
return 0: A return 0 means that the program will execute successfully and did what it was intended to do.
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().
%d (Decimal Integer) Format Specifier. %c (Character) Format Specifier. %f (Floating Point) Format Specifier.