Monday, 15 July 2019

Learn Various Operators in C (with video)

operators in c

Operators are the basic infrastructure of any programming language. The functionality of the C programming language. Learn Various Operators in C is incomplete without the use of operators. Operators are the base of any operation. They are the symbols that perform mathematical and logical operations on operands. Operators in C instruct the compiler to perform the specific task.

Let’s understand it with an example,
Suppose, c= a+b;

Then, what happens?

The compiler understands the ‘+’ instruction and adds the value of ‘a’ with ‘b’ and store the result in ‘c.’

See Also: How to avoid the creation of Dangling Pointer in C?

Here, ‘+’ is the addition operator and ‘a’ and ‘b’ are operands. The C programming language has a lot of built-in, and they are:

1) Arithmetic Operators in C

These operators are used to perform arithmetic/mathematical operations (addition, subtraction, etc.) on operands. Arithmetic operators in C are of two types:



A) Unary Operators

Those operators which operate on single operand are known as unary operators. Like, (++, --)

operators in c

a) Increment Operator
This (++) operator increases the value by one. They are of two types:

i) Post-increment Operator: When the increment operator (++) is placed after the variable name is known as Post-increment operator. For instance, z++

This operator first prints the value of z then increases the value of z by one.

Following C program helps you to understand this concept,
#include <stdio.h>
int main()
{
int z=10, out;
out=z++;
printf ("Value of z %d and out\n",z, out);
return 0;
}

Output
Value of z 10 and out 11

ii) Pre-increment Operator: When the increment operator (++) is placed before the variable name is known as Pre-increment operator. Example: ++z. This operator increases the value of z by one, then prints the output.

You can understand the working of Pre-increment operator by C Program
#include <stdio.h>
int main()
{
int z=5;
out=++z;
printf ("Value of z and out is %d%d\n",z,out);
return 0;
}

Output
Value of z and out is 6 6

b) Decrement Operator

This (--) operator decreases the integer value by one. This operator is categorized as a pre-decrement operator and post-decrement operator,

i) Pre-decrement Operator: When the decrement operator (--) placed before the variable name is called Pre-decrement operator. Example: --z

ii) Post-decrement Operator: When the decrement operator (--) placed after the variable name is called Post-decrement operator.

Following C Program helps you to understand the working of Post and Pre-decrement operator

#include <stdio.h>
int main()
{
int z=10,out;
out=--z;
printf("Value of out %d and z %d\n",out,z);
out = z--;
printf ("Value of out %d and z %d\n", out,z);
return 0;
}

Output
Value of out 9 and z 9
Value of out 9 and z 8

B) Binary Operators

Those operators which operate on two operands are known as binary operators. For example: (+, -, /, *, %)

operators in c

Addition operator: This (+) operator add two operands. Example: a+b
Subtraction operator: This (-) operator subtract two operands. Example: a-b
Multiplication operator: This (*) operator multiplies two operands like a*b
Division operator (/): The First operand is divided by the second operand. Example: a/b
Modulus: When the first operand is divided by the second operand, then this (%) operator returns the remainder. Example: a%b


//Following C Program helps you to understand the working of the C program.

#include <stdio.h>
int main()
{
int w=19, x=5, out;
//Addition
out=w+x;
printf("Output of w+x is %d\n",out);
//Subtraction
out=w-x;
printf("Output of w-x is %d\n",out);
//Multiplication
out=w*x;
printf("Output of w*x is %d\n",out);
//Division
out=w/x;
printf("Output of w/x is %d\n",out);
//Modulus
out=w%x;
printf("Output of w%x is %d\n",out);
return 0;
}

Output
Output of w+x is 24
Output of w-x is 14
Output of w*x is 95
Output of w/x is 3
Output of w%x is 4

2) Relational Operators in C

This operator is used for the comparison of two values of two operands. It compares the right-hand value with left-hand value, if these values are true, then return 1 (True value); otherwise, it returns 0 (False value).

operators in c

A) Equal to Operator

This (==) operator checks the value of left operand with a right operand, if it is true then return 1 else return 0 (False). Let’s try to understand it using C program,


#include <stdio.h>
int main()
{
int a=10, b=10;
printf ("a==b %d\n",a==b); // Vlaue of a is equal to b, returns 1 (True).
int x=25, y=5;
printf ("x==y %d\n",x==y); //Value of x is not equal to y, returns 0 (False).
return 0;
}

Output
a==b 1
x==y 0

B) Not equal to operator

This (!=) operator returns the true value, if the value of left operand is not equal to right operand’s value. See below,


#include <stdio.h>
int main()
{
int a=10, b=10;
printf ("a!=b %d\n",a!=b);
int x=30, y=10;
printf ("x!=y %d\n", x!=y);
return 0;
}

Output
a!=b 0
x!=y 1

C) Less than Operator

If the value of the left operand is less than the value of right operand then returns 1 i.e. True else return false value (0).

#include <stdio.h>
int main()
{
int a=10, b=5;
printf ("a<b %d\n",a<b);
int w=3, x=5;
printf ("w<x %d\n",w<x);
return 0;
}

Output
a<b 0
W<x 1

D) Less than or equal to operator

This (<=) operator returns the true value if the left operand is less than or equal to the value of the right operand. Below mention, C program helps you to understand the functioning of this operator,

#include <stdio.h>
int main()
{
int a=10, b=20;
printf ("a<=b %d\n",a<=b);
int x=10, y=10;
printf ("x<=y %d\n", x<=y);
int w=15, z=5;
printf ("w<=z %d\n", w<=z);
return 0;
}

Output
a<=b 1
x<=y 1
w<=z 0

E) Greater than Operator

This (>) operator returns the true value if left-hand operand is greater than right-hand operand. See an C Program,

#include <stdio.h>
int main()
{
int a=10, b=1;
printf ("a>b %d\n",a>b);
int x=12, y=13;
printf ("x>y %d\n",x>y);
return 0;
}

Output
a>b 1
x>y 0

F) Greater than equal to

This (>=) operator returns the true value if the value of the left-hand operand is greater than the value of right-hand operand; otherwise, it returns a false value, i.e., 0.

#include <stdio.h>
int main()
{
int a=1, b=3;
printf ("a>=b %d\n",a>=b);
int w=5, x=4;
printf ("w>=x %d\n", w>=x);
int y=7, z=7;
printf ("y>=z %d\n", y>=z);
return 0;
}

Output
a>=b 0
w>=x 1
y>=z 1

3) Logical Operators in C

If any event depends on more than one conditions, then we have to use a logical operator.

operators in c

A) Logical AND Operator

This (&&) operator works like digital AND gate. When both conditions are true then it returns a true value (1) else returns false value (0).

#include <stdio.h>
int main()
{
int x=10, y=15;
if (x<20 && y>11)
{
printf ("Hello\n");
}
else
printf ("Bye\n");
return 0;
}

In the above C program, the value of ‘x’ is less than 20, and the value of ‘y’ operand is greater than 11. Both the conditions are true. So, it will print Hello otherwise it prints Bye.

B) Logical OR Operator

This (||) operator works like a digital OR gate. Any one condition is true, and then it returns a true value.

#include <stdio.h>
int main()
{
int x =5, y=200;
if (x<20 || y>500)
printf ("Hello\n");
else
printf ("Bye");
return 0;
}

Output
Hello

In the above C program, the value of x is less than 20 which is True, so, it returns 1 (True Value), and value of b is less than 500 which is false, it returns 0 (False Value).

C) Logical NOT Operator

It works like digital NOT gate if the condition is false then it returns a true value. It works on single expression, if the expression is true, then it evaluates false, and if the expression is false, then it evaluates true.

#include <stdio.h>
int main()
{
int a=10;
if (!(a<5))
printf ("True\n");
else
printf ("False\n");
return 0;
}

Output
True

4) Assignment operators in C

This (=) operator assign the value of right-hand operand to the left-hand operand or right-hand side value to the left operand. The value on the right-hand side must be of the same data type of left-hand side otherwise compiler produces an error. Below you can see assignment operator is combined with other operators to make a shorter version,


A) “+=”: The operator first adds the right-hand operand’s value to the left-hand operand and then assign the value to the left-hand operand.
For instance: (x+=y) can be written as (x=x+y).
If x=5 & y=5. Then, (x+=y) =10. OR
If x=5. Then, (x+=10) =15.

B) “-=”: This operator first subtracts the right-hand operand’s value to the left-hand operand and then assign the value to the left operand.
(x -= y) can be written as (x = x-y)
If x = 10 & y = 6, then, (x -= y) = 4, For instance OR
If x =10, then, (x -=6) =4.

C) “*=”: Multiplies the right-hand operand’s value with the left-hand operand and then assign the output to the left-hand operand.
Example: (x *= y) can be written as (x = x*y)
If x = 10 & y=5, then, (x *= y) = 50. OR
If x = 10 then, (x*=3) = 30.

D) “/=”: Value of the left-hand operand’s is divided by the right-hand operand, Therefore, the result is assigned on the left operand.
For instance: (x /= y) can be written as (x = x/y)
If x = 10 and y=2, then, (x /=y) =5. OR
If x = 10, then, then, (x /=5) =2.

5) Bitwise Operators in C

These operators in C are used to perform bit-level operations. These operators are used in low-level programming. This operator first converted to the bit-level, and perform the operation.


operators in c

A) Bitwise AND Operator (&)

The output of this operator is one if a bit of two operands is 1. Any bit of an operand is zero then the result of the corresponding bit will be 0.
For instance, Bitwise AND operation of two integers numbers i.e., 10 and 12.
10 = 00001010 (Binary Form)
12 = 00001100 (Binary Form)

Bitwise AND Operation of 10 and 12.
    0000 1010
& 0000 1100

0000 1000 = 08 (Decimal Form)


C Program Demonstrates working of Bitwise AND Operator

#include <stdio.h>
int main()
{
int x =10, y= 12;
printf ("Output of Bitwise AND Operation = %d\n",x&y);
return 0;
}

Output
Output of Bitwise AND Operation = 8

B) Bitwise OR Operator (|)

The output of this operator is one if any bit of two operands is 1. Therefore, the output is zero, when both the bits of operands is zero.

For instance, Bitwise OR operation of two integers numbers i.e., 10 and 12
10 = 00001010 (Binary Form)
12 = 00001100 (Binary Form)

Bitwise OR Operation of 10 and 12.
  0000 1010
| 0000 1100

0000 1110 = 14 (Decimal Form)

C Program helps you to understand concept of Bitwise OR Operator
#include <stdio.h>
int main()
{
int x =10, y= 12;
printf ("Output of Bitwise OR Operation = %d\n",x|y);
return 0;
}

Output
Output of Bitwise OR Operation = 14

C) Bitwise XOR Operator (^)

Resultant is one if the corresponding bit of two operands differs.
For instance: Bitwise XOR operation of two integers numbers i.e., 10 and 12.
10 = 00001010 (Binary Form)
12 = 00001100 (Binary Form)

Bitwise XOR Operation of 10 and 12.
  0000 1010
^0000 1100

0000 0110 = 06 (Decimal Form)

C Program shows the operation of Bitwise XOR Operator
#include <stdio.h>
int main()
{
int x =10, y= 12;
printf ("Output of Bitwise OR Operation = %d\n",x^y);
return 0;
}

Output
Output of Bitwise XOR Operation = 06

D) Bitwise NOT Operator (~)

It converts bit-1 to bit-0 or vice-versa.
Example: Bitwise NOT Operator or complement operator change the bit from 0 to 1 or 1 to 0.
10 = 0000 1010
Bitwise NOT Operation of 10
~ 0000 1010
   1111 0101 = 245 (In Decimal Form)

See the working of Bitwise NOT Operator
#include <stdio.h>
int main()
{
int x =10;
printf ("Output of Bitwise NOT Operation = %d\n",~x);
return 0;
}


Output
Output of Bitwise NOT Operation = -11

Explanation
The Bitwise complement of 10 is 245 (in decimal form). The 2’s complement of 10 is -11. Therefore, the output is -11 instead of 245.

E) Right-shift Operator (>>)

This operator shifts the bits towards right.
196 = 1100 0100 (Binary Form)
196 >> 2 = 0011 0001 (Right shift by two bits)
196 >> 7 = 0000 0001
196>>8 = 0000 0000
196>>0 = 1100 0100 (No Shift)

F) Left-shift Operator (<<)

This operator shifts the bits towards left.
196 = 1100 0001
196 << 2 = 1100 0000
196 << 0 = 1100 0001 (No Shift)

C Program demonstrates the working of shift operators in C
#include <stdio.h>
int main()
{
int num=196,i;
for (i=0; i<=2;i++)
{
printf ("Right shift %d = %d\n",i,num>>i);
}
printf ("\n");
for (i=0; i<=2;i++)
{
printf ("left shift %d = %d\n",i,num<<i);
}
return 0;
}

