Wednesday, April 14, 2010

C Programming FAQs Part1

# include

/*************************************************************************/
#define int char
main()
{
int i=65;
printf("sizeof(i)=%d \n",sizeof(i));
}
/*Answer:
sizeof(i)=1
Explanation:
Since the #define replaces the string int by the macro char
*/
/*************************************************************************/

void main()
{
int const * p=5;
printf("%d",++(*p));
}

/*************************************************************************/

main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("%c%c%c%c \n",s[ i ],*(s+i),*(i+s),i[s]);
}
/*
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the
same
idea. Generally array name is the base address for that array. Here s
is
the base address. 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].*/
/*************************************************************************/
main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
/*
Explanation:
When static storage class is given, it is initialized once. The change
in
the value of a static variable is retained even between the function
calls. Main is also treated like any other ordinary function, which can
be
called recursively.*/

main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(" %d ",*c);
++q; }
for(j=0;j<5;j++){
printf(" %d ",*p);
++p; }
}
/*
Answer:
2 2 2 2 2 2 3 4 6 5
Explanation:
Initially pointer c is assigned to both p and q. In the first loop,
since
only q is incremented and not c , the value 2 will be printed 5 times.
In
second loop p itself is incremented. So the values 2 3 4 6 5 will be
printed.
*/
/*************************************************************************/
main()
{
extern int i;
i=20;
printf("%d",i);
}
/*
Answer:
Linker Error : Undefined symbol '_i'
Explanation:
extern storage class in the following declaration,
extern int i;
specifies to the compiler that the memory for i is allocated in some
other
program and that address will be given to the current program at the
time
of linking. But linker finds that no other variable of name i is
available
in any other program with memory space allocated for it. Hence a linker
error has occurred .

*/
/*************************************************************************/
main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}

/*Explanation :
Logical operations always give a result of 1 or 0 . And also the
logical
AND (&&) operator has higher priority over the logical OR (||)
operator.
So the expression �i++ && j++ && k++� is executed first. The result of
this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 ||
2
which evaluates to 1 (because OR operator always gives 1 except for �0
||
0� combination- for which it gives 0). So the value of m is 1. The
values
of other variables are also incremented by 1.
*/
/*************************************************************************/
main()
{
char *p;
printf("%d %d \n",sizeof(*p),sizeof(p));
}
/*
Answer:
1 2
Explanation:
The sizeof() operator gives the number of bytes taken by its operand. P
is
a character pointer, which needs one byte for storing its value (a
character). Hence sizeof(*p) gives a value of 1. Since it needs two
bytes
to store the address of the character pointer sizeof(p) gives 2.
*/
/*************************************************************************/
main()
{
int i=10;
i=!i>14;
printf("i=%d \n",i);
}
/*
Answer:
i=0


Explanation:
In the expression !i>14 , NOT (!) operator has more precedence than>
symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is
false). 0>14 is false (zero).
*/

/* http://www.c.happycodings.com/Beginners_Lab_Assignments/code24.html */
/*************************************************************************/


Thanks to :

No comments:

Post a Comment