Pages

Getting Started With C and C++




Getting StarttedIntroduction

Welcome to this blog which is about on C and C++ programming language. Here I shall discuss on this popular language from beginning to professional level. Visiting step by step through this blog, you will able to know how to create professional software with this high-level language. So no more wait…… but before starting, we will be familiar with the background of C and C++ programming language.

 Programming Language 

Language is used to communicate with each other. But programming language is specially used for computer to communicate with its user. As computer understands nothing except 0 and 1, its user should build an instruction set which is combination with 0 and 1 named as machine language. So a compiler is necessary to interpret user’s symbolic word to computer readable instruction set. As a demand, programming language comes out.
From the above discussion, Programming Language is defined as an instruction set, word or code which is readable to the computer and used to solve any practical problem by the computer.

Types of programming language

Programming languages are divided into two types…

  •  Low-level language: This is directly readable by the computer. Such as machine language and assembly language.

  • High-level language: An interpreter is necessary for these languages to translate human readable code to machine readable code. C, C++, lava, C#, Visual Basic etc are the example of high-level language.

  What are C and C++?

  • C which was created by Dennis Ritchie at Bell Laboratories at 1972 is one of the most popular high-level  computer languages. It is treated as a God language of computer.



  •  C++, an extension of C, was developed by Bjarne Stroustrup in the early 1980s to overcome the limitation of C. C++ contains all the functionality of C language with some extra features including object-oriented capabilities discussed later in details in this blog. Today, most of the general-purpose operating system like windows is written in C or C++ language.        


Advantages of C and C++ language

  •   Easy to learn than other high-level programming language.

  •    Platform independent language.

  •   Easy to control both hardware and software.

  •     Both modular and object-oriented programming capability.

  •    Operating system programming capability.

  •   Capable of programming both microcontroller and microprocessors.

  •    The best hardware friendly programming language.

  •    More Speedy.

For the above functionalists, C and C++ are the most popular modern language.

Programmer view on C and C++

There are a lot of computer languages in programming. All of these, C and C++ provide easy syntax and functionality. So, at the beginning most of the programmers suggest to start with C and C++ in programming world. I also started my programming concept with this language and it is one of my favorite languages. I think you are lucky if it is your starting point in programming.   
  
Prerequisites to learn C and C++
  • Educational quality: No specific certificate is necessary. Anyone who is eligible to read this tutorial is able to learn C and C++.
  •  Operating quality: Some basic knowledge about windows operating is suggested but not so necessary for this tutorial. Playing with C and C++ anyone can be familiar with the operating system.  
  • Necessary software: A C++ compiler and an IDE (Integrated Development Environment) are strongly necessary. There are a lot of IDEs to edit and compile program but among these we will use a Microsoft’s product MicrosoftVisual Studio 2008. I shall give you a short description how to download and install Microsoft Visual Studio 2008 to your operating system and program at it in later section.
Now we ready to start our first program in C and C++. So no more wait. You do make a lot of tolerances. We are just going to start our first program in our favorite language to the next pages.   

Click to open first program in C and C++

Finding Maximum Logical Drives With C Language

Problem: How to know current and maximum logical drives.
Solution
This is a program to know current drive’s name and the maximum logical drives to the system. Here getdisk () function is used to know current drive and setdisk () function is used to know maximum logical drives.  Programming code goes below →

#include<stdio.h>
#include<conio.h>
#include<dir.h>

{
  int disk, maxdrive;
  disk=getdisk()+'A';
  maxdrive=setdisk(2);
  printf("Current drive is : %c\n",disk);
  printf("Maximum logical drive is:%d\n",maxdrive);
}

Output  

Two Dimensional Array Program

Problem:  How to find boundary elements and higher and lower level of a matrix.

Solution 

This is a program to find boundary element of a matrix. It also finds higher level and lower level of the given matrix. Programming code is given bellow→  


#include<stdio.h>
#include<conio.h>
{
  int a[5][5],m,n,sum=0;
  printf("Enter row :");
  scanf("%d",&m);
  printf("Enter coloum :");
  scanf("%d",&n);
  printf("Enter matrix element :\n");
  {
            for(int j=0;j<n;j++)
            {
              printf("a[%d][%d]=",i,j);
              scanf("%d",&a[i][j]);
            }
  }
  printf("\nMatrix is : \n");
  {
            for(int j=0;j<n;j++)
            {
              printf("%d\t",a[i][j]);
            }
            printf("\n");
  }
  printf("\nBoundary element :");
  {
            for(int j=0;j<n;j++)
            {
              if((i*j==0)||(i==m-1)||(j==n-1))
              {
                        printf("%d  ",a[i][j]);
                        sum=sum+a[i][j];
              }
            }
  }
  printf("\nSum of boundary element is : %d",sum);

 printf("\nHigher level : ");
 sum=0;
 {
   for(int j=0;j<n;j++)
   {
             if(i==j)
             {

               printf("\t%d",a[i][j]);
               sum=sum+a[i][j];
             }
   }

 }
  printf("\nSum of higher level = %d",sum);

  printf("\nLower lavel : ");
  sum=0;
  i=m;
  {
            i=i-1;
            printf("\t%d",a[i][j]);
            sum=sum+a[i][j];
  }
  printf("\nSum of lower level = %d",sum);
}


