Macro processing in C/C++
Not what you're looking for?
The following code fragment produces the correct result sometimes and sometimes it does not. Explain what is happening and why.
#include <stdio.h>
#define max(a,b) ((a) > (b) ? (a): (b))
void main(void)
{
int i = 0;
int j = 0;
int a = 3;
int b = 4;
int c = 6;
int d = 5;
i = max(a,b);
j = max(c,d);
printf("i = %i, j = %in",i,j);
i = max(a++, b++);
printf("a = %i, b = %in",a,b);
j = max(c++,d++);
printf("c = %i, d=%in",c,d);
return;
}
Purchase this Solution
Solution Summary
Response first points out the code fragments that will produce unexpected results, and then goes on explaining in complete detail (and step-by-step manner), why it is so.
Solution Preview
Given code fragment will produce unexpected results in case of
i = max(a++, b++);
and
j = max(c++,d++);
Things would have worked as expected if max(...) were a function call. However it is a macro call, and thus given code fragments become following after preprocessing step during compilation.
i = ((a++) > (b++) ? (a++) : (b++));
and
j = ((c++) > (d++) ? (c++) : (d++));
Before these code fragments execute,
(a,b) : (3,4)
(c,d) : ...
Purchase this Solution
Free BrainMass Quizzes
Inserting and deleting in a linked list
This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.
Excel Introductory Quiz
This quiz tests your knowledge of basics of MS-Excel.
C# variables and classes
This quiz contains questions about C# classes and variables.
Basic UNIX commands
Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.
Basic Networking Questions
This quiz consists of some basic networking questions.