Printing in C programming

Printing in C

In C for printing purpose, We Use A Function Called printf( ).

A function is a small program used to perform a specific task. The function receives some input known as an argument and produces some output called return value. Functions are always suffixed with parenthesis ( ).

Syntax of printf( )

printf("Control String",[List or Variable]);

Note: Control string is a sequence of characters which you want to print. ([ ]) square brackets around the list or variable represent that it is optimal.

Example Of printf( )


printf("hello"); //Print hello


int a=5,b=6;
printf("a=%d b=%d",a,b); //print the value of a and b.


int a=5,b=6; //Varialbe Declaration
printf("%d",a+b); //Print 11.

Note:In printf( ) expression can be written .The example is given above.

printing in c programming

The first program, Say Hello World in C programming


/*program to print the "Hello world"*/
#include<stdio.h>

int main() {
     printf("Hello World");
     return 0;  
}


Output:

Hello World

Program Explanation

  1. First list the comment is specified for the name and purpose of the program Statement printf("hello world"); is used to print the msg "hello world" on the output screen.
  2. Because the program is the collection of the statement which performs a specific task and all the statement are collectively enclose in main() function.
  3. main() is the starting point of the program which is called by the running environment generally OS.
  4. The pair of curly braces {} is used to combine the statement in a group

Statement terminator

  1. In the previous program, you observe that every line ends with a semicolon(;). The semicolon is known as the end of the statement.
  2. Whenever one statement ends we have to put a semicolon(;).
  3. If we write the program in a single line by separating each statement with a semicolon, the compiler doesn't matter because of compiler treat semicolon(;) at end of a line but this way of writing makes the program very difficult to read and understand. So for the efficiency, we have to write the program as clear as possible because the white space and blank lines have no effect on the output.
  4. Observe carefully that main() function doesn't end with a semicolon because of the main end at the closing curly braces.

Reading values from the user by scanf( )

In the previous program, we assume the value of the variable. But with the more flexible program, the user wants to input the value. scanf() function is used for this purpose.

Syntax of scanf()

scanf("format specification",&list of variable);
Note: & is called "address of" operator.

Examples of scanf( ):


scanf("%d",&a); //This function reads an integer from user and store it into variable a.

scanf("%d%d",&a,&b); //This function reads two integer values, first is stored in a and second is stored in b.

scanf("%d%f%c",&a,&b,&c); //This function reads 3 different types of values, first is stored in a, second is stored in b, third is stored in c. In this situation a should be an int variable, b should be float and c should be declared as a character.


Program to find the Sum of two numbers


/*program to add two numbers*/
#include "stdio.h"

int main() { 
      int x, y, sum;
      printf("Enter any two numbers: ")
      scanf("%d%d",&x,&y);

      sum = x + y;
      printf("Total=%d",sum);
}

Output:

Enter any two numbers: 4 9
Total=13

Explanation

  1. The above program is the same as the previous program, the only difference is that the above program receives input from the user.

Program to calculate the simple interest


/*program to find out the simple interst*/
#include<stdio.h>

int main() {
      float p,r,si;
      int t;
      
      printf("Enter the principle,rate and time: ");
      scanf("%f%f%d",&p,&r,&t);
      
      si=p*t*r/100;

      printf("Simple interst=%.2f",si);
}

Output:


Enter the principle,rate and time: 12000 5 2
Simple interst=1200.00

Explanation:

In the above program two things should be noted:
  1. First is the expression si=p*t*r/100;. In this expression, the variable p and r are of float type so the result will be in float.
  2. Expression evaluation rules are...
    1. int-int calculation produce integer result
    2. float-float calculation produce float result
    3. In int-float or float-int calculation, First integer value is change into float and then arithmetic performed, and produce the float result.
    4. The summary is that in the arithmetic the result will belong to the larger type.
  3. The second thing that should be noted is that printf("Simple interst=%.2f",si);. Here %.2f means the only 2 digits will be printed after the decimal places. By default, 6 digits are printed.

Comments

Popular posts from this blog

String in golang

Inline V/S Block Level Element

Floating point Data types in Go lang

Escape Sequence | Formatted Printing | Interview Questions

Program to check a year is leap year or not.

Arrays in C Language

Operators in C Language| Part-4

Sum of two numbers

Data types in Java