Basic input/output (printf, scanf)

Basic input/output (printf, scanf)

In C programming language, printf() and scanf() are two essential built-in program functions. They are basically used to perform program input and program output text information operations from programmer to program. While printf() function prints program information to the console screen, scanf() function inputs or displays program variable data values ​​from the programmer to the program from the variable storage location.

Basic input/output (printf scanf)

printf() function.

printf() function is used in C program to print formatted program output values ​​from the user to the standard output device console screen, usually the console. printf() function displays text visual information and variable values ​​on the output console screen.

Use of printf() function in C language.

#include <stdio.h>

int main()

{

int decimal = 10;

printf(“\n welcome to c language);

printf(“The default value of decimal is – %d\n”, decimal);

printf(“The decimal integer output is – %d + %d = %d\n”, 1, 2, 1 + 2);

return 0;

}

In the above basic C program example is.

  • %d – An integer is a storage placeholder for program value.
  • \n – \n is used to display newline character information between two console print text formats.

scanf() function.

In C programming scanf() function is used to input or read formatted program variable value from the user/programmer to a standard input device, usually the keyboard. This function allows the programmer to take input from the user in program execution and store it in a variable.

Use of scanf() function in C language.

#include <stdio.h>

int main()

{

int p;

printf(“\n Enter a p value – “);

scanf(“%d”, &p);

printf(“the value p variable is – %d\n”, p);

return 0;

}

In the above C program example.

  • %d – operator specifies in the program that an integer program value should be read as p variable.
  • &p – memory address location of variable p in C program where p program variable input value will be stored.

Different C program formatting specifiers.

In C programming you are given some commonly used program variable value format specifiers for printf() and scanf() function.

  • %d – in printf() and scanf() function %d integer value is printed and read/input.
  • %f – Print and read/input floating value %f in printf ( ) and scanf ( ) functions.
  • %lf – Print and read/input double value %lf in printf ( ) and scanf ( ) functions.
  • %c – Print and read/input character value %c in printf ( ) and scanf ( ) functions.
  • %s – Print and read/input string/text value %s in printf ( ) and scanf ( ) functions.
  • %x, %X – Print and read/input hexadecimal program value %x in printf ( ) and scanf ( ) functions.
  • %o – Print and read/input octal program value %o in printf ( ) and scanf ( ) functions.

Always remember.

For scanf() function in C language, always pass the address of the declared variable to the program by using & address operator before the variable name.

Use correctly with format specifiers to match the data types of the variable.

The printf() function and scanf() function in C programs can be used together to take programmer user input and display program output as required. Both printf() function and scanf() function come built-in with the standard input/output program library (<stdio.h>) in C programming. Which makes them widely available for basic user input and program output operations in C programming.