Donate

1 String - Array and String

Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0.
Because C has no built-in facilities for manipulating entire arrays (copying them, comparing them, etc.), it also has very few built-in facilities for manipulating strings.
In fact, C's only truly built-in string-handling is that it allows us to use string constants (also called string literals) in our code.
Whenever we write a string, enclosed in double quotes, C automatically creates an array of characters for us, containing that string, terminated by the \0 character.
For example, we can declare and define an array of characters, and initialize it with a string constant:
char string[] = "Hello, world!";
Two ways to initilize string >



In this case, we can leave out the dimension of the array, since the compiler can compute it for us based on the size of the initializer.
This is the only case where the compiler sizes a string array for us, however; in other cases, it will be necessary that we decide how big the arrays we use to hold strings.
An example program showing the character data type array:


Align CenterOut put of the program



In the above example, a character based array named word is declared, and each element of array is assigned a character.
The last element is filled with a zero value, to signify the end of the character string (in C, there is no string type, so character based arrays are used to hold strings).
A printf statement is then used to print out all elements of the array.

If you enjoyed this post, make sure you subscribe to my RSS feed!


0 Function Definition - Function and Recursion

Functions are self contained program segments that carry out some specific well defined task.

In "C" , we include the header files like stdio, conio, string, etc.

These files contain number of library functions which are as follows:

printf( ), scanf( ), getchar( ), putchar( ), getche( ), gets( ), puts( ), strlen( ), getch( ), etc.

In C it is possible for the function to call themselves. we will see how it can be possible through Recursions.

A Function is a self-contained block of statement that perform a coherent task of some kind. Every C program must have a function. One of the function must be main().

Why should we use Function?

a) Using function it becomes easier to write programs and keep track of what they are doing

b) Length of the program can be reduced by using function.

c) Debugging is easier.

d) It facilitates top-down modular programming.

Classification of Function

C function can be classified into two categories:

1. Library function:

They are predefined in the standard library of C. We need to include the Library.

2. User-defined function:

User defined functions are need to be developed by the user at the time of program writing.

If you enjoyed this post, make sure you subscribe to my RSS feed!

0 Function Declaration - Function and recursion

Before defining the function, it is desired to declare the function with its prototype.

In function prototype, the return value of function, type, and number of argument are specified.

Function declaration is written in following ways:

return data_type function_name (data_type argument 1, data_type argument 2 ................data_type argument n) { local variable declaration; executable statement 1; executable statement 2 .................................. ................................ executable statement n; return(expession); }

Note: An empty pair of parenthesis must follow the function name if the function definition does not include any arguments.

Example:

If it returns a float than command is:

float f1(float arg 1, int arg 2);

if it returns no value or return a character, then replace float by void or char respectively. If no arguments passed into a function than command is:

char fun1 ( );

An example program using a small add function:



Out put of the program




If you enjoyed this post, make sure you subscribe to my RSS feed!


0 Function Call - Function and Recursion

Function can be called either by value or by reference .A function can be called by specifying its name followed by a list of arguments enclosed in parentheses and separated by commas.
From the above example the following line is used as a function call:
sum=add(a,b); /* function call */
Call by value: Call by value means directly pass value within the function.
An example program of call by value:



Out put of the program




Call by reference: Call by reference means sending the addresses of the argument to the called function.
In this method the addresses of actual arguments in the calling function are copied into formal arguments of the called function. (later discussed in pointer section)
The Return Statement
Information is returned from the function to the calling portion of the program via return statement.

General form of return statement:
return;
or
return(expression);
Storage Classes
There are two different ways to characterize variables:
1. By data type
2. By storage Class
Data type refers to the type of information while storage class refers to the life time of a variable and its scope within the program.
A variable in c can have any one of the four storage classes:
1. Automatic Variable:
It is created when the function is called and destroy when the function is exited. Hence the name is Automatic. By default a variable is declared Automatic.
2. External Variable:
It is also known as Global Variable. This variables can be accessed from any function that falls within their scope.
3. Static Variable:
A static variable may be either internal or external type , depending on the place of declaration.
Internal static variable extends up to the function in which they are defined and external static variable is declared outside of all function and is available to all the functions in the program.
4. Register Variable:
We can tell the compiler that a variable should be kept in one of the machine's registers, instead of keeping in the memory (where normal variables are stored ).
Since, a register access is much faster than a memory access and keeping the frequently accessed variables in the register will lead to faster execution of programs.

If you enjoyed this post, make sure you subscribe to my RSS feed!

0 Recursion - Function and Recursion

Repetitive calling of the same function is called recursion. Recursions are those functions which call itself again and again. Recursive functions can easily become infinite loops.
An example program to find out the factorial of any number is the one of the best example of recursion:




Out put of the program


If you enjoyed this post, make sure you subscribe to my RSS feed!