# To C or not to C

1. Apr 23, 2010

### Kajahtava

Mod note:
This thread is an off topic splinter that has been brutally chopped out of the original thread. I am not sure if this thread has any merit but have perserved it for all the effort but into replys.
Integral

Depends on what you want to do.

Contrary to most people, I would not recommend C and C++, you need to understand how the machine and computers works for that (and quite frankly, I've a feeling from reading about how people approach C and C++ here, that they hardly understand C/C++ or its reason to choice it and just use it because they know no other languages)

C is mainly oriented at making software, if you want to program as in, automate tasks or getting a firm grip on structure of programming, it's certainly not the best place to start. C has many constraints which will seem absurd to you if you don't understand how a computer works on the inside, C programs also tend to be very unsafe unless you know what you're doing. All this comes at the benefit of excellent performance, this is why most low-level software such as operating systems and firmware is written in C, regardless that programmes written in C lack security by default.

For most people, Perl or Python is the optimal solution because things just 'work' there as you expect them to work with mathematical logic. Perl and Python though are candy in that they make things quite easy for you, they're also not very 'elegant' languages, they're 'more is more' languages, with various ad-hoc solutiosn to problems.

On the other end of the spectrum you have languages which are elegant and consistent, like Scheme (or Common Lisp?), Haskell and APL, all of which require some mathematical insight, but programming in them will in the end give you a great asset into structured and well-thought of programming. It teach you to think structured and efficient. APL is well known for its capability to place complex programs in one line, programming in APL generally means to first sketch the structure of your program and then implement it, Perl and Python generally you make it up as you go.

Lastly, you have languages such as Smalltalk, Ruby and Logo, these are very 'conceptual', they don't as much deal in abstract mathematical structures but more in human concepts while steal retaining their elegant and well-structured nature.

Also, K&R is archaic and teaches a standard of C that is pretty much recommended against using nowadays, not in the least because it was never really standardized. Also, C is particularly bad for root finding functions and ends up becoming verbose for that, that's not what C was ever intended for, C was intended to write software, system software and application. C is not a tool to help you solve problems, it's a remote control for your CPU because reaching to it and using assembly becomes tiring.

Last edited by a moderator: Apr 25, 2010
2. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

First edition is, not second. Also, it's a language definition of a very old and never very standardized dialect of C.

I'm still to contend that C is inhaerently a bad choice to begin to learn programming, especially for people still in high school.

C has a lot of limitations which make sense once you understand how a computer works on the inside, but for the most part they will seem absurd on the perspective of the language itself. Also, many people that program in C only do so because they've never learnt another language, and program in C badly and program in a style what other languages are much better suited for.

Unless you plan on coding an implementation for another language, an operating system, or a hardware driver, really, there is no reason to program in C. C isn't a programming language in the traditional sense, it's a way to get your computer to do certain things on the machine level.

I mean, some examples of copying a string in different languages:

Perl or PHP
Code (Text):

$a = "string";$b = $a; # a is copied to b, that was easy. APL: Code (Text): a <- 'string' b <- a Scheme Code (Text): (define a #f) (define b #f) ;we first have to define them, it already gets more complex. (set! a "string") (set! b a) ; but still, fairly straightforward (Copying strings is by the way not really a thing you'd tend to do in Scheme, you often don't need to change the value of variables at any point, hence it's a bit more verbose) And finally, C (do not try this at home) Code (Text): char *a = malloc(256); //reserve 256 byes of memory, convert the void pointer to a character pointer and store the value that points to the first byte of memory into the a variable. a[0] = 's'; a[1] = 't'; a[2] = 'r'; a[3] = 'i'; a[4] = 'n'; a[5] = 'g'; //Yaaaaaap, this is how it goes. char *b = malloc(256); //reserve 256 bytes of memory again. while(*b++ = *a++); //why can't we just use a simple assignment? why this cryptic construct? Why not just b = a? Is there any reason for this? Sure there is, that's because strings don't really exist in C, you simulate their behaviour yourself, you also can't directly pass them to functions. print(*b); // prints "string", voilla. Now, OP, this is why I would not recommend C. That it works like this has nothing to do with 'candy' or 'making it easy', and other languages aren't 'impure' because it's a lot simpler there. C was never designed to make 'programs' as in, algorithms that solve problems for you in, C was designed to control your processor at the low level in a portable way with the benefit of structured programming instead of the spaghetti hassle of assembler. There were plenty of languages before C that allowed easy string copying and it hadn't a thing to do with the technology of the time. Also, C will not at all have more performance unless you understand the inside of a computer. C is probably the most abused language on the face of the planet today, too many people use it for things it was never designed to do for. It wasn't even designed to print 'Hello, World!' on a terminal, it was designed to make the terminal 'Hello, World!' is printed on. And though languages like Perl may copy the syntax of C, they have little to do with C and it's a common error to approach the one as the other. 3. Apr 24, 2010 ### PaulS1950 Re: How do I start to learn programming? malloc is not even used much anymore and there is a call "strgcpy" that copies a string from one to another. there is no need to allocate memory because the memory is either in the strings definition or auto-allocated when the string is copied as defined. I learned C back just after ANSI C was formed and ANSI C is still used more often to write software than any other language. C++ has finally reached the point where it has an ANSI form too. I began writing programs in 1978 when I taught myself Basic and did everything "wrong" to save space in my code. Then I moved to GWbasic, Qbasic, Acouple of structured languages and finally C, C++. You can do anything with C and C++ that you can with any language (including cross platform programming in the ANSI versions) but it requires you to be meticulous in planning, writing and execution of your software. You can include ASM code into the program and force fast operation. It was written to handle complex code and that means you can write for games, math, engineering and communication and about anything else you want to do with it. I typically write stand-alone programs as a hobby and have had much better software since I switched to C and C++. It is one of the most powerful programming languages in use - and it is the most popular. That is likely why it is also the "most abused" - how ever you define or justify the comment. 4. Apr 24, 2010 ### Kajahtava Re: How do I start to learn programming? At compile time, not at runtime. You need malloc and realloc for dynamic allocation. Also, strcopy looks like that on the inside. It would be even more cryptic if I used strcopy. Of course, it's Turing Complete. That still says nothing about how convenient it works and above all how C is not the best language for people to start with. ASM again is not the place to start for people that want to learn how to program. What is 'complex code'? The C example sure looks quite complex yes. You can also do that in Conway's game of Life. What other programming languages or paradigms have you used? No, it's one of the least powerful programming languages in use, which is a conscious decision to gain performance at the expense of expressiveness. Python is probably the most powerful programming language in use (also one I don't like because of its ad-hoc nature), but it can do about anything, it has a lot of things C/++ doesn't support like first-class functions, duck typing, first-class strings, first-class arrays, currying, dynamic class generation, closures, C/++ are extremely minimal and not at all powerful, I mean, do you know any other language in use today that does not support first-class strings or arrays? I think Visual Basic.NET is more popular, as are PHP and JavaScript, all also very much abused. But really, I like to see some backing of C being 'powerful', because a conscious design effort was to remove some features which were already standard at that time to better facilitate low-level functions. Not in the least no longer having arrays/strings as a proper data-type but using an ad-hoc solution with pointers in their place. Bounds checking is expensive you know... 5. Apr 24, 2010 ### jtbell ### Staff: Mentor Re: How do I start to learn programming? C++: Code (Text): string a, b; a = "I am a string."; b = a; In C++, there's also no need to muck around with pointers when using arrays, if you use vectors instead: Code (Text): vector<string> names(3); names[0] = "Larry"; names[1] = "Curly"; names[2] = "Moe"; for (int k = 0; k < names.size(); ++k) { cout << names[k] << " "; } cout << endl; 6. Apr 24, 2010 ### Kajahtava Re: How do I start to learn programming? Either C++ has significantly, and significantly changed since I last used it, not to mention breaking backwards compatibility. Or this isn't copying, this is assigning. What happens if you do Code (Text): b[0] = 'i'; Does a also change to "i am a string" with it? Furthermore, what is the memory allocation to a and b, is it both exactly 15 bytes? What if I later on want to concatenate to one of them? The main reason you copy a string into a new memory space is that you want to change it. The same in PHP: Code (Text):$names = array("Larry",  "Curley",  "Moe");
Foreach $names as$name {echo \$name . " ";} echo "\n";
Significantly less verbose, isn't it?

Code (Text):
(define names (list "Larry" "Curley" "Moe"))
(display-all (append (map append-space names) "\n"))
Now, I feel an arrogant ***** for saying this, but after reading this topic and the defences of C here for jobs it was never meant to do, I seriously doubt the understanding a lot of people here have of C/++ and above all what makes it good. So I like to ask you people a view quaestions (consider me erroneous if you can answer them)

A: What is the reason all members if an array have to have the same type.
B: Why do pointers have types instead of just being integers?
C: What is 'turing completeness' (Yeah, one comment made me sceptical')
D: Explain in your own words 'static typing'. (Protip: In static typing, variables have types, and not their values doesn't work.)

And last but not least:

E: in your own opinion, justify the use of C/++ for a beginner to learn programming. When there are languages out there such as Logo or Scheme which were designed as teaching languages, as in, designed to let beginners get a firm grasp of well-structured programming from the beginning while C keeps you so much occupied with memory management that the overall structure of the program goes lost.

C/++ really enforces a bad style of programming with various ad-hoc and unsafe solutions to problems simply get get that performance. Which really is not necessary for the most part.

The Scheme code above there is an example of well-structured programming. Of course 'names' is a variable, in Scheme a symbol, that points to the list of names, append-space is obviously a function such that "string" -> "string ", we can use (map append-space '("Larry" "Curley" "Moe")) to refer to the list '("Larry " "Curley " "Moe "), we then use append, to gain the list '("Larry" "Curley" "Moe" "\n") and finally run display-all on that list, which does what you expect.

This is well-structured because of the rule of freest context, each part of the program isolated on its own has a well-defined meaning, it's if you know what 'name' refers, the context of the parts of the program are not relevant to determine what they refer to. Also, scheme is an expression-oriented language, all things are expressions. Which facilitates teaching the mind thinking in well-structured and maintainable code. In C/++, this is not the case and the context is needed to understand the meaning of isolated parts of the program, it's meaning is not lexically inferable.

They do as autodidacts simply because all people tell them to, I made the same mistake, I later on realized that this was not the way to go. HTML is also not a turing complete language.

A very good piece of literature to learn programming is Structure and Interpretation of Computer Programs (SICP), which uses Scheme as an example language because Scheme is a highly structured language. But A: C is a bad place to start. B: the majority of people that started with C still don't understand C and use it in a way that does not benefits from C's strength, it's low level yet portable nature, and instead program in a style in it that you think 'Get a dynamically typed string-oriented language for this...'

Last edited: Apr 24, 2010
7. Apr 24, 2010

### rcgldr

Re: How do I start to learn programming?

It's defined a assigning a copy of the source string. You could have d = a + b + c, to assign a copy of the concatenation of 3 strings.

C or C++ get's around this limitation by using structures or classes. APL requires all members be of the same type, APL2 allows each member to have it's own type.

Depending on the machine, they may be different sizes (for 68000, integers are 16 bit, pointers are 32 bits).

If you keep the initial programs used to learn very simple, such as only using numbers and not strings, then the required sub-set of most languages is simple to learn.

I don't think a student's initial programming experience is going to be concerened with performance. The classic recursion method for calculating factorial isn't efficient, but it can teach recursion.

I don't think this is a necessary goal for a new student. Just learning the basics of programming is enough. They need to understand how to program before they start to worry about structured or maintainable code, which are somewhat subjective terms.

Considering most initial programs will be 10 to 30 lines long, I don't think this is an issue.

Perhaps the initial experience should be some interactive language, such as basic or APL, but after a few programs, the student could move onto another language like C, simply based on it's popularity for software development.

I still like this programming teaching tool / puzzle that can be introduced early into the learning experience:

https://www.physicsforums.com/showpost.php?p=1347155&postcount=1

8. Apr 24, 2010

### D H

Staff Emeritus
Re: How do I start to learn programming?

I disagree with that. I work with a lot of people who know how to program in C or C++ but haven't the foggiest idea how computers work.
You must be joking. The only reason for mentioning APL is as an example of one of the many mistakes made in the search for a good programming language/paradigm.

Oh please. That is utter nonsense and you know it.

Code (Text):
char * a = strdup ("string");

Oh please again. The original poster is a *high school student*.

GreatEscapist, I strongly suggest you take everything said in this thread by Kajahtava with a huge grain of salt. There are lots of proselytizers for/against various programming languages, and Kajahtava appears to be one of them.

GreatEscapist, to learn to program on your own you I suggest you start with a simple language. Java and Python are simple and you can get free interpreters for both. When you get to college, take a class in computer programming, even if it turns out that computer programming is not your chosen career path. For one thing, in learning to program on your own you will learn some bad habits; you will need to unlearn them. For another, while you might learn to write programs on your own, you are less likely to learn the reasons why those programming constructs work. You won't learn to see the big picture.

Last edited: Apr 24, 2010
9. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

Why doesn't it assign a *char pointer?

That's not an answer to my quaestion, it's not a limitation, it's a conscious effort.

Well, if that were true we could just declare long and short pointers.

Tip: array[index] is syntactic sugar for *(array+index).

This is most true, however after a couple of days in Perl or Python you're already performing regular expression matches against being frustrated in C why if("string" == "string") doesn't quite work.

Which can also be done in any other language?

My point was that the only benefit of C is performance at the cost of structured programming, since performance is not an issue, better take a more structured language.

Well, I disagree, as Dijkstra said, if you start on the wrong foot you will have artefacts in your style for the rest of your life.

In fact, I daresay the essence of learning to program well is first learning how to design the structure of a program, and only then translating it to a given language. There's portable code, and there are portable structures of programs.

It does condition the mind in the end to see programs more structured.

I think BASIC is a bad teaching language, I'm still to say that Perl or Python are for most people the best way to start, they have a REPL which enables one to get the feel of the language quickly but they aren't 'too structured' for beginners like Scheme and APL.

On the other hand, Logo and Smalltalk are also nice to begin because they're conceptual yet structured.

The only reason one has to use C is performance, and to get that performance one has to understand how the machine works. If not, often Python code in fact has more performance.

I see that a lot too, that's why their C code is bad and what they do would be more efficient, not to mention a lot easier in a language like Perl.

For people that don't understand it maybe. That APL still has a strong userbase that defend it and call it brilliant and that all people who've sat through the learning curve and become proficient in it adhaerant it.

This does not copy a string.

Why do we want to copy a string? Obviously because we want to change it while retaining the original. So, we want the memory allocation of the new one to be larger than the data itself so that we can concatenate to it.

It does not assume a lot of prior knowledge.

I'm not against C, I'm against using C for just about anything because one knows no other languages.

So strangely, in the end, we agree but for different reasons? I called Python or Perl for most people the best place to start.

I wouldn't call APL a good place to start by the way for most people, but it is a language that will condition your mind to program in a highly structured and well-thought-of manner.

10. Apr 24, 2010

### D H

Staff Emeritus
Re: How do I start to learn programming?

strdup does make copy a string. RTFM.

Your straw man portrayal of string duplication in C belies your anti-C mindset. Use of fallacies is against the rules here. Please follow them.

Pperl is anything but a good language for beginners. I like perl (I like it a lot; other languages have regular expressions, but none come close to the versatility of perl's). However I would never recommend that as a choice for someone new to programming learn it. While perl is powerful, it is an ugly, ugly language.

As is APL. APL is one of the ugliest languages I have learned. It was a mistake. The APL fan base is small, probably smaller than the Commodore 64 fan base (who are also quite religious in their fanaticism).

11. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

Yes, strdup... string duplicate... makes a copy. But not like this:

Code (Text):
char *a = strdup("string")
Code (Text):
char a[] = "string";
char *b = strdup(*a);
Is copying a string.

It's not a straw man, it's highlighting a point, you may use 'strdup' yes, but how many memory have you allocated to hold the new string then? All things you have to consider in C depending on what you want to do with that string? I specifically used malloc and commented it to show that I allocated 256 bytes to hold six characters, depending on what I want to do with that string it's a reasonable option.

After that still comes freeing the memory once you're done.

And you've still ignored the point I raised four times already. Why do you want to copy a string, in C, what you plan on doing with it eventually determines how you're going to copy it. If you just plan on changing one character you can use strdup. But what if you want to copy a string to serve as the start of a very much larger string? Then you need to allocate more memory and you're stuck with malloc again.

It is ugly, but that's not very relevant for beginners. And I don't like Perl by the way to work in myself.

APL is one of the most beautiful and and consistent languages out there. APL was really 'thought of', it all follows from each other. It's also hard to understand.

12. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

I think this should go in the reverse, first learn how a computer works, then learn C. Not learn how a computer works for C.

This is absolutely a reason to use C. However, this is not 'learning how to program.'

Also, FORTRAN is also a candidate here.

13. Apr 24, 2010

### D H

Staff Emeritus
Re: How do I start to learn programming?

What is your hangup against C, Kajahtava?

To GreatEscapist, Yet another reason to learn C is that this is where the market is. Java, C++, C#, and Objective-C all have their roots in C. To do a good job in one of those languages that derive from C it is helpful to learn C. Those five languages (C, Java, C++, C#, and Objective-C) are *the* market, and will be for a long time. Except for Python, the languages that Kajahtava has recommended (Lisp/Scheme, APL, Haskell, Smalltalk, Ruby and Logo) are niche languages that have very limited/non-existent market presence . Some language proselytizers hate C, not so much because it is a bad language but because the dominance of C means their pet languages can't compete.

14. Apr 24, 2010

### shoehorn

Re: How do I start to learn programming?

Eh?

Ah, so the plural of anecdote is data...

Even by the standards of internet message boards, this is a bizarre thing to say.

Although nobody who's actually familiar with using C to get work done would ever need this explained to them, this is the reason one uses libraries. Whether these libraries come bundled as part of the standard or not is irrelevant: essentially all languages are "difficult" to use without employing libraries that extend the functionality of the language beyond the core standard. Try using Python without any import statements, C++ without any includes, or C# without any using directives.

Currying, first-class functions, duck typing and so forth can all be implemented in C++ using Boost, something any serious C++ programmer will be familiar with.

15. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

Nothing, it's just a language intended for high performance computing by hardware specialists that already had a firm understanding of how a computer works, not as a language to start learning how to program and design programs in.

As I said in my first post, 'it all depends on what you want to do', I divided languages along the lines of what they were suited for. C is not a teaching language, that's all.

Sure, C has the market, but the OP wanted to learn how to program. C is a language mainly made to design applications and software in, so obviously it has the market. I mean, it's good for what it was designed for, very well designed language for that, but it was not designed as a teaching language and consequently is a bad language to get a feeling for algorithms in.

- Lisp/Scheme, APL will give you a firm understanding of the structure of algorithms, consequently, they are used to write programs that find solutions to problems you might have, that automate tasks.
- Haskell I wouldn't try unless you happen to already know things about lambda calculus and combinatory logic and the term 'Howard-Curry correspondence' has meaning to you. Haskell is used to write applications that depend on complex principles, like writing a physics engine, Haskell is your friend then.
- Smalltalk, Ruby and Logo are great languages to start with, they're 'conceptual' as in, they deal with human concepts instead of abstract and mathematical concepts, but still retain a well-structured data. They can easily be used as scripting languages.
- Perl, PHP, Python are effectively a sort of C that doesn't sacrifice accessibility for performance. Though Perl and PHP aren't as well-structured as Python. But they can all be used to manipulate text files very well.
- C/C++ Java are designed to make consumer applications for the most part, they aren't made to express algoritms.

Hmm, I'm not sure but I believe that supercomputer benchmarks are still written in FORTRAN.

It also depends on what you want to calculate, if you don't really have to solve a problem, but just have to process a lot of numbers, then C and Fortran are the way to go, if you have to write a program that for instance will calculate for you how to optimally place some objects in a box, then Haskell or some form of Lisp shall be your friend.

16. Apr 24, 2010

### shoehorn

Re: How do I start to learn programming?

Fortran is many things, but obsolete it ain't. Show me a pair of programmers, one of whom is an expert in Lisp/Scheme/Ada/etc and the other of whom is an expert in Fortran, and ask the job market who is in more demand.

Hint: it's the Fortran expert, by a mile.

Regardless, this thread has surely strayed far enough from the OP's question. The pseudo-intellectual masturbation about why C isn't as "good" a language as someone's pet niche language is as tiresome now as it was twenty years ago.

17. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

Not at all, C was radically different when it was introduced. It was machine-oriented and not assembly. In the days C was introduced, almost all languages were already no longer machine-oriented but rather a way to express algorithms. C isn't, it's a way to control a processor.

Sure, but it's also a reason why C is not a good place to start when you want to learn how to program.

If you want to give people an appreciation of control structures, surely you want them to just be able to simply use
Code (Text):
if(a == b) ... else ...
instead of hassling with a string compare library?

I'm quite sure we have a different definition of first-class function, duck typing and so forth.

This is actually a pretty common debate, with some C++ programmers advocating that they are first-class and higher-order programmers countering that those people missed the entire point of first-class functions and that it's more than just anonymous functions.

http://lambda-the-ultimate.org/node/1313

18. Apr 24, 2010

### D H

Staff Emeritus
Re: How do I start to learn programming?

That many introductory computer science classes and many classes beyond are C-based falsifies this conjecture.

Can we get off the anti-C garbage? It isn't helping the OP one iota. Fine, you have a bias (an irrational bias) against it. Let it be.

19. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

I like to see some backing of this claim. I have never seen a programming class using C as an example language.

Like I said, I'm not anti C, that's what you make of it. I'm anti C as a teaching language opposed to a language to write (not design) software in.

You also have a tendency to cherry pick the points you go into. And not go into some points which I explicitly marked as relevant. (Why do we copy a string?)

20. Apr 24, 2010

### Kajahtava

Re: How do I start to learn programming?

http://en.wikipedia.org/wiki/How_to_Design_Programs

Anyhow, this also seems like a nice piece of literature. It seems to teach programming in a well-structured way, first asking yourself what the input is, then how the output must depend on the input, and then writing the translation scheme that is your program.