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.
Printf() Function
In C programming language, the printf() function is used for output. Printf() function can take any number of arguments. First argument must be enclosed within the double quotes Hello
and every other argument should be separated by comma (,) within the double quotes.
- printf() function is defined in <stdio.h> header file. By using this function, we can print data or user defined message on monitor (also called console).
- printf() can print a different kind of data format on the output string.
- To print on a new line on the screen, we use "\n" in printf() statement.
C language is case sensitive programming language. For example, printf() and scanf() in lowercase letters treated are different from printf() and scanf(). All characters in print() and scanf() built in functions must be in lowercase.
Syntax: printf("format specifier", argument_list);The format string output can be %d (integer), %c (character), %s (string), %f (float) %lf (double) and %x (hexadecimal) variable.
Example:
int main()
{
int num = 450;
// print number
printf("Number is %d \n", num);
return 0;
}
Number is 450
Scanf() Function
The scanf() function is used to read input data from the console. The scanf() function is builtin function available in the C library. Scanf() function can read character, string, numeric, & other data from keyboard in C language. Scanf() reads formatted data from user and assign them in the variables provided the additional arguments. Additional arguments must point to variables that have the same datatypes as of user input data format.
Syntax: scanf("format specifier", argument_list);Example:
int main()
{
int x;
printf("Enter the number =\n");
scanf("%d",&x);
printf("The number is=%d",x);
return 0;
}
Enter the number =10
The number is=10
Converison techniques
Type conversion in C is the process of converting one data type to another. The type conversion is only performed to those data types where conversion is possible. Type conversion is performed by a compiler. In type conversion, the destination data type can't be smaller than the source data type. Type conversion is done at compile time and it is also called widening conversion because the destination data type can't be smaller than the source data type. There are two types of Conversion:
Operators in C are actually 5 types.
The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 10 and variable B holds 20, then
Operator | Description | Example |
+ | Adds two operands | A + B = 30 |
- | Sutracts second operand from the first. | A - B = -10 |
* | Multiplies both operands | A * B = 200 |
/ | Divides numerator by denominator or de-numerator | B/A = 2 |
% | Modulus Operator and remainder of after an integer division. | B%A = 0 |
++ | Increment operator increases the integer value by one. | A++ = 11 |
-- | Decrement operator decreases the integer value by one. | A-- = 9 |