Wednesday, August 11, 2010

functions in C ..!!

           A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. With properly designed functions, it is possible to ignore how a job is done; knowing what is done is sufficient. C makes the sue of functions easy, convinient and efficient; you will often see a short function defined and called only once, just because it clarifies some piece of code.



A function definition Syntax: 
return-type function-name(parameter declarations, if any)
{
   declarations
   statements
}

Example: 

/*
power.c: This program will give you brief idea about functions. 
Author: Afiz S 
Date: 11/08/10

*/
#include
int power(int a, int b); //prototype of the function. 
main()
{
int a,b;
printf("Enter your 2 number base and exponent\n");
scanf("%d%d",&a,&b);
printf("%dpower %d is = %d\n",a,b,power(a,b));  // calling function. 


}

int power(int a, int b) // function defination 
{
int i,base=a;
for(i=1;i
{
//printf("%d\n",a);
a=a*base;
}
return a;
}


Monday, August 9, 2010

Arrays .... in C

Arrays:
int a=10;
a=2;
printf("%d",a);  // if you try to print this you will get 2 as out put.

As we know variable can hold only one value at a time. but in few situations we need to store multiple in a single variable.

Arrays: is a variable that hold multiple elements which has same data type. or Array is a collects of variables of same type.

declaration of an array in C:

data_type array_name[size of the array];
ex: int a[5];
This statements tells that you want to store 5 integer values in array 'a'.

Monday, August 2, 2010

Character Input and Output

1. Write a program to print the value of EOF.


/*
This program is solution for 4 module problem set.

Author: Afiz'S
Date: 02/08/2010
*/
#include

main() // main function
{
char c=getchar();
printf("In the begining The value of ( c!=EOF )= %d \n",(c!=EOF));
while(c!=EOF)
c=getchar();
printf("At the ending The value of ( c!=EOF )= %d \n",(c!=EOF));

}


2.Write a program to read and accept 5 single characters from the keyboard

/*
Write a program to read and accept 5 single characters from the keyboard
Author: Afiz'S
Date: 02/08/2010
*/
#include

main() // main function
{
int i;
char c=getchar();
for(i=0;i<5;i++) // to accept 5 single characters { putchar(c); // through single character to output c=getchar(); } printf("\n"); } // end of the program.



3.Write a program to count characters of input.


/*
Write a program to count characters of input.
Author: Afiz'S
Date: 02/08/2010
*/
#include

main() // main function
{

char c = getchar();
int i=0;
while(c!=EOF)
{
i++;
//putchar(c);
//printf("\n");
c=getchar();
}

printf("Number of characters : %d\n",i);

}