Thursday, 1 February 2018

What is Strings in C language?

Welcome back! In my last post, I tell about pointers in C after that I want to tell you about String in  C that is used to suggest strings in which the stored data does not represent text. So let's start...

Definition and Concepts:

  • A string is a sequence of symbols that are chosen from a set of alphabet. A string is, essentially, a sequence of characters.
  • A string is generally understood as a data type storing a sequence of data values, usually bytes, in which elements usually stand for characters according to a character encoding, which differentiates it from the more general array data type.
  • When a string appears literally in source code, it is known as a string literal and has a representation that denotes it as such.
       For example,
       char unit;
       unit='F';       /*for Fahrenheit*/
       ...........
       unit='C';      /*for Centigrade*/
  • Most codes and ID's require more than one character. In mailing addresses, two characters are used to identify a state and five characters are used to identify a zip code. They could be stored in character array as follows:
       char state[2], zip[5];

Declaration of Strings:

In C, a character array is declared using the basic data type char and the derived data type array.

Syntax:  char StringName[array_size];

For example, char chararray[n];

The data type here is char, the name of the array is chararray, and n is the number of elements.

Standard Library Functions:

The most commonly used functions in the string library are:

strlen()
gets string length
strcat()
Concatenate two strings
strncat()
Concatenate one string with part of another
strcpy()
Copy a string
strcmp()
Compare two strings
strrev()
Reverse the string
strlwr()
Changes the string from upper to lowercase
strupr()
Changes the string from lower to uppercase
strstr()
Finds location of first substring in the string

Thursday, 25 January 2018

What is Pointer in C language?

Welcome back! In this post, I want to tell you about Pointers in C. It used to refer to memory location of another variable without using variable identifer itself. So Let's start....

Definition:  

Pointer are widely used in programming. With pointer we can perform lot many operations by using memory addresses. It is possible to access and display the memory location of a variable using '&' operater.
A pointer variable holds the memory address of any type of variable. The pointer variable and normal variable should be of the same type. The pointer is denoted by '*' symbol.

Features of Pointers:

  • Pointer are more efficient in handling arrays and data tables.
  • Pointers can be used to return multiple values from a function via function arguments.
  • The use of pointer arrays to character strings results in saving of data storage space in memory.
  • Pointer allow C to support dynamic memory management.
  • pointer reduce length and complexity of program.
  • They increase the execution speed & thus reduce the program execution time.

Declaration of Pointer:

C programming language supports pointers to different data types such as: int, float, double, char, etc. Every pointer can point to either a single value or to multiple values. In C a pointer variable is declared the * operater.

Syntax:  int*pi;           /*pi is an integer pointer*/
              float*pf;       /*pf is a float pointer*/
              char*pc;      /*pc is a character pointer*/

For exemple, Consider the following program segment where a is an integer variable, pi is a pointer to an integer variable and &a gives the address of the integer variable a:
                    int a=10;
                     int *p;
                    p=&a;
After compliation, we have the values in the variables as shown in figure 1:

After execution of the first statement, i.e. p=&a; the values of the variables will be as shown in figure 2:

 

Tuesday, 23 January 2018

Type of Arrays in C language

Welcome back! In my last post i tell you about C Array after that i want to explain you the types of C Arrays. So let's start....

There are three types of Array,
1) One-dimensional Array
2) Two-dimensional Array

One-dimentional(1D) Array:

A  list of items can be given a common name and the individual items can be referenced using only one subscript. Such variable are referred to a single subscript varriable or a One dimensional array.
The liner arrays are also called one dimensional arrays, since each element in the array is referenced by a single subscript. The one dimensional array is the array in which the elements are represented in single row.

1) Declaration of C Array: 
We can declare an array in c language in the following way:

data_type array_name[array_size];
Now, Let's see the example to declare an array.
int mark[5];
Here, int is the data_type, mark is array_name and 5 is array_size

2) Initialization of C Array:
A simple way to initialize array is by index. Notice that array index starts from 0 and ends with [SIZE - 1].

mark[0]=80;//Initialization of array
mark[1]=75
mark[2]=85
mark[3]=65
mark[4]=90

Example of 1D Array:


#include<stdio.h>

int main()
{
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
//end of for loop
return 0;
}

Two-dimensional(2D) Array:


Array are used to store the data item in the form of a list by using the one-dimentional array, but some time we have to store the data in the tabular form, then for storing data in the tabular form C provides the concept of two-dimensional array. The two-dimensional array is also known as a matrix.
A 2D array is a grid having rows and columns in which each element is specified by two subscripts. It is the simplest of multi-dimensional arrays.

1) Declaration of 2D Array in C:
We can declare an array in the c language in the following way:

data_type array_name[size1][size2];

A simple example to declare two dimensional array is given below:
int twodimen[4][3];
Here, 4 is the row number and 3 is the column number.

2) Initialization of 2D Array:
A way to initialize the two dimensional array at the time of declaration is given below:
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

Example of 2D Array:

#include<stdio.h>
int main()
{
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++)
{
 for(j=0;j<3;j++)
{
   printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
 }
//end of j
}
//end of i
return 0;
}

Sunday, 21 January 2018

