0
  1 #include<stdio.h>
  2 #include<stdbool.h>
  3 
  4 # define MAX_WORD_LENGTH 150
  5 
  6 int main(int argc, char **argv) {
  7     short int wordCountList[MAX_WORD_LENGTH];
  8     const char TAB = '\t';
  9     const char NEW_LINE = '\n';
 10     const char SPACE = ' ';
 11     char c;
 12     bool wordStarted = false;
 13     bool wordEnded = false;
 14     short int wordCounted = 0;
 15     short int index = 0;
 16 
 17     printf("\nStart typing here ... ");
 18 
 19     for (index = 0; index < MAX_WORD_LENGTH; index++) {
 20         wordCountList[index] = 0;
 21     }
 22 
 23 
 24     while((c = getchar()) != EOF) {
 25         if ((c == TAB || c == NEW_LINE || c == SPACE) && !wordStarted) {
 26             continue;
 27         } else if ((c != TAB || c != NEW_LINE || c != SPACE) && !wordStarted) {
 28             wordStarted = true;
 29             wordCounted++;
 30         } else if ((c != TAB || c != NEW_LINE || c != ' ') && wordStarted) {
 31             wordCounted++;
 32         } else if ((c == TAB || c == NEW_LINE || c == SPACE) && wordStarted) {
 33             wordEnded = true;
 34             wordStarted = false;
 35             wordCountList[wordCounted]++;
 36             wordCounted = 0;
 37         }
 38     }
 39 
 40     if (wordStarted && !wordEnded) {
 41         wordEnded = true;
 42         wordCountList[wordCounted]++;
 43     }

Above is a c program on which I am trying to debug and it is not working as per expectation. At line number 30 how to check which of the OR condition was true, instead of printing the variable contents.

  • 1
    Please do not post code as images - [reasoning](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Please edit your question and paste in the code as formatted text. – kaylum Mar 01 '20 at 09:35
  • Related question: https://stackoverflow.com/q/58358817/72178. – ks1322 Mar 01 '20 at 09:43
  • 2
    Not related to the question but to the bug in your code that you want to debug: This part is always true: `(c != TAB || c != NEW_LINE || c != ' ')`. You might want to look up [de Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) on how to properly negate boolean expressions. – Gerhardh Mar 01 '20 at 09:45
  • 1
    Just print whatever condition you want to see. e.g. `p c!=TAB` – kaylum Mar 01 '20 at 09:47
  • This is what OP wants to avoid. – ks1322 Mar 01 '20 at 09:48
  • @Gerhardh can you please explain why this always be true – Sridhar Ramanathan Mar 01 '20 at 09:49
  • What value would `c` have in order to be `false` on all the 3 parts? If one comparison is `false`, all other comparisons must be `true`, giving `true` in total. – Gerhardh Mar 01 '20 at 09:50
  • @ks1322 What do you mean? That gdb command is not "printing the variable contents" which is what the OP wants to avoid. – kaylum Mar 01 '20 at 09:52
  • Example: `c != TAB` is `false` if `c == TAB` which means `c != NEW_LINE` is true as `TAB != NEW_LINE`. Thus `false || true || true` results in `true`. – Gerhardh Mar 01 '20 at 09:52
  • @ks1322 I would assume the OP wants to avoid printing the values in the program itself. Not in the debugger. – Gerhardh Mar 01 '20 at 09:53
  • @Gerhardh, I assumed that OP wants to avoid printing the values both in program and in gdb. If this is not so then I misunderstood the question. – ks1322 Mar 01 '20 at 09:59
  • @kaylum Thank you, it seems the only solution for now – Sridhar Ramanathan Mar 01 '20 at 09:59

0 Answers0