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

L

Lew Pitcher

Prolog (it has relations) and more generally rule-based languages.
(Rules are not functions.) Early BASICs. Assembler. Does COBOL have
functions?

Yes.

COBOL provides the language facilities for a programmer to subdivide his
program into functional units, at three different levels (two "private"
levels, and one "public" level).

COBOL provides mechanisms to permit multiple "public" entrypoints ("PROGRAM
NAME"s and their associated "PROCEDURE DIVISION" code) within a single
compilation unit.

Within the code for a single "public" entrypoint, COBOL provides a named
division of code to group and identify high-level functional units
("SECTION"s), and within each of these divisions, COBOL provides a named
division of code to group and identify low-level functional units
("PARAGRAPH"s). Both SECTION and PARAGRAPH identifiers are "private", and
not exposed outside of the code for the "public" entrypoint"

With the advent of linkage-editors in the mid-1900's (circa 1950), COBOL
(and other high-level-languages) gained the ability to further subdivide
programming activities into functional components, by permitting access of
one public-named functional unit from a functional unit in a separate
compilation unit.
Well, maybe. (I don't classify it as a /proper/ programming language
unless it as /first-class/ functions.)

I guess that there are many programming languages that don't satisfy your
definition for a /proper/ programming language, then. OTOH, many systems
(and /proper/ programming language compilers) have been built using those
languages. To each his own, I guess. To me, a /proper/ programmer should be
able to write a program without depending on the features of /any/ specific
language (or, conversely, should be able to express the program in /any/
language that s/he knows). Programming languages are (or should be)
secondary knowledge to /proper/ programmers; the /primary/ knowledge of
a /proper/ programmer is how to design and write a program that achieves
the programmers goal. But, then again, that's just my opinion. :)
 
B

bpascal123

===O===

Thanks for your interest and all these infos.

Some infos go beyond my present early beginner level but it makes
things look interesting for the futur if I can still have the chance
to keep studying C at this pace (I have started 2 and a half months
ago ).

I 'm not part of a computer university class nor part of a certificate
program where for the first case students have to learn many different
subjects and languages simultaneously and second case unlike java, C
online certificate are not that common unless embedded with C++ (I
understand by the name C++ is an extension of C and there are objects
in C++ and I don't see why each google C query leads to a C++ forum or
site or why C is most of the time link to C++ ?

I feel attracted in C because of the freedom in coding it gives,
unlike coding with objects where I think objects are less easy to deal
with when facing someting new and unexpected. Of course, on the other
hand, objects are more powerful. I am not sure why. It might be
because I have no idea of what objects bring as advantages over lines
of codes.

===O===

About the script ; the first script I have posted on Jun 10, 12:58 am
is working fine, the second one (posted on Jun 10, 1:14 am) is the one
not working. Sorry for this confusion.

===O===

Someone says at this point, it's not a priority to learn how to debug.
So I'll come back later when I'll have bigger program in size, I
understand it should make more sense then.

By the way, I'm not putting anything such as functions away, it's just
I am following book chapters.

In the 2 books I am learning from, functions come just after
pointers ... but in one book (the most difficult but more complete one
"from G. Willms - french Version published by PC Poche") pointers come
far after arrays but still before functions.

In the other book written by "Tony Zhang published in french by Campus
Press / Pearson education") Pointers precede Arrays and Functions.

So, in both books, functions come after arrays and pointers...

But before getting on functions which I believe is something huge, I'd
first look at preprocessor things.

===O===

Some people in this topic say the books I am studying from might be
old. Doest it make a difference at a starting level ? Is there a new C
standardized version that revolutions things ?

===O===

About portability :

I have started learning and coding simple programs on both Linux
Ubuntu and Windows Borland and Dolorie gcc.

The 2 books I am learning from say to use getchar() instead of getche
()

getchar() and
while ( getchar() != '\n' ) ;

to make scripts work on Linux.

As Andrew says puts replaces printf without the '\n'. Puts is only to
be use where no variable arguments such as %d, %s ... At this point,
those 2 books I am learning from tell about puts but don't make much
use of it.

I'll try this script with modifications on Ubuntu but I expect many
other issues.

At this point I'm not focusing on portability. Should portability be a
subject to focus on at this beginner level?

===O===

There are many stuff in the script initially posted where my
understanding is low.

Ex. : atoi(buffer) ;
Does it says to convert alphanumeric characters to integers?
It's the first time I see atoi.

In the book where the script is taken from, it says about atoi it
checks if the number only has zeros in it.

So, I understand atoi() returns a boolean value.

I'll try to use strtol instead, I have never seen it but i'll try to
remember about atoi and strtol or strtoul and the same for gets and
fgets.


===O===

The most difficult thing to understand there was

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

From my understanding, it says as a loop until an empty value is
return to copy the content/variable of * pb into * pc and right after
to increment pb and pc then, the loop restarts and says to copie pb
into pc ...

Is it the right understanding ?

Is it as correct as :

while (* pb) /* while * pb != 0 */
{
* pc = *pb ;
pb ++ ; pc++ ;
}

===O===

I first came here to learn how to debug but it seems there are people
who have answer to my C related question, google takes me most of the
time to C++ forum and websites.

Thanks,

Pascal
 
C

Chris Dollin

Lew said:
On June 10, 2009 09:11, in comp.lang.c, Chris Dollin ([email protected])
wrote:

I guess that there are many programming languages that don't satisfy your
definition for a /proper/ programming language, then.

Oh, yes. Many many. Many many many. It is to weep.
OTOH, many systems (and /proper/ programming language compilers) have
been built using those languages.

Of course. I didn't say those languages were useless. (Painful,
that I would say.)
To each his own, I guess. To me, a /proper/ programmer should be
able to write a program without depending on the features of /any/ specific
language (or, conversely, should be able to express the program in /any/
language that s/he knows). Programming languages are (or should be)
secondary knowledge to /proper/ programmers; the /primary/ knowledge of
a /proper/ programmer is how to design and write a program that achieves
the programmers goal. But, then again, that's just my opinion. :)

I don't believe that such design can be done without at least a
view as to which (kind of) language will be used; I don't believe
programming can be done without a programming language. Certainly
there is /some/ transferable knowledge.

About /proper programmers/ I make and made no claims in these two
messages.
 
B

Ben Bacarisse

By the way, I'm not putting anything such as functions away, it's just
I am following book chapters.

In the 2 books I am learning from, functions come just after
pointers ... but in one book (the most difficult but more complete one
"from G. Willms - french Version published by PC Poche") pointers come
far after arrays but still before functions.

In the other book written by "Tony Zhang published in french by Campus
Press / Pearson education") Pointers precede Arrays and Functions.

So, in both books, functions come after arrays and pointers...

But before getting on functions which I believe is something huge, I'd
first look at preprocessor things.

All I can say is that this is not a good sign for these books. Can
you get hold of anything else? I don't suggest learning in a
different order just to be awkward -- it really makes like simpler.

There are many stuff in the script initially posted where my
understanding is low.

Ex. : atoi(buffer) ;
Does it says to convert alphanumeric characters to integers?
It's the first time I see atoi.

In the book where the script is taken from, it says about atoi it
checks if the number only has zeros in it.

So, I understand atoi() returns a boolean value.

In C, all integers can be used in a boolean context (an 'if', a
'while' and so on) but atoi returns the numerical value of the initial
portion of the string it is passed. Since in C 0 is taken to be false
and non-zero taken to be true, testing atoi(some_string) can tell you
if it contains digits other than zero at the start. It is not a good
way to do this test, but it is close enough.

The most difficult thing to understand there was

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

From my understanding, it says as a loop until an empty value is
return to copy the content/variable of * pb into * pc and right after
to increment pb and pc then, the loop restarts and says to copie pb
into pc ...

Is it the right understanding ?

No. == tests for equality. Nothing is copied. It may be that = was
intended as that will copy the characters from pb to pc.
Is it as correct as :

while (* pb) /* while * pb != 0 */
{
* pc = *pb ;
pb ++ ; pc++ ;
}

No. This loop is different to the one above and it is different from
what I think you might have meant! Even if the loop above had been

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

the second version remains different. Rather than tell you, step
through what they do when pb points to the 'a' in "abc".
 
R

Richard Bos

Chris Dollin said:
Prolog (it has relations) and more generally rule-based languages.
(Rules are not functions.) Early BASICs.

Dartmouth BASIC had DEF FN. It's a very limited form of function, but it
is a function definition.

Richard
 
W

Willem

Chris Dollin wrote:
) (e-mail address removed) wrote:
)
)> there are plenty of languages without pointers and, I'd argue, none
)> without functions.
)
) Prolog (it has relations) and more generally rule-based languages.
) (Rules are not functions.) Early BASICs. Assembler. Does COBOL have
) functions?

