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.