RSS
people

Increment Operators

Increment Operators

is the C statement

 printf("%d, %d, %dn", i,i++,++i)

a valid statement?
Think hard before arriving to any conclusions….

Well its not!!

firstly ehat do u expect the evalution order of functions a(), b() and c() in the following statement

printf("%d,%d,%d",a(),b(),c());

well by the C standards the behaviour is *unspecified* i.e. the arguments of a function may be evaluated in any order that may also be interleaved…. now this unspecified behaviour of evalution of the arguments may lead to undefined behaviour and hence the ambiguous result.

Secondly there is no sequence point between the different expressions(i, ++i, i++) and according to C standards modifying the value of an…

Continue reading about Increment Operators

No Comments |

C++ Preprocessor: What are ‘__FILE__’ and ‘__LINE__’?

Q: What are ‘__FILE__’ and ‘__LINE__’?

A: ‘__FILE__’ and ‘__LINE__’ are predefined macros and part of the C/C++ standard. During preprocessing, they are replaced respectively by a constant string holding the current file name and by a integer representing the current line number.

There are other preprocessor variables including:

'__DATE__' -> a string literal of the form "Mmm dd yyyy"
'__TIME__' -> a string literal of the form "hh:mm:ss">
'__TIMESTAMP__' -> a string literal of the form "Mmm dd yyyy hh:mm:ss"
'__FUNCTION__' -> a string literal which contains the function name (this is part of C99, the new C standard and not all C++ compilers support it)

Continue reading about C++ Preprocessor: What are ‘__FILE__’ and ‘__LINE__’?

No Comments |

gets ()

gets()

Well we all know that whenever we compile a file containing a refference to gets() function, the compiler gives a warning that using gets() might be dangerous…..

The reason is very simple… gets simply copies the input string on to the program stack without inpecting its size… for example the code…

void enterstring()
{
char string[FIXEDLENTH];gets(string);
}

now when the function is called then the space for the local variable string gets allocated in the program stack. If the length of the input is now greater than the FIXEDSIZE then the stack gets over written… but how this is dangerous???

Well one can very…

Continue reading about gets ()

No Comments |

Swapping of two integers

swapping of two integers

(!) Using temporary variable

main()
{
   int a=10,b=20,c;
   c=a;
   a=b;
   b=c;
}

(!!) Without using temporary variables

method 1: ( by changing int to float u can use this to swap floating point values )

main()
{
   int a=10,b=20;
   a=a+b;
   b=a-b;
   a=a-b;
}

method 2: ( works only for integes )

main()
{
   int a=20,b=10;
   a=a^b;
   b=a^b;
   a=a^b;
}
main()
{
   int a=20,b=10;
   a=a*b;
   b=a/b;
   a=a/b;
}

Continue reading about Swapping of two integers

1 Comment |