Was there a BASIC without GOSUB ?

Most CPU's have a call/return or jsr/ret or similar set of instructions.
Cobol has something similar too, I think.

Granted, these are procedures, not functions, but in the sense of this
discussion we were more talking about procedures anyway.

I think the Prolog 'rules' can also be classified as procedures ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
A

Andrew Poelstra

[snip]

Someone says at this point, it's not a priority to learn how to debug.
So I'll come back later when I'll have bigger program in size, I
understand it should make more sense then.

I'd agree with this - for any program less than a few hundred lines,
it's probably easier to step through the code manually than to use a
debugger anyway.
[snip]

Some people in this topic say the books I am studying from might be
old. Doest it make a difference at a starting level ? Is there a new C
standardized version that revolutions things ?

Not since 1989. (There was another standardized version in 1999, but it
was neither revolutionary nor widely adopted.)

The issue with using an older book is that programming technique (and
teaching techniques) have evolved immensely over the years. For example,
back in the day micro-optimizations - loops counting down instead of up,
and such things - were useful. But now this is considered "premature
optimization" and the goal is always to make your code readable, rather
than fast.

This is true of all languages, not just C.
[snip]

The 2 books I am learning from say to use getchar() instead of getche
()

getchar() and
while ( getchar() != '\n' ) ;