Output
Right shift 0 =196
Right shift 1 = 98
Right shift 2 = 49


Left shift 0 = 196
Left shift 1 = 392
Left shift 2 = 784

Monday, 8 July 2019

Learn Dangling Pointer in C


A pointer in the C programming language plays a vital role, but if we are not used pointer properly, then they create some crucial issues. In this article, we will discuss the adverse effect of pointers. Before start discussion on the dangling pointer, let's take a look on, how the pointer allocated in memory?


Dangling Pointer
Figure 1: Pointer points an allocated memory


What is Dangling pointer?


Most of the people ask a question is that what is the difference between a wild pointer and Dangling-pointer? When a pointer is not initialized with any address is known as a wild pointer. And a Dangling-pointer is created when data is deleted from memory, but pointer still points to that memory location (as shown in Figure 2). An issue is created because it still pointing to the memory which is not available when the programmer tries to dereference the dangling-pointers that it shows undefined behavior and can be the cause of the segmentation fault.

See Also: Learn Pointer to Pointer in C

A pointer which is not pointing to the valid memory location can be the reason of undefined behavior.

Dangling pointer

In the above figure, pointer x and pointer y pointing to valid memory data but pointer z are pointing to a memory which is deallocated. So, pointer z becomes a dangling-pointer. But when you try to access the pointer z, then you will get the undefined error. 


Causes of Dangling pointer


There a lot of reason due to the dangling-pointer is created.

1. Function Call

The pointer becomes dangling if the local variable is not static.

#include <stdio.h>
int *fun(); //Declaration of function fun()
int main()
{
int *ptr=fun();
printf ("%d\n", *ptr);
return 0;
}
//Definition of function fun()
int *fun()
{
int a =10;
return &a;
}

Output
Garbage Address

Pointer 'ptr' doesn't become dangling if 'a' is static.

#include <stdio.h>
int *fun(); //Declaration of function fun()
int main()
{
int *ptr=fun();
printf ("%d\n", *ptr);
return 0;
}
//Definition of function fun()
int *fun()
{
static int a =10;
return &a;
}

Output
10

2. Out of scope

When a local variable is declared or initialized within a block, then memory is allocated to the local variable.  That memory is deleted or freed when it comes out of the block. 

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

    //block
    int a =10;
    p = &a;
    }

    {
        printf ("a=%d\n", *p);  //Here, p is a dangling-pointer
    }

    return 0;
}

3. De-allocation of memory


In the C programming language, malloc, calloc and realloc library function are allocates memory during run time and free is used to de-allocate memory. Combat combination of malloc, calloc, realloc, and free generate the dangling-pointer. Whenever the user tries to access the freed memory, then you will get the segmentation fault. 


#include <stdio.h>
int main ()
{
    int *p = (int*) malloc (sizeof (int));
    free (p);  //free the allocated memory
    *p = 20 // p is dangling-pointer
    p = NULL //No more dangling-pointer
    return 0;
}


How to avoid Dangling pointer?


The generation of the dangling-pointer is avoided by the user or programmer if they use NULL explicitly after freeing the dynamic memory.


#include <stdio.h>
int main ()
{
    int *= (int*) malloc (sizeof (int));
    free (p);  //free the allocated memory
    p = NULL //No more dangling-pointer
    return 0;
}


Another factor which creates the dangling pointer is to not return the address of the local variable from the function. It can be solved if we are using the static variable because the lifetime of the static variable is throughout the run of the program.


