0

I'ts more of a general question: how to handle the execution path of a c program, which consist of multiple functions? For example, i have a following c program:

 void test_if_statement(int num)
{
    if (num > 0)
        //do smth
        ;
    else
        //do smth else
        ;
}

int main(void)
{
    int num;
    printf("enter an int value:");
    scanf("%d", &num);
    test_if_statement(num);
    return 0;
}

Currently i'm using something like this to see where did my function go in if statements:

void test_if_statement(int num)
{
        if (num > 0)
            printf("i'm here\n");
            //do smth
        
        else
            printf("now i'm there\n");
            //do smth else
}

How can I keep it simple and more universal?;) Putting printf in every if-else pair seems unreasonably bulky...

hyono
  • 1
  • 1
    Have a look into using a debugger, which allows you to run through the program line by line and see the values of different variables. – 0RR Dec 29 '20 at 13:26

3 Answers3

0

There is nothing wrong using printf statements to track or log program flow for the purpose of learning, or testing during development. But a good debugger will probably serve the purpose just as well without having to add in-line code.
Depending on your environment there is a large list of debugger options available.

Following is an overview of debugging programs:

A debugger is a program that allows you to step through another program one line at a time. This is very useful when trying to identify incorrect code and analyze how a program "flows". Key concepts include: Breakpoints, Stepping, and Viewing data.

Key things that you can use the debugger to determine are:

  • The flow of the program (what happens next on a line by line basis)
  • The creation of variables
  • The data being stored in each variable
  • The entering/leaving of functions
  • The calculations that are made
  • The entering of IF statements or ELSE statements
  • The LOOPING of code.

Much more detail here

ryyker
  • 22,849
  • 3
  • 43
  • 87
0

This is usually a job for a debugger, most of which allow to step through the code one statement at a time. They also allow you to set breakpoints to pause execution at specific locations, watchpoints that pause execution when a variable changes, etc.

Without knowing your environment I can’t recommend a specific debugger to use. If you’re building code with gcc, then the companion debugger is (usually) gdb. You will need to use the -g flag when compiling to enable source-code level debugging. It’s not the friendliest debugger to use, but it’s pretty comprehensive. If you’re building in the Visual Studio environment on Windows, there should be a debugger as part of the IDE.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

If you are using an IDE for purpose of writing code then it would have built-in facilities for debuggers, the only thing you would need to do is set break-points and then when the program is run your code would pause whenever a break-point is hit. IDEs also provide facilities to look up variable values at that instant.

If you are not using an IDE and your question is specifically w.r.t C you could consider using GDB. Have a look here to get a better idea of how to use GDB to debug programs.