to make scripts work on Linux.

getchar() will work -everywhere- that C does, not just Linux, and
including Windows. Why the book would use getche() at all is beyond me.
[snip]

At this point I'm not focusing on portability. Should portability be a
subject to focus on at this beginner level?

My opinion (and the one shared by most of this group) is that unless you
have a reason not to, you should focus on portability.

Certainly, anything taught in a beginner's C book should be done
portably. The reason for this is that you can learn to do everything the
portable way, and then later on when you need to do things that are non-
portable (say, using display hardware or networking or whatever), you'll
be able to write most of your program portably.

As for the non-portable bits, they can be hidden away in functions, and
then when you need to move your code, you only have to change the
functions, not the main logic.
[snip]

I first came here to learn how to debug but it seems there are people
who have answer to my C related question, google takes me most of the
time to C++ forum and websites.

That is true - unfortunately, like too many new programmers, Google does
not know the difference between C and C++. You can try searching C in
quotes like "C", and that should reduce hits for the word C++.

But bear in mind that much of the information on the Internet (for both
C and C++) is sloppy code, and often non-portable or simply wrong.
 
N

Nick Keighley

I guess that there are many programming languages that don't satisfy your
definition for a /proper/ programming language, then.

the majority I think! Including C!

<snip>
 
N

Nick Keighley

On 10 June, 14:33, "(e-mail address removed)"

(I
understand by the name C++ is an extension of C

that's what the name implies but there are enough additions and
changes
to C++ to make it, really, a different language. Although most well
written
C will compile and execute correctly with a C++ compiler in reality
idiomatic C++ doesn't look much like idiomatic C.

and there are objects
in C++ and I don't see why each google C query leads to a C++ forum or
site or why C is most of the time link to C++ ?

bad luck. Some people confuse the two (maybe google does).

I feel attracted in C because of the freedom in coding it gives,
unlike coding with objects

I don't think OO programming is significantly "less free". Whatever
*that* means!

where I think objects are less easy to deal
with when facing someting new and unexpected.

they're actually pretty good with the new and unexpected things
but they add to the beginner's learning curve. Particularly
a language like C++ which is C with objects (and other stuff!)
added. Remember most C programs are also C++ programs
(or close to).
Of course, on the other
hand, objects are more powerful. I am not sure why.

their advantages may not be very obvious until you
start to deal with large programs. They allow you to break
a large program into small pieces (C provides techniques
to do this as well). This modularisation is what allows
complex systesm to be developed. Some of the most
complex artifacts on earth are computer programs.

It might be
because I have no idea of what objects bring as advantages over lines
of codes.

even objects end up as lines of code somewhere!

Someone says at this point, it's not a priority to learn how to debug.

it's a priority to learn how to write correct programs!
If you can't write 'em correctly from scratch (and few people can)
you're going to have to debug 'em some how!

Note "debug" does not necessarily mean "use a debugger".

[hi richard!]

Often
it means examine the output or output from intermediate stages
and reason about the program until you work out why the behaviour
of the program is different from that which you expect. A debugger
can help you do this but it isn't the only way.

- good quality logging
- test suites
- run time assertions
- prrofs of correctness (these are hard actually!)

So I'll come back later when I'll have bigger program in size, I
understand it should make more sense then.

if you can't write small correct programs you won't write big correct
programs.

By the way, I'm not putting anything such as functions away, it's just
I am following book chapters.

In the 2 books I am learning from, functions come just after
pointers ... but in one book (the most difficult but more complete one
"from G. Willms - french Version published by PC Poche") pointers come
far after arrays but still before functions.

In the other book written by "Tony Zhang published in french by Campus
Press / Pearson education") Pointers precede Arrays and Functions.

So, in both books, functions come after arrays and pointers...

But before getting on functions which I believe is something huge,

you may be pleasantly surprised. I'd say functions were absolutely
fundamental to programming. But that doesn't really make them hard.
For technical reasons C needs pointers to do certain things with
functions (pass modifiable arguments) that other languages do by
other means. C is very low level" Very close to the machine. This
is its blessing and its curse.
I'd first look at preprocessor things.

the preprocessor is pretty ugly, but necessary

Some people in this topic say the books I am studying from might be
old. Doest it make a difference at a starting level ? Is there a new C
standardized version that revolutions things ?

C divides up in to roughly three eras.

1. pre-1989 there was no formal standard but a defacto one
2. 1989 standard
3. 1999 standard

The 1989 standard will still run all the well written pre-standard
stuff. You don't seem to be writing to the pre-1989 "standard"
so ignore it. The 1999 standard was very slow to be adopted. It is
*still*
not widely implemented and many users who value C for its portability
use the 1989 standard. Your code looks ok for either 1989 of 1999.
They didn't discard anything important in the 1999 standard (pre-1989
code might have problems).

