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.

sizeof() operator in C

sizeof() operator is a unary operator used to find the size of the arrays or the size of a variable or size of structures in terms of bytes. If we use sizeof(variable), it returns the size taken by it in terms of bytes.

Syntax:
sizeof(variable_name);
Let us the application of the sizeof operator in the following example.

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.

What happens if we don't use address-of in scanf

In C, while using the scanf() function, we have to specify arguments with address-of(&) operator to take the values which we give into the variables. But what happens when we don't place the address of operator? Does it return a error or works well?

Check out the following program to understand it well.

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

Format Specifiers in C

In C, we use the format specifiers in both the printf and scanf functions for specifying the type of the variable. Format specifiers are the part which we should be careful in using. We have to specify the exact format specifier for the data type we are using for the variable. If we don't use the appropriate one, it may result in the unexpected results.

The following table represents the most commonly needed format specifiers in C.

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.

Escape Sequences in C

Escape sequences are used mostly in C and C++. It is nothing but a backslash(\) followed by a character. It is meant for doing some function in the output. They are called escape sequences because, backslash makes the C compiler to escape from the normal execution of the string and perform some function.

Here is the list of escape sequences which are generally used in C.

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.

What is spaghetti code

A code with more number of goto statements in C is called as spaghetti code. A spaghetti code is that which is unstructured, twisted and tingled. If we use more number of gotos, then the code is called as Spaghetti code.

Program with Increment Operators in printf()

We have observed a program on Increment Operators when assigned to variable. If not, click here.
We have clearly understand how to analyze the pre-Increment and post-Increment Operators in such a case and ho w the values are manipulated when it is such a case. But what happens when we use the same expression in printf(). Does the result is same or any change occurs?

Consider the following example,

Program on Increment Operator

Most of the people are confused every time in analyzing the Increment Operators because they actually do. We should be careful in analyzing the Pre-Increment and Post-Increment Operators.

In most of the competitive exams, we come across a problem on Increment Operators. We should be careful enough in solving the problems on Increment Operators.

Consider the following example, to understand the Increment and Decrement Operators.