Saturday, April 2, 2016

Structure Data Type Example

Structure Data Type Example 


#include
#include
#include


struct Date {
int Month; /* Members */
int Day;
int Year;
};

struct Date AddDecade(struct Date);

int main(int argc, char *argv[])
{
struct Date BDay;

char buffer[50];

printf("What month were you born? ");
BDay.Month = atoi(gets_s(buffer, 50));

printf("What day were you born? ");
BDay.Day = atoi(gets_s(buffer, 50));

printf("What year were you born? ");
BDay.Year = atoi(gets_s(buffer, 50));

printf("You were born on %d, %d, %d?\n", BDay.Month, BDay.Day, BDay.Year);

BDay = AddDecade(BDay);

printf("You will be 10 years older on %d, %d, %d\n", BDay.Month, BDay.Day, BDay.Year);
_getch();

}

struct Date AddDecade(struct Date Target) {

Target.Year += 10;
Target.Month += 100;
return Target;


}

Wednesday, March 23, 2016

a value in an array is passing to a function Example

                   value in an array is passing to a function Example
Same as the previous program but the simple difference which is Red colourly quoted pointer calling 

#include
#include

int array_test(int arra[]);
int main ()
{

     int line[5] = {10,20,30,40,50};
array_test(line );
return 0;
getch();
}

array_test(int arra1[])
{
int i;
printf("arra value is  ");
for(i=4; i>-1; i-- )
          printf(" _%d ",*arra1++);
return 0;
}

a value in an array is passing to a function Example

                         A  value in an array is passing to a function Example 

#include
#include

int array_test(int arra[]);  // function
int main ()
{

     int line[5] = {10,20,30,40,50};        // array value named line
array_test(line );                           // array value passed to the function by function call
return 0;
getch();

}

array_test(int arra1[])        // function and declared an array named as arra1 to take the value from line
{
int i;
printf("arra value is  ");
for(i=4; i>-1; i-- )
          printf(" _%d ",arra1[i]);
return 0;
}


Tuesday, March 15, 2016

getbit() function K&R 2.9

                                    getbit() function 










#include
#include
int getbit(unsigned number, int position,int width )
{
return(number>>(position+1-width)) & ~(~0<

}

 int main ()

   {       unsigned int a ;
   
    a = getbit(0xA7,6,4);
    printf(" The extracted bit is %d = ", a);
    return 0;
    getch();
   

   }

Sunday, March 13, 2016

atoi(char s[]) function

                                    atoi(char s[]) function 


#include
#include

int atoi( char s[])
{
int n,i;
 n = 0;
 

for(i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
 n=  10 * n + (s[i] - '0');
 return n;
}

int main()
{
int c;
char s[]= {'3','4','9'};
c= atoi(s);

printf("%d",c);
 
getch(); 
return 0;

}

Friday, March 11, 2016

Use of enum data type

                   Use of enum data type  example  

#include
#include
            enum portselector { port1 = 1, port2,port3,port4
                                              } selected;
void port(enum portselector);

int main(void)
{
   int data;
   printf("Enter the port you want  -Port1=1,...,Port4 =4\n");
   scanf("%d", &data);
    
   selected=data;
   port(selected);
   printf("\n\nGiven Number is  = %d  ",data);
   
   getch();
   return(0);
}
void port(enum portselector selected)
{
   switch (selected)
   {
      case port1:
         printf("port1 selected\n");
         break;
      case port2:
         printf("port2 selected\n");
         break;
      case port3:
         printf("port3 selected.\n");
         break;
 case port4:
         printf("port4 selected\n");
         break;
      
      default:
         printf("no port available hi hi hi  only 4 ports .\n");
   }
}



 whenever the function pass the value (int data = selected)  the switch case check the number .

Monday, March 7, 2016

File Coping

Read character and print until the end of file EOF 

Here the program to copy or read by getchar() function And  the words or character  print it by putchar() function 

#include
#include

int main()
{
char a;

while((a=getchar()) != EOF)
putchar(a);
getch();
return 0;


}

See the out put 



Use of getchar() ,putchar() functions

     
                             Use of getchar() ,putchar() functions

The use of getchar function is to capture or get the data / the typed character from (single chara) keyboard .

the putchar() is do just opposite of getchar()

It prints directly to the screen , so we dont need use the printf() function .

Look the program and output

here used getchar() function  to get char.
putchar() and printf() function is used to print the given data.






Uses of for loop

                                                        Uses of for loop 



#include
#include

int main()
{

int far;
for(far = 0 ; far <= 300; far +=20)
printf("%3d %6.1f\n" ,far ,(5.0/9.0) * (far - 32));
getch();

return 0;
}

Char Data Type

 the char is used to storing  simple character  

 Unsigned: It consist only positive value i.e 0 to 255.
Signed: It consist both negative and positive values but in different formats like
-1 to -128
  • 0 to +127
And this all explanation is about 8 bit number system
Char has 1 byte only 

int main()

  char a ;

etc 

Working of a floating point data type

Working of a floating point data type 


Look  and understand the program and output shown below  simple understand what will
happen there




#include
#include

 int main(){

float far = 10;
         
       printf("%1.0f\n", far );
  printf("%2.0f\n", far);
  printf("%3.0f\n", far);
  printf("%4.0f\n", far);
  printf("%5.0f\n\n", far);

   printf("%1.1f\n", far );
  printf("%1.2f\n", far);
  printf("%1.3f\n", far);
  printf("%1.4f\n", far);
  printf("%1.5f\n", far);
   
getch();
return 0;
}







Working Of while loop

 The while loop operates as follows , the condition is tested ,if the it is true .
That is the condition is true that means

for example
            10 == 10 . or 10<20 .="" etc="" p="">If it is true the statement in the while loop will be work or execute 


while(i < j )
{
  stament for exection

}



Example
 
   while(far <= upper )

     {  

       celsius = 5 * (far - 32)/9;

       printf("%d\t%d\n", far ,celsius);

       far += step;

  }


it will execute till until the condition will get false  .

that is the far not equal to upper 


NB : 

   The body of the while loop may be single or more line .
  If there is a single line there is no need to add a the curly {} bracket 

Eg : 
       while(i < j)
        i = 2 * i ;  // it will repeat until the condition fall 



K&R text Book page 8

Variable and Arithmetic Operation 


#include

#include



 int main(){



int far ,celsius;

int lower ,upper,step;

  

  lower = 0;

  upper = 300;

  step =20;

   

   far = lower;

   

     while(far <= upper )

     {  

       celsius = 5 * (far - 32)/9;

       printf("%d\t%d\n", far ,celsius);

       far += step;

  }

 

 getch();

 return 0;

}



Uses Of %d in c Programming

 Many Of them getting confusion in %d.  lokk the various output at diffrent %d .ie . %2D ,%3d, %4d etc











Introduction to c programming

 Introduction to c programming 


First Program 

  Hello World Program

We need to compiler and an IDE  I am using Dev C++ IDE very good and simple one 


 It is a free and Open Source IDE .And  small in size download it from Here Click Here

first Program 

To print out the following words hello World