|
Home > Archive > Unix Shell > February 2007 > Adding printf lines in C style files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Adding printf lines in C style files
|
|
|
| My script adds a printf line after every statement (in C file)
for debugging purpose. There's a problem with if/else.
if (cond)
statement-1;
else
statement-2;
=> converts to
if (cond)
statement-1;
printf ("this\n");
else
statement-2;
printf ("other\n");
=> gives syntax error , since the two lines are not enclosed by {}.
I need, something like
if (cond)
{
statement-1;
printf ("this\n");
}
else
{
statement-2;
printf ("other\n");
}
If statements following if/else are already enclosed by {}, the script
is fine.
How do I do it?
TIA
James
| |
| Janis Papanagnou 2007-02-16, 1:17 pm |
| James wrote:
> My script adds a printf line after every statement (in C file)
> for debugging purpose.
Why not using a debugger in the first place?
> There's a problem with if/else.
>
> if (cond)
> statement-1;
> else
> statement-2;
>
> => converts to
>
> if (cond)
> statement-1;
> printf ("this\n");
> else
> statement-2;
> printf ("other\n");
>
> => gives syntax error , since the two lines are not enclosed by {}.
> I need, something like
>
> if (cond)
> {
> statement-1;
> printf ("this\n");
> }
> else
> {
> statement-2;
> printf ("other\n");
> }
>
> If statements following if/else are already enclosed by {}, the script
> is fine.
>
> How do I do it?
To keep that simple we have to assume at least that the source code
formatting conforms to some standard, otherwise you'd need a C parser.
Simply adding the braces is one approach; it shouldn't be a problem if
there's already a pair present. Another possibility might be to use the
sequencing operator (the comma) instead of the semicolon to separate the
printf from the peceeding statement; that way you don't need the braces.
statement-2,
printf ("other\n");
Janis
> TIA
>
> James
>
| |
| Tweedale 2007-02-16, 1:17 pm |
| On 16 Feb 2007 at 18:19, James wrote:
> My script adds a printf line after every statement (in C file) for
> debugging purpose. There's a problem with if/else.
>
> if (cond)
> statement-1;
> else
> statement-2;
> <snip>
> How do I do it?
Why don't you learn how to use a debugger instead?
--
email: echo t.adllkhsl@iypzavs.hj.br | tr a-gh-pq-z t-za-ij-s
| |
|
| On Feb 16, 10:35 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:[vbcol=seagreen]
> James wrote:
>
> Why not using a debugger in the first place?
>
>
>
>
>
>
>
>
>
>
>
> To keep that simple we have to assume at least that the source code
> formatting conforms to some standard, otherwise you'd need a C parser.
>
> Simply adding the braces is one approach; it shouldn't be a problem if
> there's already a pair present. Another possibility might be to use the
> sequencing operator (the comma) instead of the semicolon to separate the
> printf from the peceeding statement; that way you don't need the braces.
>
> statement-2,
> printf ("other\n");
>
> Janis
>
>
Actually it is a Java file. The comma does not seem to work for java.
James
| |
| Ed Morton 2007-02-16, 7:16 pm |
| James wrote:
> My script adds a printf line after every statement (in C file)
> for debugging purpose. There's a problem with if/else.
>
> if (cond)
> statement-1;
> else
> statement-2;
>
> => converts to
>
> if (cond)
> statement-1;
> printf ("this\n");
> else
> statement-2;
> printf ("other\n");
>
> => gives syntax error , since the two lines are not enclosed by {}.
> I need, something like
>
> if (cond)
> {
> statement-1;
> printf ("this\n");
> }
> else
> {
> statement-2;
> printf ("other\n");
> }
>
> If statements following if/else are already enclosed by {}, the script
> is fine.
>
> How do I do it?
> TIA
>
> James
>
Fix your source code. Not using the braces, while syntactically correct,
WILL produce a bug for you in future when you modify your code to try to
do add lines inside the "if" but forget to add the braces or you invoke
a macro that's written as:
#define foo() { ...; }
instead of:
#define foo() do { ...; } while (0/*CONSTANTCONDITION*/)
or ....
Regards,
Ed.
| |
| Janis Papanagnou 2007-02-16, 7:16 pm |
| James wrote:
> On Feb 16, 10:35 am, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
>
> Actually it is a Java file.
You've mentioned "C", twice, in the subject and in the text.
> The comma does not seem to work for java.
Then follow the other two approaches, suggested.
>
> James
>
|
|
|
|
|