I need your advices about C prg.

K

Keith Thompson

Robert Wessel said:
James Kuyper said:
On 04/25/2013 02:17 PM, Dogukan Bayraktar wrote:
25 Nisan 2013 Per?embe 20:44:06 UTC+3 tarihinde James Kuyper yazd?: [...]
I have got a file of .PDF about C programming called "K&D C Programming".

That should be "K&R", standing for "Kernighan and Ritchie", the authors.

If so, you've probably got an illegal copy of it. As far as I know, K&R
is not legally available other than as a paper book. (By "legal" I mean
"not violating copyright".)

K&R2 is available as a Kindle e-book from Amazon. I'm not aware of
any legal electronic versions of K&R1.

http://amzn.com/B009ZUZ9FW

Cool, I didn't know that; it must be fairly new.

Still, a PDF copy of K&R is most likely a copyright violation.
 
K

Keith Thompson

Tim Rentsch said:
Sure, but doing that has the problems the earlier poster
was complaining about ("Both of these problems"), which
is what prompted me to respond in the first place.

The other problem being complaining about either unreachable code or a
missing return value. The latter shouldn't be an issue for a C99 or
later implementation, which makes a missing return statement in main()
equivalent to "return 0;".
 
T

Tim Rentsch

Keith Thompson said:
Tim Rentsch said:
Keith Thompson said:
[...]
Both these problems can be avoided by writing main() this
way:

int
main( void ){

/* ... */
/* whatever main does in its loop body */
/* ... */

return main();
}

Of course I'm not completely serious, but neither is it
completely a joke. Presented with such a program, gcc
compiles it (under -O2) as a simple loop, not a call, and
with no generated return opcode or any other extra clutter.

Making the body of main() an obvious infinite loop has the same
effect.

Sure, but doing that has the problems the earlier poster
was complaining about ("Both of these problems"), which
is what prompted me to respond in the first place.

The other problem being complaining about either unreachable
code or a missing return value. The latter shouldn't be an
issue for a C99 or later implementation, which makes a missing
return statement in main() equivalent to "return 0;".

Just because the behavior is defined doesn't mean the compiler
won't issue a warning about it. There is even a reasonable
argument that it should get a warning, since C90 is different
in this regard. And for all we know the code in question may
be C90 code, not C99.

To be fair, we don't know that the compiler(s) in question will
compile the tail-recursive version as a loop rather than a call
either. I certainly don't presume to say this technique is
appropriate in all circumstances, or even many circumstances.
But in some circumstances - and reasonably plausible ones - it
may offer advantages that other alternatives don't. The point
is to provide another approach - one more arrow in the quiver,
so to speak - that may be attractive in some cases. I don't
know of any programming problem where just one approach is the
best choice in all possible cases.
 
G

glen herrmannsfeldt

There's also a lot of confusion about what "embedded programming" is.
My employers call what I do embedded programming, but it's really
for the most part ordinary Unix server programming.

Yes, more and more embedded systems run Linux, or something similar.

I suppose I call it embedded if it boots completely from ROM, EPROM,
EEPROM, or FLASH. The latter does not include FLASH interfaced through
one of the usual disk interfaces such as SATA.

The usual NAT router/wireless access point devices run Linux, for
example.

-- glen
 
S

Stephen Sprunk

From where I'm from, a foreign language (english) is included in the
national curriculum from the 1st grade, and a second foreign
language from the 7th grade onward.

For the second foreign language, students are free (technically*) to
pick from three available options -- german, french and spanish.

These choices are far from being "obvious", for a number of reasons.
Yet, people still choose and no one is harmed by it.

English as a second language is rapidly being mandated around the world;
what language such students might choose after that is irrelevant to
this discussion since they already know English.
Regarding english-speaking countries, it can be argued that each one
has its glaring obvious choice.

- US: According to wikipedia[1], the country has 45 million people
who speak spanish as a first or second language. If we take into
account that almost all neighbouring countries to the south have
spanish as their official language, spanish does appear to be an
obvious choice.

~95% of Americans speak English on some level, including most native
Spanish-speakers. Legal immigrants must know it to get a visa in the
first place, and their kids are forced to learn it in school. That
leaves only illegal immigrants, and many of them pick up English as well
to avoid detection and/or get higher-paying jobs.

Culturally, I agree that it would be a good to learn Spanish to speak to
our neighbors to the south, but it's a poor economic choice--and
mandating thatt would be political suicide. Learning Portuguese would
be a better economic choice, but Brazilians students are learning
English now, so any benefit there would be short-lived.
- Canada: it has two official languages: english and
french.

