Sunday, 19 August 2012

c program example with token pasting operator

c program example with token pasting operator
 Token pasting operator in c language

/* use of token pasting operator with Output */

//Token pasting operator is used to concatenate two tokens into a single token.


#include


#define PASTE(a,b) a##b

#define MARKS(sub) marks_##sub


int main()

{

int k1=15,k2=60;


int marks_os= 95, marks_ds=99;


//converting statement below to printf("%d %d",k1,k2)

printf("%d %d ",PASTE(k,1),PASTE(k,2));


//converting statement below to printf("%d %d",marks_os,marks_ds)

printf("%d %d ",MARKS(os),MARKS(ds));

}

c program examples with array | array in c code

c program examples with array | array in c code
*Array in c program with code output*/

#include

main()

{

char s[ ]="man";

int i;

for(i=0;s[ i ];i++)

printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);

}

Explanation:
As we know array name is the base address for that array. Here s is the base address and i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

c program code | void pointer | example of void pointer

c program code | void pointer | example of void pointer
 Void Pointer example in c language

/* How to use of void pointer in c language*/
main()

{

int a=10,*j;

void *k;

j=k=&a;

j++;

k++;

printf("\n %u %u ",j,k);

}

Explanation:

Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.

c program code for draw pyramid

c program code for draw pyramid
How to Draw Pyramid pattern in c language

/* To print Pyramid Pattern shown below

Pyramid Pattern

*/

int main()
{
int i,j,n;
printf("Enter no. of lines: ");
scanf( "%d",&n);
//for loop for upper pyramid
//for loop for number of lines
for(i=1;i<=n;i++)
{
//for loop for spaces
for(j=1;j<=n-i;j++)
{
printf(" ");
}
//for loop for stars in first half
for(j=1;j<=i;j++)
{
printf("* ");
}
//for loop for stars in second half


//for loop for lower pyramid

//for loop for number of lines

for(i=1;i<=n-1;i++)
{
//for loop for spaces
for(j=1;j<=i;j++)
{
printf(" ");
}
//for loop for stars in first half
for(j=1;j<=n-i;j++)
{
printf("* ");
}
//for loop for stars in second half


return 0;

}