LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2017, 05:39 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Commenting blocks in C in order to debug.


The compiler finds errors. I do not find any plausible cause. Then I can comment almost all code in, say main, leaving for instance only one instruction. If now the code compiles, I know the fault is within the commented block. I can comment out half the block and find which half is guilty.

Alright. But how do I comment a block that contains /* */ comments? I Pascal there were two kinds of block comments. One was { }, the other I do not remember. Any other way telling the compiler to ignore a block of code?

EDIT:
I search for some #ifdef construct and found (Google) the following much used by developers:
Code:
#ifdef 0
[code here]
#endif

Last edited by stf92; 11-11-2017 at 05:46 PM.
 
Old 11-11-2017, 06:32 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by stf92 View Post
I search for some #ifdef construct and found (Google) the following much used by developers:
Code:
#ifdef 0
[code here]
#endif
Should be "#if 0", or "#ifdef NOT_DEFINED".
 
1 members found this post helpful.
Old 11-13-2017, 04:36 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks. The compiler objected 'ifdef 0' I had to use some not defined constant as CERO. 'if 0' seems the logical option.
 
Old 11-15-2017, 08:57 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
As irritating as this sort of problem may be, it"s usually the cause of a missing closing-quote or something equally stupid. (Just like the one that I purposely put into this response.) It's really hard to spot these things, especially if the program's rambling-on for quite some distance after the actual point where the typo's actually located . . .

C 'lint' packages are sometimes better than actual compilers are, in catching these sorts of problems, basically because they are built to be looking for them.

I generally don't try to use this technique because the odds are too great that omitting some block of code will simply generate other syntax-errors unrelated to the one that I am actually trying to find.

Last edited by sundialsvcs; 11-15-2017 at 09:00 AM.
 
Old 11-15-2017, 05:21 PM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I see but for some reason Pascal had two kinds of comments. When you had a lot of comment blocks of one type, you could always nest them inside a comment block of the other type.
 
Old 11-15-2017, 07:25 PM   #6
Mill J
Senior Member
 
Registered: Feb 2017
Location: @127.0.0.1
Distribution: Mint, Void, MX, Haiku, PMOS, Plasma Mobile, and many others
Posts: 1,258
Blog Entries: 2

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
So how did you solve this?

In c++ you can use // at the start of a line to comment it out, not sure if you can use that in c.

Your best bet is to figure out the compiler's errors. Often/Sometimes the error is not on the line that it starts yelling at so that does make it harder.
 
Old 11-16-2017, 07:12 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939Reputation: 3939
Quote:
Originally Posted by Mill J View Post
Your best bet is to figure out the compiler's errors. Often/Sometimes the error is not on the line that it starts yelling at so that does make it harder.
In my experience it is almost always a stupid quote-mark, and I simply tell the editor to search backwards from the point where the error was detected.

I've also been known to drop syntax-errors into the source at earlier points to see if that syntax-error goes off first.

But in general, with practice you begin to see "where the parser probably fell off the bus." If the grammar used in your compiler is not very thorough, "lint" tools often use much better ones. And they can give better messages, like "Unexpected end-of-file while processing a string literal which began at line 1234, column 5." Wish all compiler-writers thought to do that ...

Some programmer's-editors also support parsing of the source language and can give you feedback cues in real time, e.g. through the use of color or through various lint-like tools that can be run within the editor itself.

Last edited by sundialsvcs; 11-16-2017 at 07:15 AM.
 
1 members found this post helpful.
Old 11-16-2017, 02:17 PM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by Mill J View Post
So how did you solve this?

In c++ you can use // at the start of a line to comment it out, not sure if you can use that in c.

Your best bet is to figure out the compiler's errors. Often/Sometimes the error is not on the line that it starts yelling at so that does make it harder.
Well I commented out the whole program first and then I left out of the comment block only the first sentence. Then two sentences and so on until the error appeared. In a few steps the error was put in evidence. I was lucky. It was an unmatched '{' or a ';', I don't remember.

This is why I always use // comments in C (they exist in C as well as in C++). You can nest them into /* comment blocks.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Logi Sales Manager on Ncurses (invoice, invoicing, orders, order, sale order, sales order...)? Xeratul Linux - Software 0 03-25-2017 02:45 PM
gdb, debug with separate debug file and different source dir problem a4z Programming 5 10-17-2014 01:55 AM
Changing the order of rows in multiple blocks in a config file. afroresurrection Linux - Server 3 04-05-2012 07:23 AM
LXer: Ninja Blocks available for pre-order, Kickstarter orders shipping now LXer Syndicated Linux News 0 03-30-2012 11:50 AM
How to force bad blocks into CF in order to decrease the capacity asca Linux - Embedded & Single-board computer 2 09-01-2010 02:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration