Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > beginner with programming, how to learn to debug and few C generalquestions

Reply
Thread Tools

beginner with programming, how to learn to debug and few C generalquestions

 
 
bpascal123@googlemail.com
Guest
Posts: n/a
 
      06-09-2009
Hi,

I am first an accountant and decided to take on programming a few
years ago...
I have been first studying simple algorithms...

Now I have decided to first learn C (i'll give it 600 effective hours -
so btw 1 and 3 years) before going to c++ or perl, or python, java or
php or ruby on rails or vba, i'll see at that time what answers most
my needs either as a professionnal accountant or as a personal project
i'd like to take on... Is there anything specific I could do with a
good knowledge of C only ?

So I have been having a hard time with the basics of pointers (20
hours). I really want to feel ok with this before getting any further.
I haven't looked at functions yet.

Here is a programm I know by heart - from a book - but in this code it
doesn't work althought it is very similar to other versions I have
been "coding" over and over more than 30 times ... and 10 % of the
time, there is a bug very similar to the one below I can solve by
looking carefully at the code or cannot solve if I have looked
carefully at the code. That's why I'd like to learn how to debug.

So far or so early with learning C, I'd like to use this opportunity
to learn how to debug and know about the tools that can help.

I have mainly been programming under Windows Xp DJGPP Dolorie... and I
have found a command : simify (link : http://www.delorie.com/djgpp/v2faq/faq9_3.html
) to help debug.

===O===

FIRST, here is the culprit : (this prg inserts a symbol every 3 digits
if the number includes 4 or more digits)

===O===

#include <stdio.h> /* printf, putchar, gets, puts */
#include <conio.h> /* getche */
#include <stdlib.h> /* atoi */
#include <ctype.h> /* isdigit */
#include <string.h> /* strcpy, strlen */

#define BOOL int

/**********/

main()
{
char buffer[128] ;
char clearbuf[128] ;
char output[172] ;

char * pb = buffer, * pc = clearbuf, * po = output ;

int symbol ;
int i, k ;
int cnt ;

BOOL ok ;

/**********/

printf("\033[2J");
printf("\nCe prog separe...\n");

/**********/

do
{
printf("\nSeparateur :");
symbol = getche() ;

if ( symbol < 32 )
printf("Pas de caractere de control comme separateur !");
if ( isdigit(symbol) )
printf("Pas de chiffre comme separateur !" ) ;
} while ( symbol < 32 || isdigit(symbol) ) ;

/**********/

do
{
ok = 1 ;
printf("\nEntrez un nombre entier positif ");
gets(buffer) ;

cnt = 0 ;

while ( *(buffer + cnt) && ok )
{
if ( !isdigit(*(buffer + cnt) ) )
{
printf("Pas d'entier positif!") ;
ok = 0 ;
}
cnt++;
}
} while ( !ok ) ;

/**********/

if ( cnt > 1 && atoi(buffer) )
{
while ( *pb == '0' )
pb++ ;

while ( *pc++ = *pb++ )
;

strcpy(buffer, clearbuf);
}

/**********/

if ( (cnt = strlen(buffer)) > 3)
{
for ( i = cnt - 1, k = 0, pb = buffer ; i >= 0 ; i--, k++ )
{
po[k] = pb[i] ;

if ( ( (cnt - i) % 3 ) == 0 && i != 0 )
po[++k] = symbol ;
}
po[k] = '\0' ;

for ( i = k - 1 ; i >= 0 ; i-- )
putchar(*(output + i));
}
else
puts(buffer) ;
}

===O===

Running this doesn't work after entering a symbol


NOW the simify cmd says (in an dmp output text file) :

===O===

Entrez un separateur : ;
Entrer un nbr (pas plus de 50 chiffres svp) 434334
Exiting due to signal SIGSEGV
General Protection Fault at eip=00001f9e
eax=00092ca5 ebx=0000029d ecx=0000000d edx=00000001 esi=00000054
edi=00012d74
ebp=00092d48 esp=00092b60 program=C:\DJGPP\ACT43B\PTRS36.EXE
cs: sel=01a7 base=029d0000 limit=0009ffff
ds: sel=01af base=029d0000 limit=0009ffff
es: sel=01af base=029d0000 limit=0009ffff
fs: sel=017f base=0000f860 limit=0000ffff
gs: sel=01bf base=00000000 limit=0010ffff
ss: sel=01af base=029d0000 limit=0009ffff
App stack: [00092d74..00012d74] Exceptn stack: [00012cd4..00010d94]

Call frame traceback EIPs:
0x00001f9e _main+243
0x00003a38 ___crt1_startup+176

C:\DJGPP\act43b>symify ptrs36.exe

