Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

How to use scanf and printf in C programming language?

How to use scanf and printf in C programming language?

What is the printf function?

You can use the printf function to display the output of your program. This function is most commonly used in the C programming language. The printf function can take any numbers, letters and symbol. It means, you can printf character, string, float, integer, octal and hexadecimal values. But all of this should be included in the double quote.  Ex. "sample"
  • The prndf () function is defined under the "stdio.h" header file.
  • If you want to print a new line in your program, you can use the "\ n" in the printf () expression.
  • To print on a tab horizontal space on the screen, we use “\t” in printf() statement
We use different symbols to represent different variables. It is as follows
  • %d or %i -  integer variable
  • %f            -  float variable
  • %lf           -  long float variable or double 
  • %c            -  character variable
  • %s            -  string variable
  • %x           -   hexadecimal variable

#include <stdio.h>
int main()
{
   char ch = 'A';
   char name[20] = "dtk2globle.com";
   float flt_ex = 22.478;
   int no = 150;
   double dbl = 26.123478;
   printf("Character is %c \n", ch);
   printf("String is %s \n" , name);
   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);
   return 0;
}

What is the scanf function?

Uses the scanf function to read data entered by the user. Scanf function can read character, string, numeric data from use input. The Scanf () function is made up of functions in the C library.
#include <stdio.h>int id,name;
printf("Enter your Id number");
scanf("%d",&id);
printf("Enter your name");
scanf("%s",&name);
return 0;
If you have any different idea please leave a comment below and we can discuss it.

Post a Comment

0 Comments