What Is scanf() and printf() Function

C functions

C is a functional programming language. C provides library of functions to perform input-output operations. This library is called a standard input-output library. It is denoted by stdio. The header file containing such library functions is called stdio.h. Some standard input-output functions in C are:

  1. printf()
  2. scanf()
  3. gets()
  4. puts()
  5. getch()
  6. getchar()
  7. putchar()
  8. getche()

Mainly there are two types of input-output(I/O) statements:

  1. Formatted I/O statements
  2. Unformatted I/O statements
scanf() and printf() are formatted input and output statements where as getchar(), gets() and putchar(),puts() are unformatted input and output statements.
Lets have a look on Two most widely used INPUT and OUTPUT Functions:
Scanf() Function:
  • To read the values for the variables in a program from the keyboard , C provides a function called scanf(). 
  • It is equivalent to the READ statement. 
  • It is included in stdio.h header file. 

The syntax of scanf() :


  scanf(” control string” , address_list );  

Where
Control string => It is asequence of one or more character group. Each character group is a combination of the % symbol and one of the conversion character. Control string specifies the type of the values which are to be supplied to the variable.
Address_list => Address of the memory location where the value of the input variables should be stored.

NOTE: The character f in the scanf() shows the format(” “) of this function.

Printf() Function:
  • It is used to display the data on the monitor. 
  • It is included in the stdio.h header file. 

The syntax of printf() is:


  printf(“Control string” , variable);  

Control string => this specifies the type and format of the values to be displayed.
Variable => list of variables to be displayed.

NOTE: The character f in the printf() shows the format(” “) of this function

Header files:
  • Files that are placed before main() of a C program are called header files. 
  • The header files are used to provide necessary information. 
  • Header files contain library functions. 
  • They can be written with in the angle brackets or the double quotes. 
  • The header files usually contains .h as extension. 
  • They are entered into the source program via #include directive.
Preprocessor directive:
  • C processor is a collection of special statements are called preprocessor directive. 
  • It is executed before the C program is complied. 
  • The preprocessor directive begin with the # symbol and followed by either the include or define keyword. 
  • They are used to include files into a program.

Example:  
#include<stdio.h>
where  #include is a preprocessor directive and <stdio.h> is a header file.

C Program: Addition of Two Numbers With or With Out Using scanf() Function

c program

// ADDITION OF TWO NUMBER: 2 Methods

  1. With the help of scanf() function
  2. without scanf() function

1. Without scanf() Function

#include<stdio.h>
#include<conio.h>
void main()

{

float a,b,c;
clrscr();
a=10.2;
b=5.3;
c=a+b;
printf(“Sum of two Numbers=%f” ,c);
getch();
}
Output:
Sum of two Numbers=15.5
2. With the help of scanf() Function:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(” Enter the value of a and b:/n”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“Addition of Two Numbers=%d” , c);
getch();
}
Output:
Enter the value of a and b:
50
40
Addition of Two Numbers=90
  

C Program: Enter a Number and Determine It Is Even or Odd Number

C program image

This program is based on conditional statement that is if-else.
Write a Program: Enter a Number and Determine wheather it is Even or Odd Number.

#include<stdio.h>                          
// Header file contain input output functions.
#include<conio.h>                         
// Header file contain clrscr() and getch() functions.
void main()                                   
// C program execution start with this main function. No C program can execute without this main function.
{
int a,b;                                         
// Declare two variables a and b of integer data type.
clrscr();                                       
// Clear the result of previous output.
 
printf(” Enter the value of a”);     
// This is output function. Enter the value of a is show on screen.
scanf(“%d”, &a);                     
// This is input fuction. We enter the value of variable a with the help of scanf function.
b=a%2;                                   
// The remainder value obtained after dividing integer variable a by 2 is assign to integer variable b.
if(b==0) 
// if-else condition is applied
{
printf(“Number is Even”);
}
else
{
printf(“Number is Odd”);
}
getch();
}
// If the condition b==0 is True then Number is Even execute other wise Number is Odd is executed. 

NOTE: The Lines written in // symbol is for just telling about the meaning of statement. These lines not executed in the program.

Output1:

Output1:

Enter the value of a           

44
Number is Even
Output2:
Enter the value of a              // clrscr() clears the previous output1
77
Number is Odd

Detailed Discription of C Language Operators section-2

C operators

RELATIONAL OPERATORS:

  • Relational operators are used to compare two operands. 
  • Result is either a TRUE or FALSE value. 
  • The value of TRUE is 1(non zero) and the value of FALSE is 0(zero).


 OPERATOR    
       <
       >
      <=
       >=
       ==
       !=
       MEANING 
        Less Than
      Greater Than
   Less Than or equal to
  Greater Than or equal to 
          Equal to
        Not Equal to
 PRECEDENCE
          1
          1
          1
          1
          2
          2  