At this point I'm not focusing on portability. Should portability be a
subject to focus on at this beginner level?

not really, but if you know a portable way to do something
why not do it? Running on Windows and Unix will tend to draw
your attention to really non-portable things.

===O===

There are many stuff in the script initially posted where my
understanding is low.

Ex. : atoi(buffer) ;
Does it says to convert alphanumeric characters to integers?
It's the first time I see atoi.

yes. You need a good reference. Eg. K&R. But this isn't bad:-

http://www.dinkumware.com/manuals/default.aspx#Standard C Library

just typeing "atoi" into google has a good chance of answering your
question.

In the book where the script is taken from, it says about atoi it
checks if the number only has zeros in it.

this sounds wrong. Do you mean "...only has digits in it"?
So, I understand atoi() returns a boolean value.

nope. Booleans are either true or false. atoi() returns an integer.
I'll try to use strtol instead, I have never seen it but i'll try to
remember about atoi and strtol or strtoul and the same for gets and
fgets.

atoi() isn't as bad as gets(). atoi() only gives you the wrong
answer, gets() opens a gigantic security hole that allows all sorts
of exploits and virii.

===O===

The most difficult thing to understand there was

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

that is rather idiomatic
From my understanding, it says as a loop until an empty value is
return to copy the content/variable of * pb into * pc and right after
to increment pb and pc then, the loop restarts and says to copie pb
into pc ...

Is it the right understanding ?

Is it as correct as :

while (* pb)    /* while * pb != 0 */
{
* pc = *pb ;
pb ++ ; pc++ ;

}

yes. excellent!

===O===

I first came here to learn how to debug but it seems there are people
who have answer to my C related question, google takes me most of the
time to C++ forum and websites.

Thanks,

no problem
 
C

Chris Dollin

Willem said:
Chris Dollin wrote:
) (e-mail address removed) wrote:
)
)> there are plenty of languages without pointers and, I'd argue, none
)> without functions.
)
) Prolog (it has relations) and more generally rule-based languages.
) (Rules are not functions.) Early BASICs. Assembler. Does COBOL have
) functions?

Was there a BASIC without GOSUB ?

Unlikely. Since GOSUB doesn't provide you with functions, it
doesn't matter. It doesn't even provide you with procedures,
IMAO.
I think the Prolog 'rules' can also be classified as procedures ?

At the risk of being incorrect, I'd say.
 
R

Richard Bos

Nick Keighley said:
atoi() isn't as bad as gets().

Yes, but...
atoi() only gives you the wrong answer,

....no. atoi() isn't as bad as gets() only because, making more effort
than you should need to, you _can_ stop atoi() from having undefined
behaviour using ISO C only. You cannot do that with gets(). However, if
you don't make that effort, and pass atoi() a string it cannot handle,
it will still have behaviour that is equally as undefined as gets().

In the mean time, Pascal has made the right choice: use strtol().
gets() opens a gigantic security hole that allows all sorts
of exploits and virii.
^^
*THWACK* There ain't no such word. If you don't speak Latin, don't
pretend to.

Richard
 
N

Nate Eldredge

Chris Dollin said:
Unlikely. Since GOSUB doesn't provide you with functions, it
doesn't matter. It doesn't even provide you with procedures,
IMAO.

If not "procedure", what name would you give to a self-contained piece
of code intended to be called via GOSUB?
 