Aside from those living in Quebec or working for the govt, there's not
much reason to learn French, since even the Quebecois learn English in
school now.
- UK: even when ignoring their "regional" languages, the
british isles do have significant ties with France and Germany.

.... and France and Germany both mandate English as a second language, so
any benefit to Brits learning those languages would be short-lived.
On top of that, if only less than 6% of the world are native english
speakers while over 14% speak Mandarin[2], in addition to China's
increasing prominence, Mandarin becomes a rather glaring choice.

1.8 billion people speak English, compared to 1.3 billion who speak
Mandarin. Every student in China is now required to learn English, and
it is projected that within a decade China will have more English
speakers than the US itself.

So, Mandarin is rapidly becoming a poor choice as well.

It's not only native languages that matter, especially when we're
discussing what is by far the most popular _second_ language in the
world. See:
http://en.wikipedia.org/wiki/World_language

Also, of over 6,000 languages spoken today, only half are being taught
as a native language to the current generation of kids. A handful of
languages are strong enough today to resist eradication via cohort
replacement, and those languages' native speakers are already learning
English as a second language, so they'll probably get hit in a few more
generations as well when they see they're getting left behind by those
who have already switched.

S
 
S

Seebs

No, "main" does not always have a return parameter - "main" does not
always return, and therefore a return parameter is meaningless.

This analysis is incorrect.

On at least some systems, the calling sequence depends on what type
the return would be if the function returned, so even a function that
never returns must have the correct return type.

There's an old joke about an atheist visiting Ireland during a time of
more strenuous conflict, and someone asked him whether he favored the Catholic
or Protestant side. He explained that, as an atheist, he did not believe
in God. And someone said, "Yes, but is it the God of the Catholics or the
Protestants that you don't believe in?"

Similarly, in C, even if you never return, it may matter what it is that
you are never returning.
I don't know whether this is from an official standards document, but
point 167 clearly says that "main" can be defined "in some other
implemention-defined manner".

Which is to say, a specific implementation may describe other forms.

But they aren't required to.

I'd also point out: There are now a fair number of "embedded systems" where
the bulk of actual work is done on a perfectly ordinary Linux system, so on
the entire program, there is exactly one piece of code which has a main()
which is not a standard hosted-environment C program main(), that being the
kernel. Everything else has a normal main() which returns int. Even if it
never returns.

-s
 
S

Seebs

Firstly, sorry for my bad English :/

Eh, it happens. It's my native language and I still make mistakes, after
years writing professionally.
I'm only 17 and I want to learn C and write perfect codes in C.
I'm not so good in C programming, but I want to become a good C
programmer.

Well, if you think you're not so good, you're already ahead of a great
number of people I've had to deal with. :)
My English is not enough to read the programming articles or anything.
What should I do to become a good C programmer?

Read, write. Code and language. And don't neglect things like philosophy,
psychology, and so on; everything you know can help you be better at
programming. Psychology can help you understand people, and understanding
people helps you write programs that suit them, or write code they can
read. Philosophy can help you decide what to work on. And so on.
- Do you think is English needed for programming?

Needed, not exactly, but at this point I think it's a de facto standard;
enough highly valuable material is written in English that I would recommend
practicing it even if you don't need it.
- What should I learn in C to become a good programmer?

Everything you can! :)
- Should I read the example C codes in the web?

Yes.

My basic strategy when starting out was: Find things I want to do, do them.

I will say, learning other (programming) languages has helped me more with
the quality of my C code than I expected. Even if a thing is harder to do in
C than it is in another language, knowing that it might be possible to do
can be useful.

-s
 
G

glen herrmannsfeldt

This analysis is incorrect.
On at least some systems, the calling sequence depends on what type
the return would be if the function returned, so even a function that
never returns must have the correct return type.

This is true, but the differences are most often related to actually
returning.

If the return value isn't returned in a register, then it is usual
to add an extra argument for it. Bad things happen if you return and
get that wrong. That is usually true for a struct return value,
sometimes for floating point, but rarely for int.

It would cause problems if all the other arguments (argc, argv) were
moved, (relative to their position for void) and one was referenced.
There's an old joke about an atheist visiting Ireland during a time of
more strenuous conflict, and someone asked him whether he favored the Catholic
or Protestant side. He explained that, as an atheist, he did not believe
in God. And someone said, "Yes, but is it the God of the Catholics or the
Protestants that you don't believe in?"
Similarly, in C, even if you never return, it may matter what it is that
you are never returning.

-- glen
 
J

James Kuyper

