Methods In C :
The scanf() and printf() functions are used to read the input from the console and output to the console. Both the functions are built-in functions defined in stdio.h(standard input output) header file. C has many built-in library functions. Whenever we use any built-in functions we need to declare the header file to which the built-in function belongs to.
scanf(): The scanf() function is used to read the input from the console/user.
syntax:
scanf("format specifiers",&variables_list);
Example:
scanf("%d %d", &var1,&var2); [no of specifiers = no of variables]
%d: %d in scanf allows the function to recognize user input as being of an integer data type.
&: The ampersand(&) allows us to pass the address[place in memory] of the variable where we store the information that the scanf read.
printf(): The print() function is used for output to the console.
Syntax:
printf("format specifiers", variable_list);
No need for an ampersand(&) symbol in printf().
Example:
printf("%d %d",var1,var2);
Format Specifiers: The format specifiers are used in C for input and output purposes. Using these format specifiers, the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function.
Format specifier | Type |
%d | Signed integer [both positive and negative values] |
%i | Unsigned integer [only positive values] |
%l | Long integer |
%lld | Long long integer |
%c | character |
%f | Float values |
%lf | Double values |
%Lf | Long double |
%s | String |
No comments:
Post a Comment