1

Is there a way to pause the execution of a c loop? For example, something similar to the raw_input in python. Basically, I'd like to step through a while loop and check the variables as they change through each iteration of the loop:

while ((c=getchar()) != EOF) {


    // in_string
    if (c == '"' && !in_single_line_comment && !in_multi_line_comment && !has_preceding_backslash)
        in_string = !in_string;

    // has_preceding_backslash
    if (c == '\\' && !in_string && !in_single_line_comment && !in_multi_line_comment)
        has_preceding_backslash = !has_preceding_backslash;

    // this line here, to 'pause' the program until further user input.
    raw_input("Character: %c | InString: %d | HasSlash: %d", c, in_string, has_preceding_backslash)

}

Is there a way to do something like this?

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 1
    Use a debugger and single step. – Tanveer Badar Aug 24 '19 at 06:17
  • 2
    You don't need to 'pause' the program, since the `getchar` at the top of the loop will do that. Just change `raw_input` to `printf`, and put a `\n` at the end of the format string. – user3386109 Aug 24 '19 at 06:30
  • 1
    @user3386109 I think you might actually have better understood OPs actual question than the answers below (including mine admittedly). Maybe you can make an answer which will stand out. – Yunnosch Aug 24 '19 at 06:37
  • @Yunnosch I'm pretty certain this is a duplicate of many other questions. You could search for one, and close the question. – user3386109 Aug 24 '19 at 06:43
  • @user3386109 Good point. But if you do so, be fair and close with a duplicate which answers the question from OPs point of view, which you got better than I did. – Yunnosch Aug 24 '19 at 06:46
  • 1
    But the `raw_input` as specified reads in much more than the simple `getchar()`. Maybe loop is even wrong, possibly should rather be `for(;;)` or `while(1)`? – Aconcagua Aug 24 '19 at 07:08

3 Answers3

2

How to “step through” a loop

See the other ansers given so far ('use a debugger', 'GDB'); additionally: Any IDE you might be using (like eclipse, CodeBlocks, ...) provides a GUI interface for the debugger as well, which would make debugging even simpler...

If you don't find the appropriate button (in eclipse, it looks like, well, a bug (beetle) ...), hover over them to see the tool tips, one of these should reveal your button. Alternatively, you should find some menu entry.

Next step would be learning how to set a break point so that programme execution stops at exactly the desired point.

    // this line here, to 'pause' the program until further user input.
    raw_input("Character: %c | InString: %d | HasSlash: %d", c, s, h)

Now if you really want just to get user input (which the comment rather implies, in contradiction to the question title, though), you have several options, have a look at scanf*, getchar, fgets (if you want to read a complete line), strtok (tokenizing the string read before with fgets) and this answer (to parse the strings to integers).

*Careful, when reading strings into a buffer – always provide a length guard to prevent writing beyond your buffer's bounds! This guard must be one less than buffer size to leave space for the terminating null character.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
0

That is what the purpose of a debugger is.

Here is a useful link to set you on the right track.

How to debug using gdb?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
0

The best way to do this is a debugger, like GDB.

GDB is likely already installed on your linux distro of choice. It can be used by running gdb ./executable-name . Use break filename.c:line-number or break function_name, to set a breakpoint. Use run or start, to run the program in question. p variable_name to see the values of variables. Use step to execute the next line (and enter any function calls). Use next to execute the next line while not entering function calls. Make sure to compile your code with the -g. Additionally layout src can be nice to see your code as you use the debugger.

If you want to stop execution until the user enters a key you can use getchar() from stdio.h

PiRocks
  • 1,708
  • 2
  • 18
  • 29