Output



Array sorting Program

Problem:  How to sort an array’s elements.
Solution
This is a program to sort an array’s elements. Here we have used Bubble Sort algorithm. First we will take the array’s elements. For this we have used scanf() function. Then we will sort the array’s elements using Bubble Sort Algorithm. The programming code is given bellow→

#include<stdio.h>
#include<conio.h>
  {
            int a[20],N;
            clrscr();

            printf("How many item:");
            scanf("%d",&N);
            printf("Enter items :\n");
            for(int i=0;i<N;i++)   // Taking array’s elements
            {
                  scanf("%d",&a[i]);
            }
            printf("Array is :");   // Showing  array’s elements
            for(i=0;i<N;i++)
            {
                  printf("%d  ",a[i]);
            }
// sorting array’s elements
            for(i=0;i<N;i++)  
            {
              int ptr=0;
              while(ptr<N-1-i)
              {
                if(a[ptr]>a[ptr+1])
                 {
                         int temp=a[ptr];
                         a[ptr]=a[ptr+1];
                         a[ptr+1]=temp;
                 }
                        ptr=ptr+1;
              }
            }

            printf("\nSorted array :");  // Showing sorted array’s elements
            for(i=0;i<N;i++)
            {
                   printf("%d  ",a[i]);
            }
  }

Output

Remark
By using this program we can sort an array’s elements and it is a so easy algorithm.

Showing a Clock on the screen using C Language

Problem: How to show continuous time on the screen   

Solution

To show continuous time on the screen we can use gettime() function. gettime() function accepts a structure variable reference. Here we use structure type time to provide this reference.   The programming code for showing continuous time is given below

 

#include<stdio.h>
#include<conio.h>
#include<dos.h>

{

 struct time t;
 do
 {
  gettime(&t);
  gotoxy(30,10);
  textcolor(2);
  cprintf("Time: %2d:%02d:%02d",t.ti_hour, t.ti_min, t.ti_sec);
  delay(1000);
 }while(!kbhit());

}


Output

Remark
This is a program to show continuous time until a key is pressed. If we want to show a clock on the screen then we can use this program.

Showing Date and Time with C Language

Problem:  How to show executing Date and Time   
 
Solution

It is easy to show processing date and time on the output screen. To show date and time you will just use __DATE__ and __TIME__ macro. These macros show output at string literal. Be sure that you use upper case letter for macros and double underscore before and after the macro’s name. The programming code goes below→ 


#include<stdio.h>
#include<conio.h> 

{
    clrscr();
    textcolor(6);
    cprintf("Showing Executing Date and Time \n");
    textcolor(2);
    cprintf("    Date: %s ",__DATE__);
    textcolor(4);
    cprintf("    Time: %s ",__TIME__);
    getch();
}

Output



Remark     
By running this program you can see the current Date and Time of your local PC time. For cprintf() function you can see color formatted output.  textcolor() function gives the specific color.
 

Sorting and Searching Program with C/C++ Language

Problem: How to make Sorting and Searching Program.

Solution

Sorting and Searching is an important task in programming. Here we will see such a C Program. This program is used to sorting and searching a string array. Running this program we will provide names and given names are sorted and searched by the program. Programming code is given bellow→

#include<stdio.h>  
#include<conio.h>
#include<string.h>

  struct name            // Custom Data Type
  {
      char a[10];   
       
   }b [10];                 // Custom data type’s variable


  int N,i,j;
  int c=0;
  char name[10];
  printf("How many names :");
  scanf("%d",&N);  fflush(stdin);
  {
   printf("Enter %d Name :",i+1);
   gets(b[i].a);               
  }

  printf("\n\nEntered name list :");
  {
            printf("\n%s",b[i].a);
  }

  {
            for(j=i+1;j<N;j++)
            {
             if((strcmp(b[i].a,b[j].a))>0)
               char temp[10];
               strcpy( temp,b[i].a);
               strcpy(b[i].a,b[j].a);
               strcpy( b[j].a,temp);
             }
   }
  }
  printf("\n\nSorted name list :");
   {
             printf("\n%s",b[i].a);
   }



 printf("\n\nEnter name to search :");
 gets(name);
 {
   if((strcmp(b[i].a,name))==0)
   c++;
 }
 if(c==0)
 {
   printf("%s not found .",name);
 }
 else
 {
   printf("%s remains %d times .",name,c);
 }
 }

Output





Remarks

After giving  name (string) we can see the list of name and then sorted list. When we will give a name and search, we will see that name if it is in array otherwise name not found on the screen. Here strcpy ()  function is used to copy one variable’s value another variable and strcmp() function is used to compare two string. fflush(stdin) is used to flush buffer.