Friday, September 26, 2014

LOOPS IN C

                                            DIFFERNT TYPES LOOPS IN C


  What is a loop?
     When we want to execute certain statements repeatedly, loops are used i.e., the statements which must be executed repeatedly must be placed inside loop(body section).

For example if we want to print "c programming" 10 times on screen?

    main()
{
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
    printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
}

if we use the above strategy in printing name times it takes lot of time and burden to programmer.
In this situation we use loops. Basically there are three types of loops
i) for loop   ii) while loop   iii)do-while loop

Loops are categorized into two types.
a) entry control loop (while, for loop)    b) exit control loop(do-while loop)

a) Entry controlled loop: When using this loop the condition is tested first and if condition is true then body inside the loop will be executed.  If condition fails execution comes out from the loop and the statements immediately after the loop will be executed.

i) while loop:
    
     while(test expression)
        {
                       body of the loop;
         }


/* write a C program to print c programming on the screen */
 

  main()
{
     int i=1;
     while(i<=10)
     {
        printf("\n c programming");
        }
}


  output:  c programming
               c programming
               c programming
               c programming
               c programming
               c programming
               c programming
               c programming               
               c programming
               c programming

/* write a c program to print numbers from 1 to 10 using while loop */


  main()
    {
       int i;
        while(i<=10)
        {
           printf("\t %d",i);
         }
  }

output: 1     2     3     4     5     6     7     8    9   10






(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.


 

Tuesday, September 23, 2014

Conditional or ternary operator in C

/* Biggest of two numbers using i) if else statement and ii) ternary operator */

Before going to discuss about ternary operator lets check the below program

/* biggest of two numbers *?
main()
{
   int a,b;
   printf("\n enter any two numbers");
   scanf("%d%d",&a,&b);
   if(a>b)
   {
      printf("%d is big",a);
    }
   else
   {
     printf("%d is big",b);
   }
 }

output: enter any two numbers 10 20
20 is big


In the above program we assigend a value  to 10 and b value to 20
Here 'if' is called decision making statement i.e., based on the decision either 'if' part or 'else' part will be executed.
Here 'else' part will be executed because condition is false(0). So in output we see like "20 is big"
(Note: if condition is true(1) 'if' part will be executed)

Now the same thing can be performed using tenary(condional) operator.   (?  :)



/* biggest of two numbers */
main()
{
   int a,b,big;
   printf("\n enter any two numbers");
   scanf("%d%d",&a,&b);
 
   big=(a>b)?a:b;

   printf("%d is big",big);
}

output:   enter any two numbers 10 20
 20 is big

In the above program output is printed as "20 is big"

We can divide a ternary operator in c into three parts
first part conditiona part(here a>b?)
second part positive part(true part) here a   (equivalent to if part)
third part negative part(false part) here b     (equivalent to else part)
 In the above part a is assigned a value 10 and b is assigned a value 20.
and big=(a>b)?a:b;

In evaluating this expression the compiler starts from left and move to right.
first conditional part will be evaluated i.e., a>b?  ( here condition is false)
then compiler skips the second part amd moves to third part (since condition is false, the compiler moves to thrid part) i.e., big is assigned a value to b.
So output printed is "20 is big"



NOTE: if else statement can be replaced by ternay operator.




c programming, if statments, else if statements, nested if, elsed if ladder, arrays,strings, strucutres, unions, userdefiend data types, bit wise operators, ternary operator, conditional operator, file handling in c, fseek function, ftell fucntion, rewind function, logical operators , switch statements, is ternary statement is equlivalent to if else statements,biggest of two numbers, poitner in c, switch statement in c, decision making in c, control strcutures in c. biggest of three numbers

Saturday, September 13, 2014

printing plain strings with the help of back slash characters


how to print something on screen using C printf() function?

consider a simple code given below-

main()  /* program begins here */
{
  printf("hello this is my first program");
}

if we execute this code we get output on screen as

output : 
hello this is my first program

In c if we want to print something on screen we have to use printf() function which is available in standard library function stdio.h

whatever we give in printf() function in between two inverted comas that is printed same(except some special symbols).

main()
{
  printf("hai Programmers");
}

output: 
hai Programmers


remember that every printf() function in C must be terminated by a semicolon symbol(;).

Now my task is i want to give a tab space in between two fields hi and programmers

main()
{
   printf("hi\tprogrammers");/
}

output:
hi    programmers.

\t has a special meaning in c which gives one tab space when it is encountered by a C compiler 


now my task is i want to print hi in one line and programmers in second line

main()
{
  printf("hi");
printf("\n programmers");
}
output:
  hi
  programmers

\n has a special meaning in c which moves cursor to next line and prints next word or line in next line  when it is encountered by a C compiler 




you may get doubt why i cant print two words in two lines using two printf() statements without using \n symbol. let me explain what happens
main()
{
 printf("hi");
printf("programmers");
}

output:
hiprogrammers

what happens here is after printing first word hi then cursor is at location immediately after hi. so when second printf() function is executed then second word programmers printed immediately after hi.

note:   \t is used for allocating one tab space between two words.
           \n is used to move the cursor next line and prints other lines.
           \a is used to give a beep sound  
            let us compare two things for example in word document and C editor
            

                         action                                   command or key                                  command or key

                                                                      (MSWORD)                                            (C output )
                         to move to new line              press enter key                                             \n
                        to give one tab space             press Tab key                                               \t


lets close our session with this program

main()
{
  printf("hi");
  printf("\n hello\t programmers");
}

output

hi
hello    programmers







Any queries or comments?

(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.
back slash characters,printf statement.

variable initialisation in c programming!

 what happens exactly if one typed as code below?

main()
{
  int a=10;
}

Here the compiler starts begins compiling from main() function.(Every C program begins with main() function )
when a compiler reached third step i.e., int a=10; it allocates 2 bytes of memory in main memory whose name is a and a contains the values 10. What exactly it doing is we can compare it like this-

   suppose a person needs a house.So he needs a space(here 2 bytes) and he needs a name of house(here a) and he stays in house(here 10).

what happens if one typed code as given below?

main()
{
  int a,b=5;
  float c=10.5;
 }

Here the compiler starts begins compiling from main() function and allocates two bytes of memory for a whose value is 10. It allocates four bytes of memory for  c.
But for b it allocates two bytes of memory and we have not given any value to c.
(NOTE: c contains garbage value we discuss later in storage classes about garbage value)




important points to remember:

                                    numbers are divided into two types in c 
                                     integer numbers and fraction numbers. 

           integer numbers are stored in memory using int datatype and c compiler  allocates two bytes of memory.
           fraction numbers are stored in memory as float data type and c compiler allocates four bytes of memory.

  Data types and memory sizes in C.

    datatype name                              size                                    example
     int                                                2bytes                                -123,   0,  345,
    float                                              4 bytes                               -4.5 , 0 , 3.76
   char                                                1 byte                                a, b, c        

int a=10;   /* here a is integer variable whose value is 10*/
float b=4.5;  /* here b is a float variable whose value is 45*/
 char c='a';   /* here c is character variable whose values is a*/
        (note: character variable must be enclosed in single quotes)


If any doubts query us or comment us.



(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.