On 04/25/2013 07:05 PM, Stephen Sprunk wrote:
....
~95% of Americans speak English on some level, including most native
Spanish-speakers. Legal immigrants must know it to get a visa in the
first place, and their kids are forced to learn it in school. That
leaves only illegal immigrants, and many of them pick up English as well
to avoid detection and/or get higher-paying jobs.

Culturally, I agree that it would be a good to learn Spanish to speak to
our neighbors to the south, but it's a poor economic choice--and
mandating thatt would be political suicide. Learning Portuguese would
be a better economic choice, but Brazilians students are learning
English now, so any benefit there would be short-lived.

The entire southwest portion of the US used to be controlled by Spanish
speaking people, and Spanish remains pervasive throughout that region.
Many people for whom Spanish is their birth language come from families
that have been US citizens for 3 generations, or longer - some can trace
their presence in the region to back before it was seized by the US.
Spanish was my first foreign language, and it remains by far the most
useful of the five that I've learned, even though I now live in the
Maryland (which also, for different reasons, has a significant
Spanish-speaking population).
Aside from those living in Quebec or working for the govt, there's not
much reason to learn French, since even the Quebecois learn English in
school now.

Many Québécois are much more cooperative with those who at least attempt
to speak with them in French. You can insist on using English, but the
more obstinate ones will respond by pretending not to understand.

....
1.8 billion people speak English, compared to 1.3 billion who speak
Mandarin. Every student in China is now required to learn English, and
it is projected that within a decade China will have more English
speakers than the US itself.

I can vouch personally for the fact that the English spoken by many of
those students is quite poor (it's not as poor as my Mandarin,
unfortunately - you might think that with a Mandarin-speaking wife, I'd
have plenty of opportunities to practice, but it hasn't worked out that
way). I wouldn't recommend counting on being able to communicate with
all of them using English.
 
M

Malcolm McLean

1.8 billion people speak English, compared to 1.3 billion who speak
Mandarin. Every student in China is now required to learn English, and
it is projected that within a decade China will have more English
speakers than the US itself.
Actually they write Mandarin. The various dialects aren't mutually intelligible.
Since Chinese uses a pictogram system, you can have the same pictogram
pronounced in totally different ways, without breaking the writing system.
 
J

James Kuyper

Actually they write Mandarin.

The Chinese speak and hear multiple spoken languages, such as Mandarin
or Cantonese, which they think of as dialects of a single language,
though linguists consider several of them to be distinct languages.
However, they read and write essentially a single written Chinese
language (中文 or ZhÅngwén), though it is written slightly differently
by speakers of the different spoken languages.
 
R

Rui Maciel

David said:
There are more /native/ speakers of Mandarin than /native/ speakers of
English (and Spanish falls between these two). However, there are more
people who speak English than any other language - mostly as a second
language. Figures vary wildly depending on what levels of competence
you look for, but something like 20% is probably a decent estimate (of
native and second-language speakers).

The second languages are taught because they are useful to communicate with
those who don't speak our native language. Therefore, their choice is
motivated by how useful they might be to communicate with others.

In the last decade or so, english managed to become a de facto esperanto,
but that was mainly because the US dominated the world's economy and
academia. Things won't stay like that for long.

In addition, in some western countries it's much more likely that you
stumble on someone who speaks mandarin natively than english.

Again, a couple of decades ago francophones were using the same arguments to
justify why it would be silly for them to learn a second language.

If you narrow the field to people who are seriously interested in
programming - and therefore mostly young, literate, reasonably well
educated, and familiar with computers, then I expect the percentage of
people who can read reasonable English and have at least basic English
written and oral skills is going to be very much higher.

Hardware production is already firmly established in Asia. I wouldn't bet
that in a decade or so english will still be the prefered language to
communicate with them.

Regarding software, I've published a small library (written in C, so let's
pretend it's on-topic) which is distributed under a FLOSS license and
managed to be somewhat popular. Currently, according to the site's download
statistics, the most number of downloads are by far from China, while those
from the US amount to only about 70% of that.

Singapore is ranked 4th, and Taiwan ranks 9th, right after the UK.


Rui Maciel
 
S

Stephen Sprunk

Actually they write Mandarin. The various dialects aren't mutually
intelligible. Since Chinese uses a pictogram system, you can have the
same pictogram pronounced in totally different ways, without breaking
the writing system.

1.3 billion is for Mandarin speakers only; Cantonese, Taiwanese, etc.
speakers are not included, even though they use (roughly) the same
writing system.

The PRC mandated Mandarin in all schools, govt offices, etc. on the
mainland, so now only the elderly remember their region's dialect.
Non-PRC areas kept their dialects, eg. Cantonese in Hong Kong and Macau,
Taiwanese in Taiwan, etc.

