C printf function [1.0.7]

Posted by Parth Makadiya on 07:15 with No comments
  • printf() and scanf() functions are inbuilt library functions in C 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.
  • We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions.
    First we will see What is Printf ?

C printf() function:


#include <stdio.h>
int main()
{
char a="Parth";
int b=19;
printf("My name is %c and My age is %d",a,b);


}                                                                                                                                                                                                   .
Output:
My name is Parth and My age is 19                                                                                                                                                                   .

  • 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() and Scanf(). All characters in printf() and scanf() functions must be in lower case.

Example program for C printf() function:

#include <stdio.h>
int main()
{

float flt = 10.234;
int no = 150;
double dbl = 20.123456;
char ch = ‘A’;
char str[20] = “parthmakadiya.blogspot.com”;
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);
return 0;
}                                                                                                                                                                                                   .
Output:
Character is A
String is parthmakadiya.blogspot.com
Float value is 10.234000
Integer value is 150
Double value is 20.123456
Octal value is 226
Hexadecimal value is 96                                                                                                                                                                   .
You can see the output with the same data which are placed within the double quotes of printf statement

These are few Access Specifiers .They are used inside printf to print .
  • %d    as  integer variable ,
  • %c     as character variable ,
  • %f   as float variable ,
  • %lf   as double variable,
  • %s     as string variable,
  • %o got replaced by a octal value corresponding to integer variable  (no),
  • %x got replaced by a hexadecimal value corresponding to integer variable