C:\DJGPP\act43b>symify -o ptrs36.exe
Invalid option -o - type `symify' for help

C:\DJGPP\act43b>symify -o pbb1.dmp ptrs36.exe


====O====

I don't know if I can use what simify says to find any bug.

=========

Am I using simify right ?

Is my bug tracable ?

What(free) tool in my windows Xp context can I use to debug ?

=========

Thanks in advance ,

Pascal






 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      06-09-2009

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Here is a programm I know by heart - from a book - but in this code it
> doesn't work althought it is very similar to other versions I have


> main()


> symbol = getche() ;


> gets(buffer) ;


The book you got this from must be ancient, but at first try the code seems
to work (on my OS).

What's the actual problem?

And the register dump stuff is a waste of time at this level.


--
Bart

 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      06-09-2009

"BartC" <(E-Mail Removed)> wrote in message
news:qOBXl.40289$(E-Mail Removed) om...
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Here is a programm I know by heart - from a book - but in this code it
>> doesn't work althought it is very similar to other versions I have


> And the register dump stuff is a waste of time at this level.


Ok perhaps not such a waste as the error is mentioned there:

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Entrez un separateur : ;
> Entrer un nbr (pas plus de 50 chiffres svp) 434334
> Exiting due to signal SIGSEGV


I'll have a closer look.

--
Bart

 
Reply With Quote
 
bpascal123@googlemail.com
Guest
Posts: n/a
 
      06-09-2009
Sorry - this one below is not working and I don't understand why as it
looks identical to the one working... ===o===

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define BOOL int

/***********************************/

main()
{
char buffer[128] ;
char output[172] ;
char clearbuf[128] ;

char * pb = buffer, * pc = clearbuf, * po = output ;

int cnt, symbol ;
int i, k ;

BOOL ok ;

/***********************************/

printf("\033[2J");
printf("\nCe prg permet d'inserer...\n");

/***********************************/

do
{
printf("\nSeparateur :");
symbol = getche() ;

if ( symbol < 32 )
printf("Pas de caractere de control comme separateur !");
if ( isdigit(symbol) )
printf("Pas de chiffre comme separateur !" ) ;
} while ( symbol < 32 || isdigit(symbol) ) ;

/***********************************/

do
{
ok = 1 ;

printf("\nEntrer un nbr (pas plus de 50 chiffres svp) ") ;
gets(buffer) ;

cnt = 0 ;

while ( *(buffer + cnt) && ok )
{
if ( !(isdigit(buffer + cnt)) )
{
printf(" \nCe n'est pas un entier positif !") ;
ok = 0 ;
}
cnt++ ;
}
} while ( !ok ) ;

/***********************************/

if ( cnt > 1 && atoi(buffer) )
{
while ( *pb == '0' )
pb++ ;

while ( *pc++ == *pb++ )
;

strcpy(buffer, clearbuf) ;
}

/***********************************/

if ( (cnt = strlen(buffer)) > 3)
{
for ( i = cnt - 1, k = 0, pb = buffer ; i >= 0 ; i--, k++ )
{
po[k] = pb[i] ;

if ( ((cnt - i ) % 3) == '0' && i != 0 )
po[++k] = symbol ;
}
po[k] = '\0' ;

for ( i = k - 1 ; i >= 0 ; i-- )
putchar(*(output + i) ) ;
}
else
puts(buffer) ;
}

 
Reply With Quote
 
bpascal123@googlemail.com
Guest
Posts: n/a
 
      06-09-2009

Sorry - this one below is not working and I don't understand why as
it
looks identical to the one working... ===o===



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define BOOL int
/***********************************/
main()
{
char buffer[128] ;
char output[172] ;
char clearbuf[128] ;
char * pb = buffer, * pc = clearbuf, * po = output ;
int cnt, symbol ;
int i, k ;
BOOL ok ;
/***********************************/
printf("\033[2J");
printf("\nCe prg permet d'inserer...\n");
/***********************************/
do
{
printf("\nSeparateur :");
symbol = getche() ;
if ( symbol < 32 )
printf("Pas de caractere de control comme
separateur !");
if ( isdigit(symbol) )
printf("Pas de chiffre comme
separateur !" ) ;
} while ( symbol < 32 || isdigit
(symbol) ) ;
/***********************************/
do
{
ok = 1 ;
printf("\nEntrer un nbr (pas plus de 50 chiffres svp)
") ;
gets(buffer) ;
cnt = 0 ;
while ( *(buffer + cnt) && ok )
{
if ( !(isdigit(buffer + cnt)) )
{
printf(" \nCe n'est pas un entier
positif !") ;
ok = 0 ;
}
cnt++ ;
}
} while ( !ok ) ;
/***********************************/
if ( cnt > 1 && atoi(buffer) )
{
while ( *pb == '0' )
pb++ ;
while ( *pc++ == *pb++ )
;
strcpy(buffer, clearbuf) ;
}
/***********************************/
if ( (cnt = strlen(buffer)) > 3)
{
for ( i = cnt - 1, k = 0, pb = buffer ; i >=
0 ; i--, k++ )
{
po[k] = pb[i] ;
if ( ((cnt - i ) % 3) == '0' && i !=
0 )
po[++k] = symbol ;
}
po[k] = '\0' ;
for ( i = k - 1 ; i >= 0 ; i-- )
putchar(*(output + i) ) ;
}
else
puts(buffer) ;

}

 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      06-09-2009

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Sorry - this one below is not working and I don't understand why as
> it
> looks identical to the one working... ===o===


I'm confused now, which one was supposed to be working, the first one you
posted? With the error dump?

I would stick with that one since that one worked for me and the others
(which have a couple of blatant errors) don't.

No idea how you would use your simify program to pinpoint the error.

If you get segfault errors on your machine, in this simple program I would
just insert exit(0) at specific points to see near line it occurs at (I'd
also hardcode the values of symbol and buffer to avoid having to reenter
these).

Start just after gets(), and if it's OK then just move the exit(0) a bit
further down (gets() is not a good idea to use but you can worry about
replacing it later).

--
Bart

 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      06-09-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi,
>
> I am first an accountant and decided to take on programming a few
> years ago...
> I have been first studying simple algorithms...
>
> Now I have decided to first learn C (i'll give it 600 effective hours
> - so btw 1 and 3 years) before going to c++ or perl, or python, java
> or php or ruby on rails or vba, i'll see at that time what answers
> most my needs either as a professionnal accountant or as a personal
> project
> i'd like to take on... Is there anything specific I could do with a
> good knowledge of C only ?
>
> So I have been having a hard time with the basics of pointers (20
> hours). I really want to feel ok with this before getting any further.
> I haven't looked at functions yet.
>
> Here is a programm I know by heart - from a book - but in this code it
> doesn't work althought it is very similar to other versions I have
> been "coding" over and over more than 30 times ... and 10 % of the
> time, there is a bug very similar to the one below I can solve by
> looking carefully at the code or cannot solve if I have looked
> carefully at the code. That's why I'd like to learn how to debug.
>
> So far or so early with learning C, I'd like to use this opportunity
> to learn how to debug and know about the tools that can help.
>
> I have mainly been programming under Windows Xp DJGPP Dolorie... and I
> have found a command : simify (link :
> http://www.delorie.com/djgpp/v2faq/faq9_3.html ) to help debug.
>
> ===O===
>
> FIRST, here is the culprit : (this prg inserts a symbol every 3 digits
> if the number includes 4 or more digits)
>
> ===O===
>
> #include <stdio.h> /* printf, putchar, gets, puts */
> #include <conio.h> /* getche */


<conio.h> is part of your Windows library, not of C. Therefore, your
code is not only outside the scope of this group, but more importantly,
many regulars here will be unable to compile your code. (I for one have
no <conio.h> header.)

> #include <stdlib.h> /* atoi */
> #include <ctype.h> /* isdigit */
> #include <string.h> /* strcpy, strlen */
>
> #define BOOL int
>
> /**********/
>
> main()


This should be
int main(void)

Which is clearer and works with the latest standardized version of C.

> {
> char buffer[128] ;
> char clearbuf[128] ;
> char output[172] ;
>
> char * pb = buffer, * pc = clearbuf, * po = output ;
>
> int symbol ;
> int i, k ;
> int cnt ;
>
> BOOL ok ;
>
> /**********/
>
> printf("\033[2J");
> printf("\nCe prog separe...\n");
>


You can use puts() instead of printf(), which saves a fair bit of
overhead and automatically adds the \n to the end of every line.

> /**********/
>
> do
> {
> printf("\nSeparateur :");
> symbol = getche() ;


What does this function do? If you need to get a single character, use
getc() or fgetc().

>
> if ( symbol < 32 )
> printf("Pas de caractere de control comme separateur !");
> if ( isdigit(symbol) )
> printf("Pas de chiffre comme separateur !" ) ;
> } while ( symbol < 32 || isdigit(symbol) ) ;
>
> /**********/
>


Please use proper indentation when posting on Usenet.

> do
> {
> ok = 1 ;
> printf("\nEntrez un nombre entier positif ");
> gets(buffer) ;


Stop right here. NEVER ever EVER use gets(). It is impossible to use
safely, since it does no buffer size checking and therefore is inviting
users to overrun your buffer and run over who-knows-what memory.

Use fgets() instead, which will accept sizeof buffer as an additional
argument, and will check the size of the input.

>
> cnt = 0 ;
>
> while ( *(buffer + cnt) && ok )
> {
> if ( !isdigit(*(buffer + cnt) ) )
> {
> printf("Pas d'entier positif!") ;
> ok = 0 ;
> }
> cnt++;
> }
> } while ( !ok ) ;
>
> /**********/
>
> if ( cnt > 1 && atoi(buffer) )


atoi() is also not recommended since it doesn't fail well if you pass it
invalid input. Use strtol() or strtoul() instead.

> {
> while ( *pb == '0' )
> pb++ ;
>
> while ( *pc++ = *pb++ )
> ;
>
> strcpy(buffer, clearbuf);
> }
>
> /**********/
>
> if ( (cnt = strlen(buffer)) > 3)
> {
> for ( i = cnt - 1, k = 0, pb = buffer ; i >= 0 ; i--, k++ )


Your spacing is abysmal. Please correct it.

> {
> po[k] = pb[i] ;
>
> if ( ( (cnt - i) % 3 ) == 0 && i != 0 )
> po[++k] = symbol ;
> }
> po[k] = '\0' ;
>
> for ( i = k - 1 ; i >= 0 ; i-- )
> putchar(*(output + i));
> }
> else
> puts(buffer) ;
> }
>


If you fix all of the above problems, it might fix whatever the issue
with your code is. But as it stands, I can neither compile nor read the
posted code. So please correct these things, fix the formatting and
repost if you are still having issues.

--
Andrew Poelstra <http://testing.wpsoftware.net/finex>


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-09-2009

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

[snip]

> if ( cnt > 1 && atoi(buffer) )


....

Many prefer strtol to atoi. atoi returns nothing and strtol can be error
checked. Very good you sure learn quickly. Your better than me and I've been
studying on and off for several years now. But I've never made a point of
pointers til now

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-09-2009

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Sorry - this one below is not working and I don't understand why as
> it
> looks identical to the one working... ===o===
>
>
>
> #include <stdio.h>
> #include <conio.h>
> #include <stdlib.h>
> #include <ctype.h>
> #include <string.h>
> #define BOOL int
> /***********************************/
> main()
> {
> char buffer[128] ;
> char output[172] ;
> char clearbuf[128] ;
> char * pb = buffer, * pc = clearbuf, * po = output ;
> int cnt, symbol ;
> int i, k ;
> BOOL ok ;
> /***********************************/
> printf("\033[2J");
> printf("\nCe prg permet d'inserer...\n");
> /***********************************/
> do
> {
> printf("\nSeparateur :");
> symbol = getche() ;
> if ( symbol < 32 )
> printf("Pas de caractere de control comme
> separateur !");
> if ( isdigit(symbol) )
> printf("Pas de chiffre comme
> separateur !" ) ;
> } while ( symbol < 32 || isdigit
> (symbol) ) ;
> /***********************************/
> do
> {
> ok = 1 ;
> printf("\nEntrer un nbr (pas plus de 50 chiffres svp)
> ") ;
> gets(buffer) ;
> cnt = 0 ;
> while ( *(buffer + cnt) && ok )
> {
> if ( !(isdigit(buffer + cnt)) )
> {
> printf(" \nCe n'est pas un entier
> positif !") ;
> ok = 0 ;
> }
> cnt++ ;
> }
> } while ( !ok ) ;
> /***********************************/
> if ( cnt > 1 && atoi(buffer) )
> {
> while ( *pb == '0' )
> pb++ ;
> while ( *pc++ == *pb++ )
> ;
> strcpy(buffer, clearbuf) ;
> }
> /***********************************/
> if ( (cnt = strlen(buffer)) > 3)
> {
> for ( i = cnt - 1, k = 0, pb = buffer ; i >=
> 0 ; i--, k++ )
> {
> po[k] = pb[i] ;
> if ( ((cnt - i ) % 3) == '0' && i !=
> 0 )
> po[++k] = symbol ;
> }
> po[k] = '\0' ;
> for ( i = k - 1 ; i >= 0 ; i-- )
> putchar(*(output + i) ) ;
> }
> else
> puts(buffer) ;
>
> }


Myself personally I would've put exit(EXIT_SUCCESS); for portability
rather than exit(0);

Bill


 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      06-09-2009
Bill Cunningham wrote:
>
> Myself personally I would've put exit(EXIT_SUCCESS); for
> portability
> rather than exit(0);
>


0 and EXIT_SUCCESS are both portable across all C implementations.

--
Andrew Poelstra <http://testing.wpsoftware.net/finex>


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Some information for the one who decided to learn C++, and now wantsto learn at least a bit of C? Alexander C Programming 20 09-11-2010 01:04 AM
Internet connects for a few seconds then disconnects for a few sec Cody Wireless Networking 2 06-28-2009 08:24 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
newbie question: should I learn TKinter or skip it and learn more advanced toolkit? Porky Pig Jr Python 3 05-12-2004 08:58 AM



Advertisments