Explore BrainMass

Macro processing in C/C++

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

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

© BrainMass Inc. brainmass.com October 25, 2018, 6:36 am ad1c9bdddf

Solution Preview

Given code fragment will produce unexpected results in case of

i = max(a++, b++);


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


j = ((c++) > (d++) ? (c++) : (d++));

Before these code fragments execute,
(a,b) : (3,4)
(c,d) : ...

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.

See Also This Related BrainMass Solution

Caesar Cipher Programming

See the attached file.
I am looking to get some help on an upcoming task that includes creating a Caesar cipher, a character frequency generator, and a combination of the two in order to use the frequency generator to decrypt a message using the highest frequency value as the key. The problem must be coded in c, c++, or python as I will be creating test harnesses/scripts within Linux.

Implement the Caesar cipher and develop tools that may be used to attack this cipher using a ciphertext only attack.

1) Cipher Implementation - caesarCipher

Implement an extended version of the Caesar cipher that uses the characters from ' ' (Space, ASCII 32) to '~' (tilde, ASCII 126). Any character (printable or not) that falls outside of the ASCII range of 32-126 should be left unchanged during both encryption and decryption (this will ensure that printable files will remain printable). Key values range from 0 to 94. Example with key=1:

Plaintext: Hello {~World~}!
Ciphertext: Ifmmp!| Xpsme ~"
Decrypted text: Hello {~World~}!

Command-line arguments should be used to indicate an input file (-i input_file), an output file (-o output_file), and an integer key value (-k key_value). An optional parameter (-d), when present, indicates that the input file is a ciphertext file that is to be decrypted.

Input files of up to 100 Kb in size should be accommodated.

2) Character Frequency Generator - charFreqGen

Design and implement a program that will take as input a text file and output a list of character frequency pairs, given the text within the file. The frequencies generated shall be case sensitive. This program may ignore all characters outside of the ASCII 32-126 range.

Command-line arguments should be used to indicate the file (-i input_file) from which the character frequencies are to be extracted as well as the output file (-o output_file) to which these frequencies are to be stored. The top character frequencies shall be displayed to the user in a sorted, two-column format. Characters shall be displayed in the first column, followed by their frequency in the second column. By default, the highest five character-frequency pairs shall be displayed, however this value may be overridden with a command-line parameter (-t top_count). Input file sizes of up to 100 Kb should be accommodated.

3) Ciphertext Attack Tool - caesarAttack

Design and implement a program that uses character frequency statistics generated by charFreqGen program to compute the correlation value, ø(i), for all possible key values. Once the ø(i) have been computed and sorted, decrypt the message using the key associated with the highest ø(i) value.

Note: If the a priori character frequencies are not representative of the plaintext message's character frequencies, the highest-ranked key may not decrypt the ciphertext. In order to allow for this, a command-line parameter may be used to select a lower-ranked key.

Command-line arguments should be used to indicate an input ciphertext file (-c ciphertext_file) and an input character frequency file (-f frequency_file). The top 5 ø(i) values should be displayed to the screen in sorted order, highest ø values first, with the key values listed in.

View Full Posting Details