#include<stdio.h>
int *checkstaticfun( )
{
 static int a = 9;
 return &a;
}
int main ( )
{
int *p = NULL;
p = checkstaticfun( );
 // Now p is Not a dangling-pointer as it points to a static variable.
printf ("%d",*p);
return 0;




In this article, I tried to clear all the concept related to a dangling-pointer, but I would love to know about your views. So, please write your comment in the comment box.


























Monday, 1 July 2019

Learn Pointer to Pointer in C

pointer to pointer


As we already know that pointer points to a memory location which store the address of a variable. So, What is Pointer to Pointer?

A pointer which holds the address of another pointer is known as a pointer to pointer. It is also known as pointer-pointer or double pointer.

See Also: Types of Pointers in C

Pointer to Pointer


As we can see in the above figure, pointer 'Ptr1' holds the memory address of variable 'W' and pointer 'Ptr2' holds the memory address of pointer 'Ptr1'. So, pointer 'Ptr2' is called Double Pointer in C.



How to Declare a Pointer to Pointer in C?

A double pointer is declared similarly as a pointer in C. The difference is that in Double pointer we use an additional indirection operator (*) before the name of the pointer.

Syntax

int **ptr1;

Following example help you to understand the concept of the pointer to pointer in C.
#include <stdio.h>
int main()
{
    //Declare variable 'w'.
    int w=10;
    //Declare pointer variable 'ptr2'
    int *ptr2;
    //Declare double pointer variable 'ptr1'
    int **ptr1;
    //Assigning the address of variable 'w' to the pointer variable 'ptr2'
    ptr2 = &w;
    //Assigning the address of pointer variable 'ptr2' to the double pointer variable 'ptr1'
    ptr1 = &ptr2;

    //Number of ways to find the value of the variable 'w'
    printf ("\n Value of w is :%d",w);
    printf ("\n Value of w using ptr2: %d", *ptr2);
    printf ("\n Value of w using ptr1 :%d \n\n", **ptr1);

    //Number of ways to find the Address of variable 'w'
    printf ("\n Address of w is :%p\n",&w);
    printf ("\nAddress of w using ptr2 is: %p\n",ptr2);
    printf ("\n Address of w using ptr1 is:%p\n",*ptr1);

    //Value of pointer ptr2
    printf ("\n Value of pointer ptr2 : %p", ptr2);
    // Address of pointer ptr2
    printf ("\n Address of pointer ptr2:%p",&ptr2);
    printf ("\n Address of ptr2 using pointer ptr1: %p",ptr1);
    //Address of pointer ptr1
    printf ("\n Address of pointer ptr1: %p",&ptr1);
    //Value of pointer ptr1
    printf ("\n Value of pointer ptr1: %p\n\n",ptr1);
    return 0;
}

Output

Value of w is: 10
Value of w using ptr2: 10
Value of w using ptr1: 10
Address of w is: 0028FF1C
Address of w using ptr2 is: 0028FF1C
Address of w using ptr1 is: 0028FF1C
Value of pointer ptr2 is: 0028FF1C
Address of pointer ptr2: 0028FF18
Address of ptr2 using pointer ptr1:0028FF18
Address of pointer ptr1: 0028FF14
Value of pointer ptr1: 0028FF18

You can understand the program by these equations:
Value of w ==*ptr2==**ptr1
Address of w ==ptr2 == *ptr1
&ptr2 ==ptr1

Find Out the Output of the Following Program Using Pointer to Pointer or Double Pointer in C.

#include <stdio.h>
int main()
{
    int arr[]={10,20,30,40,50,60};  //Line-1
    int *ptr[]={arr,arr+1,arr+2,arr+3,arr+4,arr+5};  //Line-2
    int **qtr=ptr //Line-3
    qtr++;  //Line-4
    printf ("%d\n",**qtr);   //Line-5
    *qtr++;   //Line-6
    printf ("%d\n",**qtr);  //Line-7
    ++*qtr //Line-8
    printf ("%d\n",**qtr);  //Line-9
    ++**qtr;  //Line-10
    printf ("%d\n",**qtr);  //Line-11
    return 0;
}


pointer to pointer


Explanation

In the above program, double pointer(**qtr) work as an arithmetic pointer. An integer type array (arr) is declared. Pointer (*ptr) consists of a base address of array(arr).
Line 1: An integer type array 'arr' with 6 elements are declared.

Line 2: An array of pointer *ptr[] is declared which holds the address of array arr[].

Line 3: Double pointer (**qtr) holds the address of pointer *ptr.
If the address of the array starts from 2000 and the size of the integer is 2. The pointer array 'ptr' will contain the values like 2000,2002,2004,2006,2008,2010. Assume, the base address of pointer array 'ptr' starts from 3000. The double pointer (*qtr) holds the address of pointer array (*ptr).

Line 4: Value of 'qtr' is increased by one i.e. double pointer (**qtr) points to address of pointer array i.e. 3002.

Line 5: printf ("%d\n", **qtr);
*qtr==*ptr==arr[1]
Double pointer (**qtr) holds the address of (*ptr) i.e. 3002 and Pointer (*ptr) holds the address of array i.e. 2002.
qtr = 3002, *qtr =2002, *(*qtr)=*(2002)=20 i.e. output will be 20 at the output screen.

Line 6: As we can see, *qtr++ is written. Here, are the two operators * and ++ operator. But, Both have the same precedence associativity so it will evaluate from right to left. *qtr++, qtr hold the address 3002 i.e. (*(qtr++))==(*3004))==2004.

