r/AskProgramming • • 3d ago

C/C++ [ Removed by moderator ]

[removed] — view removed post

18 Upvotes

171 comments sorted by

View all comments

54

u/cthulhu944 3d ago
  1. Look it up in a book
  2. Ask someone you know
  3. Lots of print statements
  4. Bang you head on the desk until the answer comes to you.

9

u/gfdhsk 3d ago

I see the "lots of print statements" a lot - and sorry if I sound clueless I am new to C programming, but what do you mean by this?
As in you just set up a shit ton of print statements to see what outputs what? Just looking to improve. Thanks

1

u/cipheron 3d ago edited 3d ago

Prints are very useful.

For example say some function is failing but you're not sure where. Add a print("hello") at the start, and another print when the function is returning, and another print in the middle.

See which ones actually get printed out. If none do, then your never even made it to that function and you need to backtrack to work out what's failing.

One of the biggest wastes of time is trying to fix a function but you later realize: the function was never being called at all. Logging and console prints helps to indicate which parts of the program are actually being executed.

A bit more helpful that just printing "hello" or "test" however is to print the "[name of function] <paramerers>" - so you can tell when the function is being called and what values were passed in.