Printf() and Scanf() are inbuilt library functions in C language that perform formatted input and formatted output functions. These functions are defined and declared in stdio. h header file. The 'f' in printf and scanf stands for 'formatted'.
printf (print formatted) in C, writes out a cstring to stdout (standard output). The provided cstring may contain format specifiers( beginning with % in the cstring). If there are format specifiers, those are replaced with their respective arguments that follow the cstring to the printf call.
printf("Enter a number "); printf(" and press Enter: "); scanf("%d", &a); IMPORTANT: the symbol & is known as the ADDRESS-OF operator.
In this program, the user is asked to enter two integers. These two integers are stored in variables number1 and number2 respectively. printf("Enter two integers: "); scanf("%d %d", &number1, &number2);
%d tells printf that the corresponding argument is to be treated as an integer value; the type of the corresponding argument must be int .
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.
Example: int var; scanf(“%d”, &var); The scanf will write the value input by the user into the integer variable var.
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);
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.
Generally, printf() function is used to print the text along with the values. If you want to print % as a string or text, you will have to use '%%'.
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 %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.
%d specifies signed decimal integer while %i specifies integer. There is no difference between the %i and %d format specifiers for printf.
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.
%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.
%g. It is used to print the decimal floating-point values, and it uses the fixed precision, i.e., the value after the decimal in input would be exactly the same as the value in the output. %p. It is used to print the address in a hexadecimal form.
The Most Commonly Used Format Specifiers in C. %d (Decimal Integer) Format Specifier. %c (Character) Format Specifier. %f (Floating Point) Format Specifier.
Each argument takes a size of integer in stack. For data types whose sizes are greater than integer, double or multiples of integer size are taken. Inside the function, we take the pointer of the first argument. We can get the next argument by incrementing the pointer value.
To create a function with two inputs, we just need to provide two different arguments inside function. For example, if we want to create a function to find the square of a+b then we can use x and y inside function.
Using split() Method
The split() method is useful for getting multiple inputs from users. The syntax is given below. The separator parameter breaks the input by the specified separator. By default, whitespace is the specified separator.
scanf(“%[^\n]%*c”,name); means that all the characters entered as the input, including the spaces, until we hit the enter button are stored in the variable, name; provided we allocate sufficient memory for the variable.