Line 7: printf ("%d\n", *qtr) i.e. *qtr==*(2004)==3000.

Line 8: ++*qtr can be rewritten as according to the rule of associativity, (++(*qtr)). Since, qtr = 3004, *qtr=2004, ++*qtr = 2004+2=2006.

Line 9: printf ("%d\n", **qtr), *qtr=2006=*(2006)=40

Line 10: ++**qtr can be written as (++(*(*(qtr)))), qtr = 3004, *qtr=2006, **qtr=40, ++**qtr = **qtr+1=41.

Line 11: printf ("%d\n", **qtr); 41 will print at the output screen.

Monday, 24 June 2019

Learn If Statement in C

If statement in C is a decision-making statement (in real life, we need to take some decision, these decisions decide what should we do next, the same condition for programming which determines what block of code will execute). When the condition in if statement is true, then the block of code will execute and condition in if statement is false, then the block of code will not execute.

Syntax
if (condition) {
//Block of code

Flow Chart of if Statement in C

if statement in C

If curly braces ({ ) is not provided after if statement, then only the first statement will execute, and the rest of the statement will not execute.


See Also: Do While Loop in C

Program 1: Write a program to find out the largest number from the given numbers are 14 & 13 using if statement in C.

 Here, we tried to explain how to find out the largest number step by step:
Algorithm
Step1:  Start the program.
Step2:  Enter input data a =14, b=13.
Step3: Write if statement with the condition (a>b).
Step4:  When the condition (a>b) is true, then the block of code will execute. When the condition (a>b) is false, then go to Step5.
Step5: Print the output.
Step6: Finish the program.


See Also: Learn Dynamic Memory Allocation in C

Flow Chart of the Largest Number Program


if statement in C

Program
#include <stdio.h>
int main()
{
    int a =14, b 13;
    if (> b)
    {
        printf ("largest number is=%d\n", a);
    }
    return 0;
}

Output
largest number is = 14

Program 2: Write a program to count the number of boys whose weight is less than 50 kg and height is greater than 170 cm using if statement in C.

Steps mentioned below helps you to understand the program,
Algorithm
Step1: Start the program.
Step2: Input data number_of_boys, i, count=0, weight, and height.
Step3: Write condition in if statement (weight should be less than 50 kg and height should be greater than 170 cm).
Step4: When if the condition is true, then the number of count of boys will increase by one. When the condition is false, then go to Step5.
Step5: Print the output.
Step6: Stop the program.

Flow Chart

if statement


Program
#include <stdio.h>
int main()
{
    int number_of_boys, i, count = 0;
    float weightheight;
    printf ("Enter the number of boys\n");
    scanf ("%d", &number_of_boys);
    printf ("Entered number_of_boys are %d\n", number_of_boys);
    printf ("Enter the number of weight and height");
    for (=1;i<=number_of_boys; i++)
    {
        scanf ("%f%f", &weight, &height);
        if (weight < 50 && height >170)
        {
            count ++;
        }
    }
    printf ("The number of boys with weight less than 50kg and height greater than 170cm is %d\n"count);
    return 0;
}

Output
Enter the number of boys
5
Entered number_of_boys are 5
Enter the number of weight and height 45 172
46 178
51 179
47 155
43 458
The number of boys with weight less than 50kg and height greater than 170cm is 3.

Program 3: Write a Program to find out the given number is even or not using if statement in C.

If a number is divided by 2 and the remainder comes to zero, then that number is even number otherwise that number is odd. 
Algorithm
Step1: Enter the number a=5.
Step2: If the remainder is equal to zero, then the given number is even otherwise it is odd.
Step3: Stop

Program
#include <stdio.h>
int main()
{
    int a=5,rem;
    rem 5%2;
    if (rem==0)
    {
        printf ("Entered number %d is even %d",a);
    }
    printf ("Entered number %d is odd ",a);
    return 0;
}

Output
Entered number 5 is odd.







Monday, 17 June 2019

THE C PROGRAMMING LANGUAGE

Ok, Friends...Now, I am going to discuss the basics of the C programminglanguage. Before start discussion about the C programming language, first, we will discuss the history of C like who developed it and why and what is the need of the C programming language. C is a general-purpose procedural computer programming language.
       If you want to learn C++, C#, JAVA, JAVASCRIPT, Python and any other programming language it is necessary you must have knowledge about the basics of the C language. Let’s start...


the C programming language






















Features of the C programming language

1. A C is a case-sensitive programming language and it follows the scope rule. 
2. This language is easy to learn and understand.
3. There is a lot of arithmetic and logical operator in the C programming language, such as +,++,-, =, &, ! etc.
4. It has a fixed number of keywords.
5. Programmer can create its own data type in the C programming language using the keyword struct, union, enum etc.
6. Supports preprocessor that executes before the compilation.
7. It also supports pointers.
8. You can avoid function return values if you don't want to use it.

Why use C language?

There are various reasons to select the C programming language for development language. I have some reason that describes why C is a popular language and why most of the people use it.

1. C is a general-purpose language and easy to learn, whatever you learn in the C programming language, you can be used in the future programming language.
2. This language is a good choice for the designing of an operating system such as Microsoft's windows, Mobile phone's operating system.
3. C is used in the creation of language compiler and interpreter.
4. A lot of legacy programs are written in the C programming language.
5. This language is also a portable language.
6. C language is also used in the database (Oracle database, MySQL).
7. This language is also used in the designing of embedded systems.
8. It is widely used in IoT (Internet of Things) applications. 

History of the C programming language

The first programming language 'ALGOL' is introduced in the year 1960. This language is widely used in European Countries.  A new programming language i.e. Basic Combined Programming Language (BCPL) is developed in 1967 which is specially designed for writing system software. This language was developed by Martin Richards.

After three years, in 1970, a new programming language 'B' is designed by Ken Thompson at AT&T and Bell Laboratories. In 1972, Dennis Ritchie developed a new programming language called 'C' at Bell laboratories. This language consist features of 'ALGOL', 'BCPL' and 'B' programming language.


the C programming language



















UNIX operating system is written in 'C' language. At the initial stage, 'C' is used only for designing the operating system, but as it becomes popular this language is used commercially and many compilers are designed for cross-platform systems. Nowadays, various operating system and hardware platform are designed using 'C' language. As it started evolving many different versions of the language were released.

In 1989, the American National Standard Institute (ANSI) declared a commercial standard for 'C' programming language. In 1990, 'C' language is approved by the International Standard Organization (ISO). This language is also known as 'ANSI C'

Structure of C program

From the structure, it means that any program can be written only in this structure. Therefore, writing a C program in any other composition will make the compilation error. The structure of the C program is as follows:

the C programming language




















Write a Program to display "WELCOME TO THE HELLO WORLD!". 

There are many in-built library functions to do input/output tasks in C programming. The two most used functions for input/output are printf() and scanf(). The scanf() function reads the input data while printf() sends the formatted output to the standard output (the screen).

 #include <stdio.h>
int main( )
{
printf ("WELCOME TO THE HELLO WORLD");
return 0;
 }

Output
WELCOME  TO THE HELLO WORLD

Explanation
#include <stdio.h> : In the C program, all the lines starting with # are processed by the preprocessor, which is a program implemented by the compiler. In a very basic word, the preprocessor takes a C program. The program produced does not have any line starting from #,  all such lines are processed by the preprocessor. In the above example, the preprocessor copies the preprocessed code of stdio.h on our file. .h files are called header files in C. Declaration of functions are usually in these header files. We require stdio.h for the printf() function used in the program.

int main( ): This is a starting point from where the execution of C program begins. main() can be written to take parameter also. The int wrote before main indicates return type of main(). The value returned by main indicates the status of program termination. Main() can be written as mention below:

1. main(): blank parentheses indicates that the main( ) can have an argument.
2. void main(): void means function does not return any value.
3. int main(): int means function returns an integer value. so, it is necessary to mention return 0 at the end of the program.
4. main (void): It also indicates that there is no argument in the main function and return type is also void
5. int main (void): no argument with the integer return type.
6. void main (void): no argument with no return type.

printf ("WELCOME TO THE HELLO WORLD");: printf is a standard library which is used to print output at the screen. The semicolon at the end of this line indicates the termination of the line.

return 0;: The return statement returns the zero value to main(). The value 0 means successful termination of the program.