What is Array in C language?


Welcome back! In this post I will explain a very important concept that is Array. It is used for storing a single data item programmers uses variables; but in many applications, programmers need to store a large amount of data, thus for storing large amount of data we need to declare large number of variable which is not convenient. And these variables are independant and not related with each other, so for storing large amount of data C provides the concepts of Array.

Array means collection. An aaray is used to store elements of the same type. It is a very popular and useful data structure and stores elements in contiguous locations. More than one element is arranged in sequence so it is also called a composite data structure.

Array is a liner and homogenous data structure. Homogenous means that the same types of elements are stored in it. It can be combined with a non-homogenous structure and a complex data structure can be created.
For example, when we declare a variable,
 int x;
The variable x is declared and a memory location of two bytes is allocated to it. Later, a single value can be stored in it.

Every variable has a name, value and memory location. Hence, from the above, we can say that only value can be stored in a variable.

Friday, 19 January 2018

What are Loop Structures?

Welcome back! In my last post, I explain about functions and Types of functions. After that you should know about Loop structures also known as Statement that is used for making long programs into simple and short programs. So let's start.....

C has three loop constructs:
1) For loop
2) While loop
3) Do-while loop
The first two are pre-test loops and do-while is a post-test loop. In the post_test loop, the code is always executed once.

1) For Loop: Allow a set of instructions to be repeatedly executed until a certain condition reached. This condition may be predetermined or open ended.

Syntax:
           for(initialization; condition; increment)
           {
             statement;
           }
Here is a example program, you can try to write this yourself;
Program: For generation of multiplication table as for demonstration.
   #include<stdio.h>
   #include<conio.h>
        void main()
     {
          int row, column;
          puts("\t\t My Handy multiplication table");
          for(row=1; row<=10; row++)
                {
                  for(column=1; column<=10; column++)
                  printf("%6d", row*column);
                  putchar("\n");
                }
           getch();
     }

2) While Construct: While loop check the test condition at the top of the loop, which means that the body of the loop will not execute if the condition is false to begin with. This feature may eliminate the need to perform a separate conditional test before the loop.

Syntax:
              While(condition)
            {
               statement;
            }

3) Do-while construct: Unlike For loop and While loop, which test the loop condition at the top of the loop, the do-while loop checks its condition at the bottem of the loop. This means that a do-while loop always executes atleast once. The general form of the do-while loop is given below:

Syntax:
              do
             {
                statement;
             }
               while(condition);


Tuesday, 16 January 2018

Types of C functions

Welcome back! My last post, I will explain about C functions. In this post i will tell you about types of C functions and Function declaration. So let's start.....
In C, there are two kinds of functions:

  • Standard Functions(library functions): The functions that are inbuilt in the language compiler are library functions. printf() and Scanf() belongs to the category of library functions.
  • User-Defined Functions: These are actually the user modules that are deliberately created for a specific purpose. This creation of modules results in function. Various programming constructs like looping, jumping are used with it.

Function Declaration:

A function decleration statement is also known as prototype of the function or as function prototype. Function declaration consists only of a function header; it contains no code. Function header consists of three parts:

Syntax:
             return_type Function_name(argument list);

1) Return_Type: If the function has no arguments, then write void in the parentheses, else the specific return type is mentioned. If the function has two or more arguments, each is separated by comma.
2) Function_name: It is given by the programmer as in case of variables.
3) Argument list: It is not mandatory(essential) to include the names of the arguments in the argument list. The purpose of the argument list is to tell the compiler the number, data type and order of arguments. The names of arguments do not need to be the same in the function declaration and the function definition. On the other hand, if the data types do not match, the compiler will flag an error. The compiler checks the data types of the arguments in the function call to ensure that they are the same or atleast compatibility.
    A semicolon follows the function header. Function declarations also provide an excellent quick reference for functions used in the program, making them excellent documentation.
    For example,
                       int largest(int a, int b, int c);
Explanation: Above function definition tells the compiler that the return type of the function is int, name of the function is largest, and it has three arguments, all of type int. The name of argument are not significant.

Monday, 15 January 2018

What is Function?



Welcome back! In my last post, I tell you about C tokens after that we need to know about Function that is very important in C/C++ language, so let's start....

Every C program consists of one or more functions. One of these functions must be called main. Execution of the program will always beign by carrying-out the instructions in main. Additional functions will be subordinate to main, and perhaps to one another.
Generally, a function will process information that is passed to it from the calling portion of the program, and return a single value.
Information is passed to the function via special identifiers called arguments(also called parameters), and returned via the return statement. Some functions, however, accept information but do not return anything(of type void), whereas other functions return multiple values, with help of pointers and arguments(e.g., library function scanf).

Body of a function:

  • The general structure of the general function is:
    Functiontype  FunctionName
    {
      Statement 1;
      Statement 2;
    }
  • The function body is the bit between the opening and closing braces that the line where the function name appears. The fuction body contains all the statements that define what the function does.
For example, consider the following main function having only two statements:
  
    {                                 /*This marks the beginning of main()*/
         printf("Hello");    /*This line displays a quotation*/
         return 0;              /*This returns control to the operating system*/
    }                              /*This marks the end of main()*/