Sunday, 28 April 2019

Increment and decrement operators in C


Increment Operators are used to increase the value of the variable by one and Decrement Operators are used to decrease the value of the variable by one in C programs.

Both increment and decrement operator are used on a single operand or variable, so it is called as a unary operator. Unary operators are having higher priority than the other operators it means unary operators are executed before other operators.
There are two special unary operator in C/C++, called increment(++) and decrement(--) operators that changes the value of variable by a unit.
Syntax:
Increment Operator : ++
Decrement Operator : --

Example:
int i=1;
i++;
then i=2

int j=5;
j--;
then j=4

 Note :
i++ is same as i=i+1;
j-- is same as j=j-1;

Types of Increment Operator :
(a).Post Increment: varName ++
In post increment statement, first use the value of variable and then increment it.
For example;
int i=1;
int x= i++ + i;
Output:
x=2

(b).Pre Increment : ++varName
In pre increment statement, first increment and then use its value after completion of equation.
For example;
int i=1;
int x= ++i + i;
Output:
x=4

Types of Decrement Operator :
(a).Post Decrement : varName--
In post decrement statement, first use the value of variable and then decrement it.
For example;
int i=5;
int x= i-- + i;
Output:
x=10

(b).Pre Decrement : --varName
In post decrement statement, first decrement and then use its value after completion of equation.

For example;
int i=5;
int x= --i + i;
Output:
x=8

Complete Example:
#include<stdio.h>
int main(){
          int i=1,j=1,k=5,l=5,x1,x2,y1,y2;
//        printf("X1=%d\n",++(i+j));
          x1=i++ + i;
          printf("I=%d,X1=%d\n",i,x1);
          x2=++j + j;
          printf("J=%d,X2=%d\n",j,x2);
         
          y1=k-- + k;
          printf("K=%d,Y1=%d\n",k,y1);
          y2=--l + l;
          printf("L=%d,Y2=%d\n",l,y2);
         
          return 0;
}
Output:



Please comment and share if you find it useful.

Thursday, 4 April 2019

Functions in C(Part 2-Parameter Passing Methods)

C programming
In the previous article, you learnt basics of functions in C. In this article, you will learn about methods of passing values.

Passing values to function
There are two ways to pass arguments/parameters to function calls -- call by value and call by reference.
(i). Pass by value
(ii). Pass by reference

(i).Pass by Value
  In this method of parameter passing, a copy of actual parameters is passed to respective formal parameters. Any changes done in formal parameters are not reflected in actual parameters.
When a function is called, any arguments that are provided by the caller are simply treated as expressions. The value of each expression has the appropriate conversions applied and is then used to initialize the corresponding formal parameter in the called function, which behaves in exactly the same way as any other local variables in the function.
The initialization of the formal parameters is the last time that any communication occurs between the caller and the called function, except for the return value.

Example:
//Following example shows the functioning of call by value method
#include<stdio.h>

void swapCallByValue(int,int);  //function prototype
int main(){
    int num1=10,num2=20;
   printf("Values before swapping by call by value: num1=%d,num2=%d.\n",num1,num2);
    swapCallByValue (num1,num2);
    printf("Values after swapping by call by value: num1=%d,num2=%d.\n\n",num1,num2);
    return 0;
}
//function definition: using third variable
void swapCallByValue (int n1,int n2){
  int temp;
  temp=n1;
  n1=n2;
  n2=temp;
  printf("Inside swapCallByValue : num1=%d,num2=%d\n",n1,n2);
}
Output:
   Values before swapping by call by value: num1=10,num2=20.
   Inside swapCallByValue : num1=20,num2=10
   Values after swapping by call by value: num1=10,num2=20.

(ii).Pass by Reference
In call by reference method of parameter passing, the memory address of actual parameters is passed to formal parameters, hence any change made to formal parameters will also reflected in actual parameters.

This method of parameter passing is use-
(a).If the size of data is large.
(b).When there are more than one value we want to alter.

Example:
//Following example shows the functioning of call by reference method
#include<stdio.h>
void swapCallByReference(int *,int *);  //function prototype
int main(){
    int num1=10,num2=20;
    printf("Values before swapping by call by reference: num1=%d,num2=%d.\n",num1,num2);
    swapCallByReference (&num1,&num2);
    printf("Values after swapping by call by reference: num1=%d,num2=%d.\n\n",num1,num2);
    return 0;
}
//function definition: using third variable
void swapCallByReference (int *n1,int *n2){
  int temp;
  temp=*n1;
  *n1=*n2;
  *n2=temp;
  printf("Inside swapCallByReference : num1=%d,num2=%d\n",*n1,*n2);
}
Output:
   Values before swapping by call by reference: num1=10,num2=20.
   Inside swapCallByReference : num1=20,num2=10
   Values after swapping by call by reference: num1=20,num2=10.

