Sunday, March 31, 2019

Basics of Programming in C (part-6)


Hello Everyone!


Welcome to the 7th post. This post is contributed by Sumanth Nidamanuri.

In the previous post we have discussed about switch case statements.
Basics of Programming in C (part-5):

In this post we are going to discuss Functions. This post will take less than 10 minutes to completely read and to understand it.

Function
A computer cannot perform all the tasks by itself.So it utilizes the entities named functions.
But what is a function?
According to Wikipedia a function means to work or operate in a particular way.Similarly a function in C also performs a desired task according to the user requirements.  

Definition
A function is a self contained block of code that performs a particular task of acting on data and may return a value.

Function prototype:
Datatype function_name(datatype variable-1,datatype variable-2,_ _ _ _)
{
            Local declarations;
            Executable statements;
            Return statement;

}
The part within the curly braces is called the body of the program and the initial line 
is called the header.The header consists of the function name,the return type of the function and the number and the type of the arguments passed to the function.  

Example:

Code:


Output:

Explanation:


As the C program executes from top to bottom initially the main() calls the function call_fun().This means that the control passes  to function call_fun().The functioning of main is temporarily suspended and the call_fun() starts working.When all the statements in the call_fun() are executed then again the control passes to the main() exactly at the point where the control has been transferred to the call_fun().

Here main() is the calling function and call_fun() is the called function.

Basically there are two types of functions.
1.user defined functions
2.built-in functions

User defined functions: these are created by the user as per the requirement of the program


Built-in functions: these are the part f the standard library and are made available by the compiler as a  part of the compiler package.

Passing values between functions:
Example:
#include<stdio.h>
int sum(int x,int y,int z);
int main(){
  int a,b,c,result=0;
  printf("\n enter the values of a,b,c:");
  scanf("%d %d %d",&a,&b,&c);
  result=sum(a,b,c);
  printf("\n THE SUM OF THE NUMBERS IS %d ",result);
  return 0;
}
int sum(int x,int y,int z){
  int p;
  p=x+y+z;
  return (p);

}

Output:

enter the value of a,b,c: 2 4 9

THE SUM OF THE NUMBERS IS 15

Explanation:
The values of a,b,c which are read from the user are passed to the function sum() from the main().

In the sum() these values get collected in the variables x,y,z.The variables a,b,c are called the actual parameters and x,y,z are formal parameters.

Any numbers of arguments can be passed to a function.But the number of parameters and the data type must match otherwise it results in an error.sum() returns a value p which is of data type integer .So in the function prototype the return type of the function sum() is declared int.

On execution of the return the control directly passes to the calling function and the sum of the three numbers is being returned back to the main().

The important point to note here is the scope.The scope of the variables x,y,z  is limited only between the braces of the function sum().That means that these cannot be used outside the braces.

That’s all for this post.In next post we will discuss about recursion functions

Share it with your co-geeks and help us to improve by giving feedback in the comments section below.

Thank you.

 ABOUT US
SUMANTH NIDAMANURI, B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT ANITS and SUJITH SAGAR, B.tech SECOND YEAR COMPUTER SCIENCE AND ENGINEERING STUDENT AT NIT DELHI.


Basics of Programming in C (part-5)


Hello Everyone!


Welcome to my 6th post. First of all, I should thank my parents, sister and my friends for supporting me and encouraging me.

In the previous post we have discussed about switch case statements.
Basics of Programming in C (part-4):

In this post we are going to discuss for loops and while loops. This post will take less than 5 minutes to completely read and to understand it.

Loop statements

Loop statements are used when we want to execute some line of code repeatedly. 
For loops are used when we know the number of times we run the loop. 
while loops are used when we are not sure about the number of times we run the loop.

Syntax:

FOR loop:

for(counter_variable initialization; for_condition; counter_variable increment/decrement )
{
        statements;
}


WHILE loop:

while(while_condition)
{
      statements;
}

Do WHILE loop:

do
{
      statements;
}while(while_condition);


In For loop parenthesis we have three parts. First is for initialization, Second is for condition and third is to increment or decrement  the counter variable.

First the variable is initialized. Then the condition is checked. If the condition evaluates to be true then compiler will execute the statements inside the loop block. after execution, it will increment/decrement the counter variable and will check the condition again. if condition is true then executes the block statements, otherwise the flow of control comes out of the loop and continues to execute the statements outside the loop.

Similarly in WHILE loop also, first the condition is evaluated. If it is true then statement inside the block are executed else loop is terminated. After executing the statements inside the loop compiler will check for the while condition.

In DO WHILE loops, the while condition is evaluated after executing the statements for the first time. Therefore, in Do while loops even if the condition evaluates to be false the statements are executed once.

Note: In both types of loops we need to take care of termination condition. Otherwise, the loop runs infinitely many times(😈).

Example:
Code:


Output:



That’s all for this post. In the next post we’ll discuss about  Functions.

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

 ABOUT ME






















MYSELF SUJITH SAGAR. I AM A B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.

Basics of Programming in C (part-4)


Hello Everyone!


Welcome to my 5th post. First of all, I should thank my family and my friends for supporting me and encouraging me.

In the previous post we have discussed Conditional statements.
Basics of Programming in C (part-3):

In this post we are going to discuss about switch case statements. This post will take less than 5 minutes to completely read and to understand it.

Switch case

Switch case changes the direction of flow of control with respect to the input. The condition in switch statement is compared with case statements in the switch block.when the switch condition and case condition both are equal, the statements in that specific case block are executed. Using default statement is optional but it is preferred to handle errors when switch condition does not matches with any of the case conditions.

Therefore, the statements in the default block executes only if the switch condition does not matches with any of the case conditions.

Using break statement is optional.

Note: If there is no break statement in case blocks then after executing the necessary  case block instead of exiting the switch block compiler executes all other case block which are followed by the necessary case block. We should use break statement in each case block in order to prevent the flow of control in entering into next case block. 




Flow chart:





















Syntax :

 switch(choice_variable)
{
          case case_variable-1:
          Statements-A;
          break;

          case case_variable-2:
          Statements-B;
          break;
          .
          .
          .
          case case_variable-n:
          Statements;
          break;
           
          default:
          Statements;
}


Example:
Code:


Output:



That’s all for this post. In the next post we’ll discuss about  for loops.

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

 ABOUT ME






















MYSELF SUJITH SAGAR, B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.


Saturday, February 16, 2019

Basics of Programming in C (part-3)


Hello Everyone!


Welcome to my 4th post. First of all, I should thank my parents, sister and my friends for supporting me and encouraging me.

In the previous post I covered topics like constants, variables and data-types etc.
Basics of Programming in C (part-2):

In this post I am going to explain about Conditional statements. This post will take less than 5 minutes to completely read and understand it.

Understanding Conditional Statements

In order to understand conditional statements, first we need to understand conditions.
In C, conditions are logical statements whose final value will be either true(it is generally represented as integer 1 or any non-zero integer ) or false(it is represented as 0)

In logical statements, we need to use logical operators.
Some of the logical operators are:

·        == (if L.H.S and R.H.S are equal then this operator returns true(1) else returns false(0))
Example: (2+1)==3 returns true and 2==3 returns false

·        < (if L.H.S is less than R.H.S then this operator returns true(1) else returns false(0))
Example: 2<3 returns true and 5<4 returns false

·        > (if L.H.S greater than R.H.S then this operator returns true(1) else returns false(0))
Example: 3>2 returns true and 2>8 returns false

·        <= (if L.H.S less than or equals to R.H.S then this operator returns true(1) else returns false(0))
Example: 2<=3 returns true and 4<=3 returns false

·        >= (if L.H.S greater than or equals to R.H.S then this operator returns true(1) else returns false(0))
Example: 4>=3 returns true and 6>=10 returns false

·        != (if L.H.S and R.H.S are not equal then this operator returns true(1) else returns false(0))
Example: 2!=3 returns true and (2+4)!=(3+3) returns false

In C, conditional statements are implemented through if-else statements.




Syntax :

 if(condition-A)
{
          Statements-A;
}
else if(Condition-B)
{
          Statements-B;
}
else
{
          Statements-C;
}

Example:
Code:


Output:



We use conditional statements to direct the direction of flow of control of our program into respective parts of the program by using some respective conditions.

When the flow of control reaches ‘if - else’  block, first it will check the condition that is present inside the parenthesis of ‘if’ block. If the condition evaluates to be true then the flow of control will go inside the ‘if’ block and execute the statements in it. After that flow of control skips all the else-if blocks and final else block and continues with the rest of the program.
If the ‘if ’condition evaluates to be false then compiler will check the next else-if block condition. If else-if block condition is true then those statements present in else-if block will be executed. If the else-if condition is false then compiler will check for the next else-if condition if it is present.

When ‘if’ condition and all the else-if conditions are false then the statements inside the ‘else’ block are executed.



Note: we can use multiple 'else-if' blocks corresponding to one 'if' block but we can use only one 'else' block corresponding to one 'if' block. Using 'else-if' and 'else' blocks without any preceding 'if' block is an error. We can also use nested if-else block.

Practice Question: write a program to print whether a given number is even or odd using if-else statements.

comment your solution in the comment section below.

That’s all for this post. In the next post we’ll discuss about  Switch case statements.

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

Thanks to Binshumesh Sachan for helping me in editing.

Thanks alot to Nikhitha, Bhavani, Sadhana, Kailash varma, Devendhar, Manoj, Rakesh, Karthik, Koushik, Soma Sekhar, Saqlain, Jaswanth and all other who are sharing these posts with their Co-geeks. Once again thank you guys. 

 ABOUT ME






















MYSELF SUJITH SAGAR. I AM A B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.



Sunday, February 3, 2019

Basics of Programming in C (part-2)

Hello Everyone!

Welcome to my 3rd post.First of all, I should thank my parents, sister and my friends for supporting me and encouraging me.

In the previous post I covered topics like installation of compiler, Writing our first C program etc.
Basics of Programming in C (part-1):
https://codewithsujithsagar.blogspot.com/2019/02/basics-of-programming-in-c-part-1.html

In this post I am going to explain about constants, variables and data-types with some simple examples. This post will take only 5 minutes to completely read and understand it.

Understanding constants, variables and data-types

By the name itself we can understand that constants are those whose value doesn’t change and variable are those whose value can be changed.

The Data-type of a value or variable is an attribute that tells what kind of data that value or variable can have. Some of the data-types are 
int (for integer values like -56, 0, 89 etc)
float (for floating values like 2.36, 5.298 etc)
char (for characters like ‘A’, ’b’, ’%’ , ‘4’, compiler will treat ‘4’ as a character as it is enclosed within apostrophe. So 4 is an integer and ‘4’ is a character).

A variable having integer data-type indicates that we can only store integer values in that variable and a constant is said to be having integer data-type if it is an integer.

Similarly, a variable having float data-type indicates that we can store floating values in that variable and a constant is said to have float data-type if it is a floating number.

We will store constants in variables by assigning them to variables. Here we need to remember that we can only assign that constant to variable where both of their data-types are same.

However, there are some exceptions. We can store an integer value in a float variable (that integer variable is automatically converted to float, for example ‘2’ is converted to ‘2.0’).
Similarly, we can store float values in integer variables but the floating number are rounded off to integer values while assigning them (for example, ‘3.67’ is converted to ‘3’).

Memory

Each variable occupies some amount of memory in the computer. The amount of memory that is occupied by a variable is depended upon the data-type of that variable. Each memory location will have some address. That memory is allocated to the variable when we initialize the variable. In background, the required amount of memory that will be occupied by the variable is named as that variable_name and each memory location have some address.

In order to assign any value to a variable, we should use = (assignment operator).
Variable should be on the left side of the = operator and the value which we want to store should be on the right side of the = operator. We can also assign the value of one variable to other. We can also perform arithmetic operation on variables and constants while assigning them to a variable.
Example:
int x = 3;
int y = 2;

For example, we can consider a bag which can hold at most one book at a time as a variable and it’s data-type is book (so, that bag can only hold books) and books of different subjects each as constants. Here that school bag may contain a book related to math or science or social or any other subject but at an instance of time it will contain anyone of them. So here we can change books in that bag. But if we observe, we cannot transform math textbook into social textbook, we can only replace them by removing one from bag and by adding the other. We can say that the data-type of the school bag is book. That means we cannot keep a cricket bat in that school bag. Don’t think about the size of bat and bag, we can’t even keep a pencil in that bag instead of book because the data-type of the bag is book.

Syntax for variable initialization and declaration:

Data-type variable_name;
Data-type variable_name = constant;
Data-type variable_name = another_variable + constant * (another_variable);

Example:
int x = 3;
int y = 2;
int z = x*(y+3);

To display the value stored in a variable we should know about format specifiers which tells to compiler about which type of variable you are trying to display.

int -> %d
float -> %f
char -> %c

syntax:
printf(“format_specifier”,variable_name);

usage:
printf(“The value stored in the variable X is %d”,X);

Now we know how to display the text and the values stored in a variable using printf statement. We can store the values in variables by using scanf statement.

Syntax:
scanf(“format_specifier”,&variable_name);

example:
scanf(“%d”,&x);

Here x is variable of int data-type and we need to declare x as an int variable above the scanf statement. Otherwise, compiler will throw an error because we already saw that compiler starts compiling from first line to last line of program sequentially. It is our job to tell compiler that we are going to use some type variable before using that variable.

If you notice, in the syntax of scanf statement we used symbol ‘&’ which tells the compiler to store this constant in that memory location(at the address of variable x).

Note: while printing a value of a variable we don’t use & symbol as a prefix for variable_name whereas while using scanf statement to store them we should use & symbol as a prefix for variable_name.

Try executing the following statement,

int x=2;
printf(“%u is the address of variable x”,&x);

'%u' is for unsigned integer.
for more information:
https://www.w3schools.in/c-tutorial/format-specifiers/

That’s all for this post. In the next post we’ll discuss about Conditional statements.
https://codewithsujithsagar.blogspot.com/2019/02/basics-of-programming-in-c-part-3.html

Share it with your co-geeks and help me to improve by giving feedback in the comments section below.

Thank you.

Thanks to Binshumesh Sachan and Shaik Saqlain Musthak for helping me in editing.

Thanks alot to Nikhitha, Bhavani, Sadhana, Kailash varma, Devendhar, Manoj, Rakesh, Karthik, Koushik, Soma Sekhar and all other who are sharing these posts with their Co-geeks. Once again thank you guys. 

 ABOUT ME




MYSELF SUJITH SAGAR. I AM A B.tech SECOND YEAR  COMPUTER SCIENCE AND ENGINEERING STUDENT AT NATIONAL INSTITUTE OF TECHNOLOGY DELHI.

Linkedin profile: www.linkedin.com/in/sujith-sagar-gandi-2b3544179

How Can You Test and Revert a Failover in Azure Cache for Redis Premium with Geo-Replication?

If you're using Azure Cache for Redis Premium with geo-replication across different regions, knowing how to test failover and revert it ...