EXAMPLE:

If  W=7   X=5    Y=10    Z=7
  • X<Y        TRUE (Value 1)
  • W==Z     TRUE (Value 1)
  • Z>Y        FALSE (Value 0)
  • W!=X      TRUE (Value 1)
LOGICAL OPERATORS:
  • AND,OR and NOT are three Logical Operators. 
  • The result of these operators is either TRUE or FALSE. 
  • AND,OR are Binary operators. 
  • NOT is Unary Operator.


OPERATOR 
     &&
       ||
       !
  MEANING  
  Logic AND
  Logic OR
  Logic NOT 
 PRECEDENCE 
            2
            3
            1

KEY POINTS ABOUT TABLE:
  • AND Operator is Equivalent to Multiplication.
  • OR Operator is Equivalent to Addition
  • The result of Logic ANDing is TRUE when both the operands are TRUE otherwise FALSE.
  • The result of Logic ORing is FALSE when both the operands are FALSE otherwise TRUE.
  • The result of Logic NOT is TRUE when the Operand is FALSE and vice versa.
UNARY OPERATORS:
Unary operators acts upon only one operand. They are of 3 types:
  1. Unary minus
  2. Logical NOT operator
  3. Bitwise complement

EXAMPLES:

1) Unary Minus: 

If x=2 and y=5
z = x + (-y)
   = 2 + (-5)
   = -3
Value of z is -3 because initially y is positive integer variable, when operated by unary minus, its value changed to negative.

2) Logical NOT operator:


 OPERAND 
   True
    False 
 !OPERAND 
     False
     True  

3) Bitwise complement:
 Let a=10 and is equivalent to 1010 Binary value
z = ~a
   =~(1010)
   = 0101

Detailed Discription of C Language Operators Section-1

C operators

OPERATOR: Operator is a symbol that perform any operation.

OPERAND: On which operation is to be performed.
EXAMPLE:
a+b Where + is an Operator and a, b are the Operands
                  
C contains a rich set of operators. Operators used to perform basic airthmatic operations, manipulation of bits ,comparison etc. Operators operate on a single operand or two operands. C operators divided into 3 categories:
  1. Unary operators
  2. Binary operators
  3. Ternary operators
So Lets have a look on all Operators one by one:

TERNARY OPERATORS:
  • It takes three operands.
  • It is a conditional operator.
  • The two symbols that is ? and : are used in ternary operator.

<Condition>  ?  <Value1>  :  <Value2>

EXAMPLE:
M = a>b ? a : b;
Where Condition = a>b
Value1 = a
Value2 = b
If this condition a>b is True then a is assign to M but If condition is False then b is assign to M.
BINARY OPERATORS:
It takes two Operands. Binary operator are classified into 4 categories
  1. Airthmetic operators
  2. Logical operators
  3. Relational operators
  4. Bitwise operators
AIRTHMETIC OPERATORS:
These are used to perform the basic airthmatic operations such as multiplication,division,addition, substraction and Modulus Operator. It is used for finding remainder after an integer division. Operates on integer and float numbers. So there are two types of airthmetic operations:
  • Integer airthmetic
  • Floating point airthmetic.
INTEGER AIRTHMATIC:
If both the Operands are of integer type then Integer airthmetic is performed and result is integer value.
Ex: 
      If A=10 and B=20  then 
  • A+B=30
  • B-A=10
  • A*B=200
  • B/A=2
FLOATING POINT AIRTHMATIC:
If both the Operands are of float type then Floating point airthmetic is performed and result is Floating point  value.
Ex:
  If A=1.5 and B=2.5  then 
  • A+B =4
  • B-A=1
  • A*B=3.75
  • B/A=1
AIRTHMATIC OPERATORS:


 OPERATIONS 
     Addition
     Substraction
     Multiplication    
     Division
     Modulus 
 OPERATOR  
        +
        –
        *
         /
        %
 PRECEDENCE 
         2
         2
         1
         1
         1 

KEY POINTS ABOUT TABLE:

  1. Lowest Number Indicates Higher Priority.
  2. Highest Number Indicates Lowest Priority.
  3. Divide(/) Turncates the Fractional Part.
  4. Modulus Operator can not be used with Floating Point Number.


EXAMPLE:
If A=2 and B=5
  • A+B=7
  • B-A=3 
  • A*B=10
  • B/A=2
  • B%A=1