L

Lew Pitcher

Chris Dollin wrote:
) (e-mail address removed) wrote:
)
)> there are plenty of languages without pointers and, I'd argue, none
)> without functions.
)
) Prolog (it has relations) and more generally rule-based languages.
) (Rules are not functions.) Early BASICs. Assembler. Does COBOL have
) functions?

Was there a BASIC without GOSUB ?

Apparently, there is/was.

GOSUB is part of the original (Dartmouth) BASIC, but (so the Wikipedia
article says) FreeBASIC deprecated (and disabled) GOSUB in favour of
the "SUB" keyword.

[snip]

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Ben Pfaff

Nate Eldredge said:
If not "procedure", what name would you give to a self-contained piece
of code intended to be called via GOSUB?

In the versions of BASIC that I used, GOSUB subroutines were
hardly self-contained, because there was no notion of local
variables.
 
K

Keith Thompson

Nate Eldredge said:
If not "procedure", what name would you give to a self-contained piece
of code intended to be called via GOSUB?

I'd call it a subroutine, but I think of both functions and procedures
as kinds of subroutines.

This is all meaningless without a definition of the word "procedure".
And since C doesn't use the word "procedure", ...
 
B

bpascal123

OK Ok ok,

I have made quite a gigantic step with some answers, thanks!

I will now try to focus on portablity even if it's boring to switch
between xp and linux ubuntu...

ONE THING I want to say is that I didn't "invent" any lines of this
script, I have copied and learned it by heart from a book written by
G.Willms, so credit and discredit should be addressed to him ... I
wouldn't say if there is anything right or not right for this little
script. To me, I find this book conveniant (that's a beginner point of
view). I'm sure there are a lot more books and if I want to go further
with C, I will look for a good reference (the price should match the
content).

By the way, my method to learn is 3/4th of the time to learn by heart,
like to write a script like this one over and over until i remember
all of it or the most important part.

1/4th of the time to read and practise through exercices. Then when i
practise, i recall what i have learned by heart and then of course I'm
not close to the official solution (i need more variable and i have
less controls of the user's input) but the program works.
I don't know if it'a good method but to be honest i find it easy to
implement in terms of concentration and stuff...

Pascal
 
R

Richard Bos

Nate Eldredge said:
If not "procedure", what name would you give to a self-contained piece
of code intended to be called via GOSUB?

Officially (Dartmouth again) it was called a subroutine, surprisingly
enough.

Richard
 
Z

zorro

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 ?

C is a low-level language, while it can be used for high-level app
programming, it's natural domain is in embedded and system
programming, or compiler writing. As an accountant you are better off
starting with a more high-level language, like Java. Before doing
that, learning a scripting language like Python, could be useful.
 
B

bpascal123

C is a low-level language, while it can be used for high-level app
programming, it's natural domain is in embedded and system
programming, or compiler writing. As an accountant you are better off
starting with a more high-level language, like Java. Before doing
that, learning a scripting language like Python, could be useful.

Yes, but i have readen somewhere that C is useful when other languages
can't solve a specific matter and it is quite good to understand the
basics of C.

So far, I seek not to spend my entire life with C ( around 600 hours
or 1-2 years, if i don't have too many duties...)

Then I would learn C++ for 300 hours (I have readen C++ is more for
game programming but i belive you can do a lot more with C++).

Aftward, a quick look at perl and a deeper look at Python (maybe 2
years overall).

Then I'd see what's hot and what are my goals at this time ;

-web developpements (I have a good knowledge of html and an I have
been dealing with css for about 1 year and I had some insights of
php...

-database programming (this is close to an accountant's job).

I am doing things at a personal pace, for instance, one day I can
learn and write 10 scripts, and for a week i can just let it go
because of personal duties, fatigue... but i read at least everyday a
chapter or a programm about in C.

I can only say my skills in a any of these languages mentionned above
will remain basics for the next 10 years.
Then i hope to be able to talk seriously with real programmers, i
don't know, maybe getting to know how car systems work, gps, mobile
phones, online databases... computer is a very wide area, I just hope
not to get lost.
 
B

BartC

Yes, but i have readen somewhere that C is useful when other languages
can't solve a specific matter and it is quite good to understand the
basics of C.

So far, I seek not to spend my entire life with C ( around 600 hours
or 1-2 years, if i don't have too many duties...)

The problem is you might get frustrated by not being able anything very
exciting in C without a lot of fiddly, error-prone code. Or without
importing third party libraries which is an area of expertise all by itself.

So the suggestion to use a 'softer' language first is good and will give a
useful perspective.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top