Thursday, December 01, 2011

BASICS

Introduction

C is a programming language which was developed by Dennis Ritchie at Bell Laboratory in 1972. Many parts of Windows, Unix, Linux are written in C. In the programming world C holds a major part. To learn C#, C++ or Java one must know C well.

First Program :

#include"stdio.h"
main()
{
       printf("Hello Friends ! \n");
}

Compile the program and then execute it.

#include"stdio.h"

It is a pre-processor directive, to use some functions like printf(), scanf() etc this directive is used. This should be written at the beginning of the program.

main()

All sets of statements are written under main() function starting with { and closing with }
For example :
                              main()
                              {
                                  statement 1 ;
                                  statement 2 ;
                              }

printf()

This is a function which is always used to achieve the output to the screen.
For example :
                      printf("Hello");         // Only Hello will be displayed.

                      printf("\nHello");      /* Hello will be displayed in the next line.
                                                         \n is called new line. */

                      printf("\nSum of 1+1 = %d ",1+1);   // Sum of 1+1 will be displayed.

In C all statements are ended with ; or it can be called as statement terminator after that we can write anything for our own understanding just putting  // before the line. For multi-line comment we can write within :
   /*
       As much
       line we can
       write.
   */

There are 32 keywords in C which are :

auto           break          case              char
const        continue      default             do
double        else           enum             extern
float            for             goto                if
int              long           register           return
short         signed         sizeof             static
struct         switch        typedef           union
unsigned     void           volatile            while

Among which break, case, char, default, do, double, else, float, for, goto, if, int, long, return, short, switch, void and while are very commonly used keyword in basic programming.

Declaration

Besides keyword there are Constant and Variables. Any Variables used in a program has to be declared before using it in a statement and while declaration we can also initialize it.
For example :
                       int a = 5 ;
  • int is one of that 32 keywords, ' a ' is the variable which is initialized with value 5.
  • So a short definition of int would be, it is used to declare a variable which will be initialized with a whole number.
  • Just like that, float is used to declare a variable which will be initialized with a decimal number.
  • And char is used to declare a variable which will be initialized with a single character.
  • Memory allocation for int is 2 byte, float is 4 byte and char is 1 byte.
  • Variable names should not be same with each other in a program.
Time for few examples :

#include "stdio.h"
main()
{
        int a ;

        printf("Value of a = %d \n",a);   // A garbage value will be displayed.

        a= 5;   // a is initialized with a integer value.

        printf("After initializing value of a = %d \n", a);

        float b = 5.5;   // b is initialized with a decimal value.

        printf("Value of b = %f \n", b);

        char c = 'x';   // c is initialized with a single character.
                            //  That single character should be written within ' '

        printf("Value of c = %c \n", c);

}

After executing the program the following output will be shown :


Same program above can also be done in different way as shown below :

#include "stdio.h"
main()
{
        int a;
        float b = 5.5;
        char c = 'x';

        printf("Value of a = %d \n",a);

        a= 5;

 printf("After initializing value of a = %d \nValue of b = %f \nValue of c = %c \n", a, b, c);

}

The output will be same.

  • %d is used to print integer value.
  • %f is used to print real value.
  • %c is used to print character.
For example :

#include "stdio.h"
main()
{
        int a=5;
        float b = 5.5;
        char c = 'x';

        printf("  %d  \n  %f   \n  %c  \n  ", a, b, c);

}

Here %d is responsible for a, %f for b and %c for c.
Variable names should be written respectively.

printf("  %f  \n  %d   \n  %c  \n  ", b, a, c);   // is correct.
printf("  %d  \n  %f   \n  %c  \n  ", b, a, c);   // is wrong, it will not show any syntax error but values will be displayed wrong.

Addition, Subtraction, Multiplication and Division of Two Numbers :

#include "stdio.h"
main()
{
        int a, b, c;

        a=10;
        b=5;

        c=a+b;
        printf("a + b is = %d \n", c);

        c=a-b;
        printf("a - b is = %d \n", c);
       
        c=a*b;
        printf("a * b is = %d \n", c);

        c=a/b;
        printf("a / b is = %d \n", c);
 }

Or in short it can be written as :

#include "stdio.h"
main()
{
        int a=10, b=5;
         
printf("a + b is = %d \na - b is = %d \na * b is = %d \na / b is = %d \n", a+b,a-b,a*b,a/b);

}

In both the cases output will be same.

When we can't estimate whether the processing result will be real or integer in case of division, we can write the program as shown below :

#include "stdio.h"
main()
{
        int a=26, b=5, c;   // c is declared as integer.
        c=a/b;
        printf("a / b is = %f \n",(float)c);
}

scanf()

As we know printf() is used to output the result on sceen like that scanf() is used to take input from keyboard while the program is running.
Syntax for scanf() is :

scanf("%d",&a);

Here every thing are familiar with us except & which is called ampersand which has to be given before the variable name. It gives the location of the variable where the value taken from keyboard has to be assigned.
For example :

#include "stdio.h"
main()
{
        int i;
        printf("Enter a integer number for i : ");
        scanf("%d",&i);
        printf("Integer number is : %d \n ", i);
}

If i was declared as float then the statement would be :
scanf("%f",&i);

Some Examples of Basic Programs

Swap of Two Numbers :

#include"stdio.h"
main()
{
    int a,b,c;

    printf("Enter value for A : ");
    scanf("%d",&a);

    printf("Enter value for B : ");
    scanf("%d",&b);

    c=a;
    a=b;
    b=c;

    printf("\nA=%d    B=%d \n",a,b);
}

Converting Degrees From Fahrenheit to Centigrade :

#include"stdio.h"
main()
{
    int f;
    float c;

    printf("Enter value in Fahrenheit : ");
    scanf("%d",&f);

    c=5*(f-32)/9;

    printf("Centigrade : %f \n",c);
}

Demo of Power Function :

#include"stdio.h"
#include"math.h"
main()
{
    int a,b,c;

    printf("Enter value in A : ");
    scanf("%d",&a);

    printf("Enter value in B : ");
    scanf("%d",&b);

    c=pow(a,b);
    printf("A (%d) to the power B (%d) is: %d \n",a,b,c);
}

pow(x,y) is a function which is defined under "math.h" header file, to use this function in a program we have to include that header file at the beginning just like "stdio.h".

Demo of Square Root Function :

#include"stdio.h"
#include"math.h"
main()
{
    int a;
    float c;
    printf("Enter a Number : ");
    scanf("%d",&a);

    c=sqrt(a);
    printf("Square Root of %d is %f \n",a,c);
}

This function is also defined under math header file, to use this "math.h" has to be included at the beginning of the program.

Increment :

#include"stdio.h"
main()
{
    int a=1,b=1;

    printf("Value of A is : %d \nValue of B is : %d \n",a,b);
 
    a++;   // Post Increment
    ++b;   // Pre Increment

    printf("After Increment \n");
    printf("Value of A is : %d \nValue of B is : %d \n",a,b);
}

a=a+1; can also be written as a++; or ++a which are called Post Increment and Pre Increment respectively.

1 comment:

Followers