Points to note about Functions
(a).The name of a function is always followed by ( ).
(b).The keyword void is used in the example to show the function has no return
value and no parameters.
(c).When no return type is specified, it is assumed to be int.
(d).The main program is also a function. It is called the driver function.
(e).The program always starts at the beginning of main, regardless of whether it is
the first function defined or not.


Please comment and share if you find it useful.

Functions in C (Part 1-Introduction)

C programming
 Introduction to Functions
A function is an independent module or block of program code which deals with a particular task. A function can be called or used anywhere in the program by calling the name.
Each function has a name or identifier by which is used to refer to it in a program. A function can accept a number of parameters or values which pass information from outside, and consists of a number of statements and declarations, enclosed by curly braces { }, which make up the doing part of the object.
The main is a special function called at the beginning of the program. All other functions are directly or indirectly called from main. That’s why main function is called driver function.

Reasons for using functions
There are many reasons for using functions.
(i).Reusability: A function can be reused in many different contexts without repeating parts of the program text.
(ii).Manageability: Using functions, program will be divided to separate blocks. Each block will do a specific job Designing, understanding and managing smaller blocks of code is easier.
(iii).Readability : Since each function has few lines of code which is easy to read and understand.

Parts of a function
(i).Function prototype
(ii).Function Definition
(iii).Function Calling

(i).Function Prototype / Function Declaration
If we want to use a function before we define it, we must declare it just like a variable to inform the compiler about the function. This declaration is called the function prototype.
The point at which a name has a type associated with it. No storage is reserved at this point.

Syntax:
returnType functionName(dataType var1, dataType var2,……,dataType varN);

 The variable names are not required when declaring a function prototype.

returnType functionName(dataType, dataType,……,dataType);

Examples:
          int square(int number);
          void display(void);


(ii).Function Definition
The block of code between braces following the function header is called the function body.
The function header defines the name of the function, the function parameters (which specify the number and types of values that are passed to the function when it’s called), and the type for the value that the function returns.
The function body contains the statements that are executed when the function is called, and these have access to any values that are passed as arguments to the function.

The general form of a function looks like this:
Return_type Function_name( Parameters - separated by commas )
{
// Statements...
}
Also a declaration, but at this point some storage is reserved for the named object.
The arguments used in the function definition are called formal parameters.
Example:
int square(int num){
   int result;
   result=num*num;

   return result;
}
(iii).Calling a function
A function is called (i.e. control is passed to the function) by using its name with the usual brackets () to follow it, along with the values which are to be passed to the function.
When a function is called, the statements within the body of that function are executed, and when the function has finished executing, control returns to the point at which that function was called.
    Syntax:
                   functionName(parameters);
The  arguments used when the function is actually called are called actual parameters. In other words, the values that the formal parameters will have on entry to the function.

Example:
   int res=square(number);
Complete example
#include<stdio.h>
 //function prototype
int square(int number);

int main(){
  int number,res;
  printf("Enter an integer:\t");
  scanf("%d",&number);
  //functional calling
  res=square(number); 
  printf("\nSquare of %d is %d.\n",number,res);
  return 0;
}

//function definition
int square(int num){
   int result;
   result=num*num;
   return result;
}

How function works?
Program execution start from main function.  Statements are executed in sequence in the normal way until a function call is found.
When function call is found, control passes to function definition along with parameter values and statement with in the called function are executed.
Execution of the program continues through the function statements until it encounters a return statement or reaches the closing brace marking the end of the function body. The return statement of closing brace ‘}’ indicates the end of function and control passes to the point immediately after the point the function was originally called.

A Function can different forms:-
Type 1: No return type, no argument
   void functionName(void){
          //function body
      }

Type 2 : return type, no argument
   dataType functionName(void){
          //function body
        return variable;
      }

Type 3 : return type, with argument list
dataType functionName(argument list){
          //function body
        return variable;
      }

Please comment and share if you find it useful.

Wednesday, 3 April 2019

Top free stock photo sites for your blog

Looking for photos for your blog, presentation,magazine without worrying for copyright, here we present top free stock photo sites for you.

Pixabay is a vibrant community of creatives, sharing copyright free images and videos. All contents are released under the Pixabay License, which makes them safe to use without asking for permission or giving credit to the artist - even for commercial purposes.

2.Unsplash
Over 850,000 free (do-whatever-you-want) high-resolution photos brought to you by the world’s most generous community of photographers.
3. StockSnap
StockSnap offer beautiful, high quality stock photos for just about any use you can think of - including commercial usage - for free. No tricks, no gimmicks, no fine print; just dazzling images for your creative projects.
Pexels provides high quality and completely free stock photos licensed under the Pexels license. All photos are nicely tagged, searchable and also easy to discover through our discover pages.


Please comment and share if you find anything incorrect, or you want to improve the topic discussed above.

Sunday, 31 March 2019

C Programming Break and Continue statements

The break and continue statements in C language are used to alter the normal flow of execution of program.
Break statement
  • Used with loops and switch statements
  • When encountered, it breaks the execution of block in which break statement is used and program execution starts from the first statement flowing the block in which break statement is used.
Example:
#include<stdio.h>
int main(){
            int arr[5]={10,8,9,6,5};
            int i,key=9;
           
            //use of break statement
            for(i=0;i<5;i++){
                        if(arr[i]==key){
                                    printf("Element %d found at index %d.\n",key,i);
                                    break;
                        }
            }
            return 0;
}
Output:


Continue statement
  • Used with loops.
  • Usually used with if statement
  • When encountered, it breaks the execution of current iteration and start next iteration.
Example:
#include<stdio.h>
int main(){
            int arr[5]={10,8,9,6,5};
            int i,key=9;
            //use of continue statement
            for(i=1;i<=10;i++){
                        if(i%2==0){
                                    continue;
                        }
                        printf("\n%d",i);
            }
            return 0;
}
Output:


Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Friday, 29 March 2019

C Loops - For loop, while loop and do while loop

You may encounter situations, when a block of code needs to be executed several number of times. A loop is used for executing a block of statements repeatedly until a given condition returns false.
There are three types of loop constructs in C. These are as follows-
(1). for loop
(2). while loop
(3). do while loop

(1).for loop
It is used mainly when number of iterations (number of times loop will execute) are known beforehand.
Syntax of for loop
The for loop can be written in several ways.
Syntax 1:
for(initialization;test_condition;updateStatement){
   //loop body
}

Syntax 2:
Initialization;
for(;test_condition;updateStatement){
   //loop body
}

Syntax 3:
initialization
for(;test_condition;){
   //loop body
updateStatement;
}

Note : updateStatement means increment or decrement statement

How for loop works?
  • The initialization statement is executed only once.
  • Then, the test_condition is evaluated. If the test_condition is false (0), for loop is terminated.
  • But if the test_condition is true (nonzero), codes inside the body of for loop is executed and the updateStatement is updated.

This process repeats until the test_condition is false.
Examples:
//program to print integer number from 1 to 10 in ascending order
#include<stdio.h>
void main(){
  for(int i=1;i<=10;i++){
     printf(“%d\t”,i);
   }
}
Output: 1 2 3 4 5 6 7 8 9 10
//program to print integer number from 1 to 10 in descending order
#include<stdio.h>
void main(){
  for(int i=10;i>=1;i--){
     printf(“%d\t”,i);
   }
}
Output: 10 9 8 7 6 5 4 3 2 1

(2). While loop
The while statement is used when the program needs to perform repetitive tasks.
The general form of a while statement is:

while (condition){
statement;
}
The program will repeatedly execute the statement inside the while until the condition becomes false (0). (If the condition is initially false, the statement will not be executed.)
 How while loop works?

  • The while loop evaluates the condition.
  • If the condition is true (nonzero), codes inside the body of while loop is executed. The condition is evaluated again. The process goes on until the condition is false.
  • When the condition is false, the while loop is terminated.


Example :
//program to print numbers from 1 to 10 in ascending order
#include<stdio.h>
void main(){
int i=1;
while(i<=10){
  printf(“%d\t”,i);
  i++;
 }
}

(3).do while loop
The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once.

Syntax:
initialization;
do{
  //loop body
updateExpression;
}while(testExpression);

How do...while loop works?
  • The code block (loop body) inside the braces is executed once.
  • Then, the testExpression is evaluated. If the testExpression is true, the loop body is executed again. This process goes on until the testExpression is evaluated to 0 (false).
  • When the testExpression is false (nonzero), the do...while loop is terminated.

·
Example:
//program to print numbers from 1 to 10.
#include<stdio.h>
void main(){
  int i=1;
do{
  printf(“%d\t”,i);
  i++;
  }while(i<=10);
}

Above discussed loops can be categorized into two groups based on-when test condition is tested.
(1). Entry controlled loop
  • The types of loop where the test condition is stated before the body of the loop, are known as the entry controlled loop.     
  • In this category of loops, test condition is evaluated before entering into the loop body.
  • Example- for loop and while loop.


(2). Exit controlled loop
  • The types of loop where the test condition is stated at the end of the body of the loop, are known as the exit controlled loops.
  • So, in the case of the exit controlled loops, the body of the loop gets execution without testing the given condition for the first time.
  • Then the condition is tested. If it comes true, then the loop gets another execution and continues till the result of the test condition is not false.
  • Example : do while loop


Please comment if you find anything incorrect, or you want to improve the topic discussed above.