Thursday, May 17, 2007

My Passion for C

Posting some useful information regarding C language & C FAQs
----------------------------------------------------------------------
Developing C programs from Visual Studio.NET
File -> New -> Project ->
Select Visual C++ Project Type -> Win32 -> Win32 Console Application.

----------------------------------------------------------------------

Function Pointer Example:
#include "stdafx.h"
#include "conio.h"
int Add(int a,int b)
{
return a+b;
}

void Dummy()
{
printf("\nI am in a dummy function now");
}

int _tmain(int argc, _TCHAR* argv[])
{
int (*fp)(int,int);
void (*voidfp)();
fp = Add;
printf("%d",fp(2,3));
voidfp = Dummy;
voidfp();
getch();
return 0;
}

------------------------------------------------------
How to find the size of a structure in C
Ans: Size of a structure is the sum of the sizes of individual elements in it.
------------------------------------------------------

Swap 2 variables in one line: a ^= b ^= a ^= b;
but the solution is not portable

------------------------------------------------------
What's a "sequence point"?
A point (at the end of a full expression, or at the , &&, ?:,
or comma operators, or just before a function call) at which all
side effects are guaranteed to be complete.
------------------------------------------------------

Is this correct?
char *p; *p = malloc(10);

Ans: No. p is the pointer, not *p.
------------------------------------------------------

*p++ means what?
it increments p
to increment the value pointed to by p use (*p)++;
------------------------------------------------------

Why doesn't the code "((int *)p)++;" work?

In C, a cast operator is a conversion operator, and by definition it yields an rvalue, which cannot be assigned to, or incremented with ++.
------------------------------------------------------

what is a null pointer?
(null pointer is a language concept)
this is not the address of any object or function. constant zero is assumed as null pointer at compile time (in pointers context)

------------------------------------------------------

what is NULL ?
NULL is a pre-processor macro (its value is zero) which is used to generate null pointers.
null is #define 0 (or)
((void *)0)

------------------------------------------------------
What do "lvalue" and "rvalue" mean?
An "lvalue" denotes an object that has a location; an "rvalue" is any expression that has a value.

------------------------------------------------------

What is assert()?
It is a macro which documents an assumption being made by the
programmer; it terminates the program if the assumption is
violated.

------------------------------------------------------
What does a+++++b mean ?

It's interpreted as "a ++ ++ + b", and cannot be parsed.
------------------------------------------------------
How can I swap two values without using a temporary?
a = a + b;
b = a - b;
a = a - b;

or
a ^= b;
b ^= a;
a ^= b;

------------------------------------------------------
How can I call a function, given its name as a string?
maintain a correspondence table of names and function pointers.

------------------------------------------------------
How can we access command-line arguments?
via argv[] parameter of main function

------------------------------------------------------

How can I invoke another program from within a C program?
'System' will help you.

------------------------------------------------------
How can %f be used for both float and double arguments in printf()?

ans: In variable-length argument lists, types char and short int are promoted to int, and float is promoted to double

------------------------------------------------------
How can I print a '%' character with printf?

"%%"
------------------------------------------------------

No comments: