简体   繁体   English

如何“循序渐进”循环

[英]How to “step through” a loop

Is there a way to pause the execution of ac loop? 有没有办法暂停交流回路的执行? For example, something similar to the raw_input in python. 例如,类似于python中的raw_input Basically, I'd like to step through a while loop and check the variables as they change through each iteration of the loop: 基本上,我想遍历while循环并检查变量在循环的每次迭代中的变化情况:

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? 有没有办法做这样的事情?

How to “step through” a loop 如何“循序渐进”循环

See the other ansers given so far ( 'use a debugger' , 'GDB' ); 查看到目前为止给出的其他答案( “使用调试器”“ 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... 另外:您可能正在使用的任何IDE(例如eclipse,CodeBlocks等)都为调试器提供了GUI界面,这将使调试更加简单...

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. 如果您找不到合适的按钮(在月食中,它看起来像是个bug(甲虫)...),请将鼠标悬停在它们上方以查看工具提示,其中之一应该可以显示您的按钮。 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. //此行,以“暂停”程序,直到进一步的用户输入为止。\n    raw_input("Character: %c | InString: %d | HasSlash: %d", c, s, h) raw_input(“字符:%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). 现在,如果您真的只想获取用户输入(虽然与问题标题相反,但注释却暗示了这一点),则有几个选项,请看一下scanf *getcharfgets (如果您想阅读完整的内容,行), strtok (使用fgets标记之前读取的字符串)和此答案 (将字符串解析为整数)。

* 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. 该保护符必须小于缓冲区大小一个,以留出空间来终止空字符。

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? 如何使用gdb进行调试?

The best way to do this is a debugger, like GDB. 最好的方法是使用调试器,例如GDB。

GDB is likely already installed on your linux distro of choice. GDB可能已经安装在您选择的Linux发行版中。 It can be used by running gdb ./executable-name . 可以通过运行gdb ./executable-name来使用它。 Use break filename.c:line-number or break function_name , to set a breakpoint. 使用break filename.c:line-numberbreak function_name设置断点。 Use run or start , to run the program in question. 使用runstart来运行有问题的程序。 p variable_name to see the values of variables. p variable_name查看p variable_name的值。 Use step to execute the next line (and enter any function calls). 使用step执行下一行(并输入任何函数调用)。 Use next to execute the next line while not entering function calls. 在不输入函数调用的情况next ,使用next执行下一行。 Make sure to compile your code with the -g . 确保使用-g编译代码。 Additionally layout src can be nice to see your code as you use the debugger. 另外,在使用调试器时, layout src可能很高兴看到您的代码。

If you want to stop execution until the user enters a key you can use getchar() from stdio.h 如果要在用户输入密钥之前停止执行,可以使用stdio.h getchar()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM