Thursday, July 18, 2013

Functions in C

Functions are nothing but a piece of code which is meant to do some task out of the main program, which can be reusable. In C, we write functions to make program simple. Suppose we want to find the square of a number ten times in a program, instead of writing ten times the same code with different numbers, we can write a function which takes a number and gives the square and use the function every time we need the function.

Functions are basically of three types.

Storage Issues in Arrays

In arrays, we do have storage under a single name. All the array elements are assigned sequential address starting address from the index 0. Based on the data type we use, we can access the location of the index by increment of the address based on the data type we use.

Here is the table, which states the general address space used by a data type.

Wednesday, July 17, 2013

Arrays in C

Arrays are nothing but a bulk storage of a single data type in a single name. Suppose in a program, we need to store months of a year, then we take twelve different variables. Instead of taking twelve different variables and taking a risk to maintain them, we can declare all the twelve under one name i.e., months, by declaring it as an array.

An array can have any number of elements. All these elements are given an index with a subscript to the variable.

gets() as Input Function in C

In C, along with scanf(), we have another function which is widely used for taking inputs. That is gets(). But the difference between scanf() and gets() is that gets() takes only the Strings or char arrays for the input. If you observe the prototype of the gets() function, you can understand it easily.
char   *_Cdecl gets(char *__s);
You can understand more clearly if you observe the following program.

scanf() as Input Function in C

In C, the most commonly used Input Function is scanf(). This function takes the input from the standard input i.e., keyboard and sets the values taken to some variable. This scanf() function takes any type of data based on the format specifiers and then assigns the value to the variable of that particular data type when specified as arguments.

To know the prototype of the scanf() function, you can refer here.

Tuesday, July 16, 2013

puts() as Output Function in C

In C, along with printf(), we have puts() to print some text on to the screen. We can print some literal text but not the variables. Using puts(), we can't print any variable values. But can print only some text on the output.

If we observe the prototype of the function puts() in STDIO.H, we will understand the function. It will be like this

Printf() as Output Function in C

Printf() is the most widely used output function in C. We have used printf() for many printing operations previously. Let us know more about the printf() statement. In C, printf() has a certain format strings which define what should be output of the program.

We already know the prototype of the printf() function. If not, click here to know.

Input and Output Functions in C

Before going to the functions, we will first know what is Input Function and What is Output Function.

Input function is that function which is ready to take some value into the program. In C, we have many functions, but the most used Input function is the scanf().


Monday, July 15, 2013

The continue Statement in C

In C, along with the control Flow statements, there are other two statements which also controls the flow. Among them one is continue statement while the other is break statement.

While the break statement gets the flow out of a loop, continue statement makes the loop to turn to the next iteration. It doesn't break the entire loop, but just breaks the current iteration and makes the loop to move on to the next iteration.

The do... while loop in C

The do...while loop is another kind of loop in C. It is different from the other looping statements. In for loop or while loop, we check for the condition before execution of the statements. But in do... while, we perform execution first and then check for the condition. You will get clear understanding after checking the following program.

The while loop in C

In C, while loop is one of the looping statements which is used most. In while, unlike for, we need not mention the initial and increment statements. We directly specify the condition which is to be met and the loop continues to execute until that condition fails.

In some situations, while loop is better than for loop. But in most of the case, for loop is better than while.
Syntax:

  • while(condition)
  • {
  • //Statements
  • }

Sunday, July 14, 2013

The for loop in C

In the Control Flow Statements, loops are one of the most useful and necessary part. Among the loops, for loop is the most familiar and used looping statement.

Syntax:

  • for(initial; condition; Increment)
  • {
  • //Statements to be executed
  • }
The above shown is the syntax of a for loop. A for loop contains three parts mainly.

The goto Statement in C

The goto Statement is an unconditional branching statement in C. Suppose if we want to go to some set of statements in the same function immediately. Then we use goto to switch from current executing statements to the block of statements which we want to perform and the execution continues from that lines of execution.

The goto statement must have a label. A label is that which recognizes which set of statements are to be executed. Consider the following example, to understand more about the  goto statement.

Saturday, July 13, 2013

The break Statement in C

Break statement in C plays an important role in terminating the control statements. In C, we have many control statements. To get out of the statements in the middle, break is used. Break is generally used in looping statements and in the switch...case statements.

Syntax:
break;

Friday, July 12, 2013

Switch Statement in C

The switch statement is multi-decision statement which matches an expression to a set of constant values and branches to the corresponding value.

The switch case statement is placed in place of if...else if we have more number of matches to a variable.

The syntax of the switch-case statement is as shown below.

If... Else Statements in C

If...else is the first and the most widely used Control Statements in C. It is widely used because of its simplicity. If Else Statements is used in three ways. In all the three cases, it just takes some condition and perform some set of statements.

The condition we use is mostly by using the relational operators and the logical operators. We can use a single condition or multiple conditions in a single if statement.

Thursday, July 11, 2013

Control Flow in C

In C, we generally have a flow of statements which are executed step by step. They are performed in the order they are specified. Suppose if we want to execute a set of statements when a condition is satisfied, then the Control Flow comes into play.

Control Flow is nothing but controlling the flow of statements execution. In C, we have different types of Control Flow Statements.

The Comma Operator in C

Comma is generally used to separate two things. In C, along with separating two things, we also use comma as an operator along with separator.

If we use comma as an operator, then the expression evaluates to the right sub-expression.

For example,
x=(a,b);
Then x is assigned the value of b.Here parenthesis are must because comma operator has the lowest precedence than that of assignment. So parenthesis should be used to evaluate the expression.

Conditional Operator in C

Conditional Operator is a special kind of Operator which is used to check whether the condition mentioned is true or not and then perform an action based on the result. It is represented by
cond?exp1:exp2
which means, to evaluate the condition first. If the condition is true, then execute exp1, if it is false, execute exp2.

Wednesday, July 10, 2013

Compound Assignment Operator in C

We know what is Assignment Operator in C. Compound assignment Operator is the advancement of Assigning. Compound assignment operator is the short notation of an operation if the operation is performed on the variable itself.

For example, 
x+=1 means x=x+1
The first notation is compound assignment where as the second notation is the notation we already know. The following table gives you more types of compound assignment Operators.