Re: learning c and c++

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/09/04


Date: Fri, 08 Oct 2004 22:31:44 GMT


"maurizio" <mcivenn@tin.it> wrote in message
news:rfC9d.10057$b5.391417@news3.tin.it...
>
> "Mike Wahler" <mkwahler@mkwahler.net> ha scritto nel messaggio
> news:iEA9d.6899$UP1.2917@newsread1.news.pas.earthlink.net...
> > "maurizio" <mcivenn@tin.it> wrote in message
> > news:BjA9d.94749$35.4457176@news4.tin.it...
> > > I have started since few days programming c and c++
> >
> > First, in my opinion it is a big mistake to try to
> > learn two separate computer languages at the same time,
> > especially two as complex as C and C++. Further exacerbating
> > the problem is that C and C++ are similar enough as to confuse
> > even the most discerning mind as to the correct use of either.
> >
> > >and I haven't found good
> > > books, articles, examples.
> >
> > Another problem is that there is a plethora of incorrect
> > information about C and C++ (especially C++) available on
> > the Internet. It seems that perhaps you want to protect
> > against this, so I applaud you for coming here first to
> > seek advice about learning materials.
> >
> > >Could someone help me to find something very good
> > > to learn fast and good?
> >
> > First I strongly advise you to choose one language or the other,
> > learn it first, then learn the other. (You may have heard others
> > say that knowledge of C is necessary in order to learn C++. This
> > is patently untrue).
> >
> > Having said all this, you can see peer reviews and recommendations
> > of books on C and C++ at the web site of the "Association of C and
> > C++ Users", at www.accu.org . I recommend obtaining at least two
> > books on each desired language, more would be better, but of course
> > one's budget must be considered.
> >
> > And of course you can come to this newsgroup to ask questions and
> > discuss particular issues, concerns, and problems you might have
> > with learning either or both languages.
> >
> > Finally, be sure to see this newsgroup's FAQ document:
> > http://ma.rtij.nl/acllc-c++.FAQ.html
> >
> > It has links to many useful resources, as well as to separate
> > FAQ documents for each langauge, C and C++.
> >
> >
> > Good luck!
> >
> > -Mike
> >
> > I am a good java programmer and I have read a book about C and it's not
> really clear

Which book? Have you looked at the reviews at accu.org ?
If there's something in your book you don't understand,
you can quote it here and ask questions about it.

> in fact I have problem about chanching values between variables(if is
> possible) int to char,

Types 'int' and 'char' are both 'integer types'. The value
of one can be simply assigned to the other (keeping in mind
that the range of 'char' is typically smaller than that of
'int') If you know that a particular 'int' value is within
the range of a 'char', you can suppress a possible compiler
warning with a cast.

E.g.

char c = 'X';
char d = 0;
int i = 0;
int j = 42;

i = c; /* 'i' now holds the value of 'c' */
d = (char)j; /* 'd' how holds the valud of 'j' */

The language guarantees that the minimum range of a
'signed char' is at least -127 through 127,
'unsigned char' is at least 0 through 255,
'int' is at least -32767 through 32767.
Whether or not 'plain' type 'char' is signed or
unsigned is left up to the implementation. Check
your documenation to see which it is for yours.

> int or double to string.

Types 'int' and 'double' are numeric types, 'string' is
not, so it doesn't make sense to convert e.g. an 'int'
to a 'string'. But you can store the textual representation
of a number (i.e. the characters representing its digits)
in a string.

Also note that C doesn't have a "real" string type, but
represents them with arrays of characters, the last of which
is a character with value zero (known as the string's
'terminator'). C++ does supply a 'string' type in its standard
library.

There are various ways to create the text
representation of a number:

C and C++: The standard functions 'sprintf()'.
C++: a stringstream object and its inserter operators,
(or 'sprintf()).

/* C or C++: */
#include <stdio.h>

int main()
{
    int i = 42;
    char text[10] = {0};
    sprintf(text, "%d", i);
    puts(text); /* prints 42 */
    return 0;
}

/* C++: */

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    int i(42);
    std::ostringstream oss;
    oss << i;
    std::string s(oss.str());
    std::cout << s << '\n'; /* prints 42 */
    return 0;
}

Another way is to write your own function to do it,
which can be an excellent learning exercise.

>I haven't found nothing to use the <time> lib.

There's no header <time> in either language. C and C++
have <time.h>, and C++ has <ctime>. Both headers declare
several functions for manipulating time values.

If you have a particular thing you want to do with time,
I can show you an example.

> About C++ I start reading the on-line book of Bruce Eckel

That's the only online C++ book I'd recommend.

>but it's not
> really clear like about java.

Feel free to quote any passages you don't understand
and ask questions about them here.

Again, I'll stress that you should choose one language
at a time to learn.

-Mike



Relevant Pages

  • Re: socket communication: send & receive doesnt work right
    ... So I don't want to send a string as bytes. ... public void send_doubles(double vals, int len) throws ... // send a short acknowledgement to the server ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: [PATCH] markers: modpost
    ... pointers to the name/format string pairs. ... The same can then be done with modules using the __markers section. ... +static void read_markers(const char *fname) ... int main ...
    (Linux-Kernel)
  • Re: [PATCH] markers: modpost
    ... This adds some new magic in the MODPOST phase for CONFIG_MARKERS. ... will be a neighbor of its format string. ... +static void read_markers(const char *fname) ... int main ...
    (Linux-Kernel)
  • Re: Is this code totaly a shit?
    ... | void UppStrg(char *Low, char *Upp, int cnt); ... whitespace-delimited string. ... You're also assuming that the representations of the characters ...
    (comp.lang.c)
  • Re: A string collection abstract data type
    ... duplicate string in Insert, InsertAt, ReplaceAt; ... used int instead of size_t for count and size for consistency with API. ... char *(StringCollection *SC, int idx, ... static int IsReadOnly; ...
    (comp.lang.c)