Thursday, February 24, 2011

else if in c

Decision making is done by generally in four ways :
  1. By if statement
  2. switch statement
  3. conditional operator statement
  4. goto statement

if statement is good for decision making as it is very simple and it work on 2-ways decision (either this or that )
syntax
if(Test expression)

If the condition tested in if statement is true then it will execute the body of if statement or it will skip the body of if statement

Example
if (test expression )
{
       body part;
}

There are many types of if statements , depend on on which type of decision to be taken or what conditions are to be checked.

  • Simple if
  • if....else statement
  • Nested if …. else statement
  • else if ladder.
  • Simple if statement is given in the above example.


if....else statement

Structure of if...else

if  (test expression )
     {
         statement for true condition;
     }
else
    {
        statement for false condition;
    }
statements of program ;
While checking the test expression of if statement, for condition is true then if part is executed and for false condition else part is executed.

Nested if …. else statement

Structure of Nested if …. else

if(test expression-1 )
{
       if(test expression-2 )
          {
               statement for true condition;
          }
       else
          {
              statement for false condition;
          }
}
else
   {
         statement last;
   }
statements of program ;

When there are more condition to be checked and program is some what complicated this nested if..else structure is used.

  • Function of it is complicated. It will execute first “test expression-1” of if function , for true condition it will execute “test expression-2” of second if function , again condition is true then it will execute “statement for true condition;” and return “statements of program
  • suppose condition is false for “test expression-1”, then it will return else which contain“statement last;” ,and return “statements of program
  • suppose condition is false for “test expression-2”,and true for “test expression-1”,then it will return else which contain“statement for false condition;”,and return “statements of program









else if ladder

Structure of else if ladder

if(test expression-1 )
      statement-1;
else if(test expression-2 )
      statement-2;
else if(test expression-3 )
      statement-3;
else if(test expression-4 )
      statement-4;
else
      final statement;

statements of program ;

  • In else if ladder the condition are checked from top op to bottom in a sequence ,
  • As if condition is checked first and the followed by else if from top to bottom, if one condition is true,that statement is executed and program again returns to “statements of program ;
  • If all the condition are false the else is executed and final statement is executed as defolt statement
EXAMPLE
#include<stdio.h> // header files
#include<conio.h> //header files
void main()
{
  int marks ;
  clrscr(); //to clear output of previous program
  printf("\n\nenter your marks");
  printf("\n\n i will know grade and I m program\n\n ");
  scanf("\n\n%d",&marks);

  if(marks>=70)
   {
      printf("\n\nyou got distinsion");
   }
  else if(marks>=60)
  {
     printf("\n\nyou got first class");
  }
  else if(marks>=50)
  {
     printf("\nyou got second class");
  }
  else if(marks>=35)
  {
    printf("\nyou have just passed");
  }
  else
   printf("\nyou have failed");
   printf("\n\n\n");
   getch(); // It will accept one character from keyboard to end the program
}

I Hope friends  this may help you to understand the program easily post you suggesion and comments

Monday, February 21, 2011

Constants in C

             Constants refer to the values that are not changed during execution of program in C .There are two types of constants in C language basically :
  • Numeric constants
  • Character constants
Numeric constants are further divided in two parts real constants and Integer constants
Integer constant
An integer constant means the set of digits from 0 to 9 generally. Hexadecimal and octal integer are less in use in C programming .
  • Hexadecimal refer to digit 0 to 9 and alphabets from A to F where A means 10 and F means 15
  • Octal refer to the digit from 0 to 7 .

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

void main()
{
   clrscr();
   printf("This are integer\n");
   printf("%d\n %d\n",3456,-234);
   getch();
}

Output
This are integer
3456
-234




Real Constants
Numbers which are not possible to be representation in Integer form as they change there value or they have fraction part in them for e.g pi = 3.14 or any fraction number such as 15.45 which are in fraction part are known as real constants .
A real number can be executed in exponential form also and the general syntax is as follows
number e exponent
example 0.35e4 , 45e-5
The letter e separates the number from the exponents.
Character constants are are of two types single character constant and string constant .

Single character constant
A single character constant contain one character in a pair of single quote marks. e.g such as 'S' or '45'.

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

void main()
{
   clrscr();
   printf("This are single character constant \n");
   printf("%c\n %d\n",'c','F');
   getch();
}

Output
This are single character constant
c
F
String Constant

  • A string constant is a sequence of character enclosed in double quotes. This character can be letter,number,special characters and blank space .

  • Example is seen in above program “This are single character constant” , this is a string with letters and blank space .
  • Backslash characters are also allowed in the string constants as “\n” is example of this type in the above program

C Tokens and Character set in C


  1. keywords
  2. Identifiers
  3. Constants
  4. String
  5. Special Character
  6. Operators




Keywords
  • In C keyword has a special meaning. It is special word of C which has predefined meaning .
  • there are 32 set of keywords in C Language
  • they are displayed by white colour in  turbo C 

Identifiers
  • It refer to the name of the variable ,function and array. they are user defined name ,consists of sequence of letter's , digits ,and upper case , lower case both type letters are allowed here .

Rules for Identifiers

1 First character must be an alphabet
2 Must contain only letters , digits or underscore
3 Only first 31 character are significant
4 Cannot use a key word
5 must not contain white space  

C constants




  • Constants means one whose value is not going to changed during the execution of whole program.
  • they are generally known as global declared variable
Strings
    • String is a sequence of character that is treated as single data item. When group of character is written in double quotation it is considered as string.
    • string can not be scan directly array of string has to be declared.
    Operators
    • Operator is a symbol that tells the compiler to perform the specific tasks.
    • they decide the priority of the operation to be performed .
    • There are eight type of operators in C language 
    The character in C are categories in four type
    1 letters
    2 digits
    3 special characters
    4 white space
    Compiler do not consider white space till they are not used in string constant







    Sunday, February 20, 2011

    LOOPS IN C LANGUAGE

    Why we use loops in C language or any language ? 

                 Look friends to understand the looping concept you need to know the importance of  loop , loops are use to perform any task repeatedly and take decision according to condition set in looping , loops are always a part of a big program . 

    Lopping in C
    Loops :- Loops generally possess two parts one is body of loop and another is called control statement .Control statement decides the category of loop,there for control statement is classified as entry-control loop or exit-control loop.
    Loops generally posses few steps :-
    • declaration and initialization of condition variable
    • writhing condition statement in loop
    • putting increment or changing condition of variable

    Loops provided by C language are as follows:-
    • While loop
    • Do loop
    • for loop
    While Loop
    format of while loop

    While (test condition )
    {
    Body of loop
    }

    example-

    int i=1 sum=0; /* initialization */
    While (i<=10 ) /* condition */
    {
    sum=i+1;
    i=i+1; /* increment */
    }

    Do Loop
    format of do loop:-

    do
    {
         Body of loop
    }
    While (test condition );
    note:- this will execute the loop 1st time and then from next time will check the condition of while till condition does not goes false .
    Example:-
    int I;
    do
    {
       printf(“Enter any number”)
       scanf(“%d”&i);
       printf(“number=%d”,i);
    }
    While (i>=1);

    For Loop:-
    for loop provides the best control structure and reduce the code length of a user.
    Format of for loop :-
    for (initialization;condition; increment)
    {
       body of loop;
    }

    example:-
    int I;
    for (i=1;i<=5; i++)
    {
         printf(“*”);
    }
    printf(“\n”);

    this will print 5 star in a column every star on new line .   

    Learn to make proper Input/Output operation in C

                 Input/Output operation are very important in C language to  initialize the variable and to put the desired output in C. In every C program there is a use of input/output functions.
              
    There are three types of input/output operations :-
    1. Formatted    Input/Output
    2. Character     Input/Output
    3. String           Input/Output 
      Formated input/output function : 
                   


      There are two function printf() and scanf() 

      printf() function:-
      This function is used to display the output to the screen  the  string which is to be printed as it is usually .
      Syntax
      printf("printable string ",argument1,argument2,....,argumentN);
      • printable string  can be any printable character which is written between " ".
      • It can be also be the character of escape sequence starts with (\).the are generally used for moving cursor
      • formats specifier which starts  with % symbol then format specifier are used to print some useful value
      scanf() function:-
      This function is used to take the input value from keyboard or to initialize the variable at  run time of program 
      Syntax
      scanf("printable string ",&argument1,&argument2,....,&argumentN);

      • For scanf(), function "printable string " is  mostly format specifier with % symbol .
      • &argument1,&argument2,....,&argumentN  are the address of the argument variable , initialized value are stored memory ,which has corresponding address of arguments.
      Character Input/Output function : 

      • This type Input/ Output function is used to get one character at a time from the input device such as keyboard and put output of one character on the screen . 
      • getchar and putchar function are used for such operation in C
      getchar() function:-
      syntax
      variable_name= getchar();

      This function is used to accept one character from the keyboard and it should be of C character set, but character may or may not be printable .

      putchar() function:-
      syntax
      putchar(variable_name);

      This function is used to print one character on the screen and it should be of C character set, but character may or may not be printable .

      String  Input/Output function : 

      • The  function is  used to accept the string from the keyboard and print the string to the screen , String  refer to one whole sentence it can be on word also. 
      • this function reads the string till the newline  character is encountered or enter key is pressed.
      • gets() and puts() are string input/output function .

      gets() function:- 
      syntax
      gets(variable_name)
      It accepts the text till the (\n) newline character is encountered or enter key is pressed,Variable must be a array of string.


      puts() function:- 
      syntax
      puts(variable_name)
      This is used to print the string on the screen , it also assign or appends the (\n) newline character at the end of string. 

            Remember

      • Actually puts() and putchar() function can also be execuated by use of a scanf() function. 
      •  string function do not use &  symbol while passing argument .
      • this are all functions of <stdio.h> header file 








        Thursday, February 17, 2011

        Structure of C program


        How to write a simple C program ?

        Hi , friends C is a very easy and important language to learn. A simple C program consist of some main() 
        function , Header files , and body for the program.

        header files

        main function
        {
        variable;
        executable -program;
        }

        Example
        #include<stdio.h> //header files
        #include<conio.h>//header files

        void main() // main function
        {
        clrscr(); // this will clear the screen remove previous programs output
        printf("hello , I am learning C language\n"); /* this command will print “hello , I am learning C language”*/
        getch();// it allows to see output till you press any key
        }


        this program will print "hello , I am learning C language" in output 

        Rules for writing C program
        • every statement in C ends with semi colon.
        • header files should start with “#”.
        • Every main function program should be written in Braces “{}”.





        Tuesday, February 15, 2011

        Features of C Language


         C is a most popular language today because it is well structured with features of high level language and machine. It was designed for special purpose for creating the Unix operating system. 

         Properties of Language

        • C is a programming language which allows to program from top to bottom .
        • C is a procedure oriented programming language .
        • All function in C share global data .
        • C is a powerful, flexible, portable and good structured programming language .
        • C has features of both high level language and elements of assembler.
        • It is suitable for, system programming as well as application.

        Benefits You get using C language
        • C is programming language simple to implement and highly effective .C is middle level programming language provides high level and low level programming support, gives user assemble level programming 
        • C is general purpose structural language, instruction consist of simple expression and common English words like, for, while, break, etc. 
        • C is a compilable language and can run on all machines. It has a rich build-in-function and it has 32 key words. It is extensible continuously adds library functions supported by C library .
        • C has variety of data-type operators and functions in it that makes it highly effective . 
        • It also supports pointers, that access address of variable from computer memory .It also supports dynamic allocation of memory this allows allocating memory at run time and provides modular programming collection of one or more functions or modules. 

        Draw backs of C

        • C language do not supports inheritance. It do not supports the floating in pointers. 
        • Switch statement in C do not provides facilities to use logical operators. 
        • Pointers are difficult to access in this language and only provides one main memory allocation "mallox".


        Sunday, February 13, 2011

        C Programming Language History


        Dennis Ritchie image not found
         Dennis Ritchie








        C is the programming language which was born at AT&T ,s Bell laboratories in USA in year 1972. Language was constructed by Dennis Ritchie. Evolution of C language was done from Language B which was made by Ken Thomson in 1970 , at Bell laboratories . Actually B language was made in reference from the Language BCPL constructed by Martin Richards at Cambridge University in 1967.

                        C language was the most popular language of that time . So due to popularity of the language new version of languages were discovered which were almost same but in compatible, So system developers had to face many problems . So for keeping C language standard in 1983 , American national standard Institute set a committee for making standard for C . Committee approved a version of C in 1989 which is know as ANSI C . It was then approved by ISO in 1990 It is known as C89, Later on new version of C was approved by Standardization committee In 1999 , it is known as C99many old compilers do not support every features of C99.