Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

From what I have read, K&R seems to be a good place to start learning programming in general, and C programming specifically. However, I've just started the first chapter and I have a few questions. They may be extremely simple, and I apologize if that's the case (I am new, after all) -- but your helping me answer them would be very appreciated.

1) I understand the book is written to describe C languages in general. Does this mean the examples they give are just rough outlines, and that for me to attempt them myself I have to "translate" the general outline given into C++ or C# or whatever?

For example, the textbook gives this program:

#include <stdio.h>
 main()
 {
 printf("hello, world\n");
 }

But I've found a program to print "Hello, world" in C#, for example is:

// A Hello World! program in C#. 
using System;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

And the program in C++ looks different from both the above...

So my question is: does the book expect me to be able to translate the first program (or outline, whatever it is) to a C# or C++ program -- I am using Visual Studio -- to be able to practice myself?

2) If the book does expect me to do this, do you all have any tips on how I can go about translating general C code to C# or C++.

3) This may be opinion, but which should I base my learning on (and practice coding in) -- C# or C++?

share|improve this question
10  
I have no idea why learning C have anything to do with C++ and C#... one at a time please. Sometimes it is not possible to directly translate something in one language to another language (or it will be very very bad to do so) –  Bryan Chen Jun 30 at 22:29
12  
All these other languages came after C. K&R do not "expect" you to do use anything else - their book is for learning C. –  andy256 Jun 30 at 22:34
19  
C is not C++ is not C#. C# is actually much closer to Java than either C or C++. Trying to learn C# from K&R would be analogous to trying to learn modern Italian from a textbook on Latin. –  Joe Ballard Jul 1 at 0:10
6  
Your confusion seem to be caused by the similar names. C, C++ and C# are different languages, with C# having nothing in common with C. There is no such thing like "C languages". And Visual Studio is a development environment, which is a fancy name for a complex code editor. It is used to write programs in many unrelated languages - but many people associate it with C#. –  vasile Jul 1 at 9:49
2  
I would definitely not recommend learning C++ and C# from K&R, furthermore, I would advise against most people learning C as a first programming language. C is quite a hard language - it is much closer to the machine than most modern languages. Instead, I'd recommend learning something like Haskell if you have a strong maths background, and something like Python otherwise. I'd only recommend C if you are an electrical engineer or something. Programming skills are easy to transfer between langauges - I think it would be better to learn to program in an easy language first. –  sweeneyrod Jul 1 at 15:17

4 Answers 4

I understand the book is written to describe C languages in general.

The book is written to teach you ANSI C, not 'C languages in general'

Does the book expect me to be able to translate the first program ... to a C# or C++ program?

No, those are different languages, and the book does not expect you to do anything with those languages. The examples are ANSI C examples.

Do you all have any tips on how I can go about ...

Which should I base my learning on (and practice coding in) -- C# or C++?

Those questions, while perfectly fine, are off-topic for this site.

Suffice it to say, if you want to learn C# or C++, you should seek out resources for those specific languages.

share|improve this answer

Firstly, let me agree that K&R is a great place to start with the C family. It is a really wonderfully written book. Importantly, it is quite concise.

Be aware that C++ is much more closely related to C that C#, although C# does borrow heavily from both. Java in fact does the same, and indeed C# followed from Java.

You can't learn C# or C++ just using K&R - you need resources for each of those.

You seem to be proposing learning all three of C, C++ and C# simultaneously. Whether this is a good thing depends on you. It will give you a great opportunity to compare and contrast the languages without having built up a prejudice from whichever you learn first.

However, this may prove very challenging. Ask yourself, if you wanted to learn Spanish, Portuguese and Italian, would you learn them at the same time or separately?

share|improve this answer
    
+1, especially for the natural language simile - it's quite applicable. –  Angew Jul 1 at 9:08

The K&R book is a great place to start with any of the C variants.

The short answer is that you can run C code through a C++ compiler and it will run.

Some things you need to know:

  • When C++ was introduced, it updated C's capabilities by (at a minimum) adding object oriented coding to C. (UPDATE) As many of you have noted, the languages are not the same, continuing to grow further apart with time. (/UPDATE). According to the wikipedia article, Compatibility of C and C++, there are semantic differences:

The C and C++ programming languages are closely related. C++ grew out of C, as it was designed to be source-and-link compatible with C.1 Due to this, development tools for the two languages (such as IDEs and compilers) are often integrated into a single product, with the programmer able to specify C or C++ as their source language. However, due to minor semantic differences, most non-trivial C programs will not compile as C++ code without modification — C++ is not a superset of C. Emphasis added.

Both C and C++ give you a lower level of abstraction that, with increased complexity, provides a breadth of access to underlying machine functionality that are not necessarily exposed with other languages. C++ adds the convenience (reduced development time) of a fully object oriented language which can, potentially, add an additional performance cost.

C# provides a managed memory model that adds a higher level of abstraction again. This level of abstraction adds convenience and improves development times, but complicates access to lower level APIs and makes specialized performance requirements problematic.

share|improve this answer
10  
I don't agree that C++ extends C because the two languages have diverged by miles C++ is really a replacement to C additionally VSC++ is also managed –  Ramhound Jul 1 at 2:20
2  
C has things which C++ does not (restrict, type-generics, implicit conversion from void*, for example). A C program is not necessarily a valid C++ program even if it doesn't use any C++-only keywords. –  Angew Jul 1 at 9:07
    
(I have no enough rep on this site to do -1, but consider this a downvote) "C++ extends C by (at a minimum) adding object oriented coding to C" If you think that you doesn't know anything neither of C and C++ nowadays: C With Clases was the "beta" of C++ and dates from the middle of the eighties, and C have evolved a lot since K&R (In fact as others noticed C and C++ have diverged). I understand that people still rely on classic books because many people used them at the time they are learning and that books where awesome, but we should update our bibliographies. In the case of ... –  Manu343726 Jul 1 at 11:36
    
... C++ there are many efforts to update the books, and in fact I will never recommend anybody to read TC++PL 3th ed having a version updated for C++11. The same applies (Or should apply) for C. Stop using K&R for learning, there is C99 and C11, ANSI C is just very old –  Manu343726 Jul 1 at 11:39
1  
I do not see that C# has C roots. It borrows some syntax, that's it. C# has Java roots. I think it's widely accepted that the name was chosen for marketing reasons. That's in fact someone should always think about: C# - in contrast to C, C++, Java - is proprietary, only useful to learn if you are hooked to this kind of platform. –  Zane Jul 1 at 18:47

From a c# perspective K&R might be a great book on c ... i recently read it in an attempt to brush up on c. I have to say the code in it makes my eyes bleed (when having a c# hat on).

In c# the current style is to make everything clear and not ram 4+ statements into one.

If you want to learn c# this is not the place to start. I cannot recommend a book however. But c, c++ and c# are completely separate languages.

c is amazing in its simplicity and elegance. Its a bit stone age in its processes but going back to its simplicity was something i found rewarding. And highly inspiring because entire operating systems are built on this without all the stuff that is essential to c#

c# is enterprisey and is very good at it.

c++ has evolved so much over the years, learning it from a c perspective is a bad idea as so much has been learnt and rebuilt over time. you want to start from a very modern approach to avoid the horrible bits imo.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.