Fundamentals in C Programming - printf and scanf

in #tutorial6 years ago

C - printf and scanf

-printf() and scanf() functions are built in library functions in C programming language which are available in C library by default.
-These functions are declared and related macros are defined in “stdio.h” which is a header file in C language.
-We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions in C language.

1. PRINTF() FUNCTION IN C LANGUAGE:
-In C programming language, printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.

-We use printf() function with %d format specifier to display the value of an integer variable.

-Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.

-To generate a newline,we use “\n” in C printf() statement.

Note:

C language is case sensitive. For example, printf() and scanf() are different from Printf(), Scanf(), PRINTF() and SCANF(). All characters in printf() and scanf() functions must be in lower case.

EXAMPLE PROGRAM FOR C printf() FUNCTION:

OUTPUT:

2. SCANF() FUNCTION IN C LANGUAGE:
-In C programming language, scanf() function is used to read character, string, numeric data from keyboard
-Consider below example program where user enters a number. This value is assigned to the variable “num” and then displayed.
-Then, user enters a string and this value is assigned to the variable “str” and then displayed.

EXAMPLE PROGRAM FOR printf() AND scanf() FUNCTIONS IN C PROGRAMMING LANGUAGE:

OUTPUT:

-The format specifier %d is used in scanf() statement. So that, the value entered is received as an integer and %s for string.
-Ampersand is used before variable name “str” in scanf() statement as &str.

ALWAYS REMEMBER

  1. printf() is used to display the output and scanf() is used to read the inputs.
  2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
  3. All syntax in C language including printf() and scanf() functions are case sensitive.

Thank you for spending your time to read this.

I hope this would help you guys :D
See you again to my next tutorial it would be about DATA TYPES..
And don't forget to upvote and comment some suggestions :D

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.031
BTC 71082.84
ETH 3864.32
USDT 1.00
SBD 3.52