There is a difference between "Simplified" (PRC) and "Traditional"
(non-PRC) writing, but natives tell me that the two are mutually
intelligible, something like a continental European still being able to
recognize their language without its normal diacritical marks.

S
 
J

James Kuyper

On 04/26/2013 11:02 AM, Stephen Sprunk wrote:
....
The PRC mandated Mandarin in all schools, govt offices, etc. on the
mainland, so now only the elderly remember their region's dialect.
Non-PRC areas kept their dialects, eg. Cantonese in Hong Kong and Macau,
Taiwanese in Taiwan, etc.

Cantonese is still quite widely used in Canton itself and in neighboring
provinces.
There is a difference between "Simplified" (PRC) and "Traditional"
(non-PRC) writing, but natives tell me that the two are mutually
intelligible, something like a continental European still being able to
recognize their language without its normal diacritical marks.

My wife tells me differently. Raised in Taiwan, where they still use
almost exclusively the Traditional characters, she finds the simplified
ones almost unintelligible. That fits my own experience - I had a hard
time finding textbooks that teach the traditional characters; the best I
could do is find one that taught both forms. I learned to write both
simplified and traditional characters, and they were quite different.
The difference is far greater than, for instance, the difference between
the Helvitica and Fraktur typefaces.
 
S

Seebs

Still, a PDF copy of K&R is most likely a copyright violation.

Seems quite likely, although I'm not sure -- the question of how fair use
applies to media shifts is moderately open.

That said: As someone who has made a living writing now and then, I would
point out that I have never seen any evidence that I should care about
copyright violations of my writing, in general. By and large, if I'm selling
something, I care about sales. The theory that a given non-sale might have
been a sale otherwise is interesting, but I've seen no statistics bearing
out that it's a generally applicable pattern; rather, everything I've seen
suggests that widespread copying of a work tends to increase sales.

I know a lot of people who want to learn to program and can't, right now,
afford books. And worrying about whether they are getting their books
illegally is in general really, really, low on my priority list.

Note: I don't have the legal authority to tell people that they are welcome
to free copies of my shell book, because the publisher has a vote in the
matter. However, having access to concrete sales figures on at least one other
book, and knowing how people copying it freely affected them... Well, I
am pretty sure that people trading PDFs of my book would be in my economic
best interests, whether or not it is authorized by the publisher.

Economics: Weird.

-s
 
S

Seebs

There's also a lot of confusion about what "embedded programming" is.
My employers call what I do embedded programming, but it's really
for the most part ordinary Unix server programming.

I don't think this is really a confusion, just... Times change.

When I was first active on comp.lang.c, I had what was at the time a fairly
powerful computer. It had 6MB of memory, I believe, and a 25 MHz processor,
and I think a 200MB hard drive.

Nowadays, I do embedded systems stuff. We do, in fact, I believe, still
actually talk to people about targets that have less than 200MB of available
long-term storage. I don't think I've heard of anyone actually using a
system with under 64MB of main memory with this product. I mean, I'm sure
it must happen, but it doesn't come up much. If I have to look at a target,
it's typically got at least one 700MHz or faster core, and realistically
it's pretty common for it to have 2-8 cores at >1GHz, at least 512MB-1GB
of memory (or quite a bit more), and so on. I take it for granted that users
will want decent performance when assembling filesystems 1GB or more in
size that will be installed on thousands+ of embedded devices.

At which point, developer time tends to be more expensive than storage,
so yeah.

I still remember when I thought it was in some way unreasonable that people
wanted to run the whole GNU toolchain on their target systems in the field.
I've long since gotten used to that. Of *course* people want self-hosting so
they can recompile hundreds of packages from source using autoconf
configuration.

For embedded systems.

-s
 
S

Stephen Sprunk

On 04/25/2013 07:05 PM, Stephen Sprunk wrote: ....

The entire southwest portion of the US used to be controlled by
Spanish speaking people, and Spanish remains pervasive throughout
that region. Many people for whom Spanish is their birth language
come from families that have been US citizens for 3 generations, or
longer - some can trace their presence in the region to back before
it was seized by the US. Spanish was my first foreign language, and
it remains by far the most useful of the five that I've learned, even
though I now live in the Maryland (which also, for different reasons,
has a significant Spanish-speaking population).

Still, most of them speak English as well, aside from the illegal
immigrants, despite potentially speaking Spanish at home.

As noted, making Spanish mandatory in the schools in the southwestern US
would be political suicide. We require _some_ foreign language to
graduate, and many students do choose Spanish, but most schools offer
other languages as well.
Many Québécois are much more cooperative with those who at least
attempt to speak with them in French. You can insist on using
English, but the more obstinate ones will respond by pretending not
to understand.

My experience in France is that they pretend not to know English, but if
you try to speak French, they will answer in English rather than allow
you to continue butchering their beautiful language. (I speak it well
enough to get pegged as French Canadian, but to the actual French that
still qualifies as butchering their language.)

This is notably unlike in most other countries, where people will
happily speak whatever common language can be found--and that is most
often English. Even if you do speak their language fluently, many still
prefer English just to get practice with a native speaker.
I can vouch personally for the fact that the English spoken by many
of those students is quite poor ... I wouldn't recommend counting
on being able to communicate with all of them using English.

Spoken English quality varies among non-native speakers, but most
problems can be attributed to starting too late in life, eg. college.
The current (and future) generations of Chinese students will be far
better by the time they reach the job market simply because they're
starting earlier.

S
 
S

Seebs

If you have such a system, then that would be true. However, I have a
hard time imagining what could go wrong within the body of main even if
the calling sequence depended on the return type. I could imagine how
things could fail on the return from main() if the return type did not
match the expected type, but not during execution of main() and its
infinite loop.
I think your argument here is perhaps technically true, but very much
hypothetical.

I don't recall details, but I believe there have been systems on which
arguments and return value both went on the stack, so if your function
didn't have the same expectations for its return value as the caller, it
could end up reading the wrong arguments. Not sure, though.

Mostly, though:

I am a big believer in the theory that if something is undefined behavior,
and I don't have a really *good* reason to believe that I absolutely need
to find a way to make it happen, the best answer is to say "that's undefined
and I don't know what will happen".

So the fact that it's undefined is enough to make me think it's unsafe,
because even if it might well work in some environments, it might not in
others. Someone will do something thoughtful like a compiler which mangles
names to denote return types, and the program won't link, or will get a
default main() implementation provided by something like lex/yacc, or
whatever other madness.
I made a specific point about /small/ embedded systems. I didn't define
"small embedded systems" here, but Linux (or other OS's capable of
calling independent programs) do not count as "small" - "small" system
programs are free-standing rather than hosted.

I would concede this, but I would also argue that even then, it doesn't
necessarily matter; there's nothing *preventing* a freestanding environment
from choosing to use a setup in which main() is called and expected to
yield a return value.
Typically in embedded systems there are at least two programs which are
not standard hosted environments with "int main(...)" - the kernel, and
also the bootloader (such as U-Boot). There may in fact be more levels
of bootloader, some of which may not have any main() at all.

This is a good point, I hadn't been thinking about the boot loader. And
arguably, whatever's picking up the boot loader is almost certainly something
that someone somewhere wrote as code...

-s
 
S

Seebs

The second languages are taught because they are useful to communicate with
those who don't speak our native language.

And also because thinking in different languages promotes mental flexibility.
In the last decade or so, english managed to become a de facto esperanto,
but that was mainly because the US dominated the world's economy and
academia. Things won't stay like that for long.

They might not, but the network effects may win anyway; at this point,
there's so much literature in key fields in English that you practically have
to learn it anyway. English is the new lingua franca! :)
Again, a couple of decades ago francophones were using the same arguments to
justify why it would be silly for them to learn a second language.

.... I made the joke above before I'd read that paragraph. But I'm leaving it
now.
Hardware production is already firmly established in Asia. I wouldn't bet
that in a decade or so english will still be the prefered language to
communicate with them.

Hardware production is moving around more thanks to automation, though.

In any event, I think I'd say this: I don't think I've ever regretted learning
something.

-s
 
S

Seebs

Telepathy is useful too, when you are trying to figure out what a
customer wants rather than what he says he wants...

Observations: Read Kahneman's _Thinking, Fast and Slow_. Read sites like
"Not Always Right" and "Clients From Hell". Study Miller's Law ("To
understand what another person is saying, you must assume that it is true
and try to imagine what it could be true of.") Do this stuff and you will
find that customer requirements are substantially more approachable.

Still impossible to be consistently right, but you can improve your odds.
Seriously, that's good advice. There is lots more to programming well
than just writing code - you have to know /what/ to code.

Other useful related skills are graphical design, economics (programmers
are notoriously bad at figuring out real-world costs), physics, maths,
more maths, applied maths, pure maths, and then some more maths.

Yes. Human interface considerations are something programmers in general are
HORRIBLE at. Norman's "The Design of Everyday Things" is another book I
strongly recommend.

-s
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top