Describe the printf() and scanf() functions with necessary examples.
|NU Year||Set: 1.(c) Marks: 3 Year: 2013|
printf() and scanf() functions are inbuilt 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.
char ch = 'A';
char str = "fresh2refresh.com";
float flt = 10.234;
int no = 150;
double dbl = 20.123456;
printf("Character is %c \n", ch);
printf("String is %s \n" , str);
printf("Float value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
· In C programming language, scanf() function is used to read character, string, numeric data from keyboard
· Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.
· Then, user enters a string and this value is assigned to the variable “str” and then displayed.
printf("Enter any character \n");
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
printf("Entered string is %s \n", str);