Tuesday, 24 July 2012

Token pasting operator in c language

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));}

nesting of macros in c language with output

nesting of macros in c language with output
How to write code for nesting of macros in c language
#include
#define ISLOWER(c) (c>=97&&c<=122) #define ISUPPER(c) (c>=65&&c<=90)
#define ISALPHA(c) ISLOWER(c) || ISUPPER(c)
int main()
{
char chr;
printf("Enter a character:\n");
scanf("%c",&chr);


if (ISALPHA(chr) )

{

printf("%c is an alphabetical character.\n",chr);

}

else

{
printf("%c is not an alphabetical character.\n",chr);
}

return 0;
}

Array in cprogramming

Array in cprogramming
/*Array in c program with code*/

#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, in directing it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

Void Pointer example in c language

Void Pointer example in c language
/* How to use of void pointer in c language*/

#include

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.