Jump to content



Photo

Role of "C" programming language for a beginner?

role of c why c important for beginner why c why i should learn c before? why c prefered initially? where we are using c? advantages if i prefered c

  • Please log in to reply
57 replies to this topic

#31 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 14 May 2014 - 00:51

Yes, that's quite probably the worst advice possible. Learning basic, especially as a first language, will teach all kinds of bad habits.

Such as?




#32 Max Norris

Max Norris

    Neowinian Senior

  • Tech Issues Solved: 22
  • Joined: 20-February 11
  • OS: Windows 2012R2, 7, BSD Unix, Arch Linux
  • Phone: HTC One (Work) Lumia 1020 (Home)

Posted 14 May 2014 - 00:53

Yes, that's quite probably the worst advice possible. Learning basic, especially as a first language, will teach all kinds of bad habits. It's also so different to C that it'll just end up confusing a beginner and putting them off altogether.

Oh gawd I can attest to that. First one I ever picked up was some variant of BASIC back in the 70's.. transitioning to anything else was jarring to say the least, next one was C under Microsoft Xenix and Modula-2 just because and went from there, BASIC did me no favors. There's a lot of similarities across many of the languages.. except BASIC. That's way out in left field all by itself. (This is ye olde BASIC mind you.. VB.NET is nowhere near as bad as "classic", they cleaned up the language quite a bit, different animal entirely.. not that it's my favorite mind you as it's not.)

#33 simplezz

simplezz

    Neowinian Senior

  • Tech Issues Solved: 8
  • Joined: 01-February 12

Posted 14 May 2014 - 01:42

that many universities actually use languages other than C to teach data structures and algorithms (mine used Java)

We're not talking about a CS course where writing such things is mandatory. There's little motivation for a beginner learning a higher level language to rewrite data structures and algorithms. They probably won't even know what they are, they'll just be calling built-in functions. So they're missing out on learning the fundamentals of software programming.

and that C is not particularly fit for this task, with no list comprehensions, iterators, higher-order functions or explicit tail recursion.

None of which is necessary and yet more complexity to burden the beginner.

The set of features supported by C closely follows hardware-level abstractions

You keep repeating that. Which set of features?

The fact that most languages have richer standard libraries than C

That matters little when third party libraries exist.

If anything, a quality standard library gives a good example to follow when implementing your own.

I guess you haven't seen an STL implementation then ;)

I have implicitely advised against learning something like C# (or Java or C++); I have advised using a simple language that has a low barrier of entry and REPL support. Python is a popular choice; the MIT has used Scheme for a long time as I referred to.

Answer me a question. Which is the simplest language, C or Python? Also take into account Python's rather unique white space indentation.

#34 astropheed

astropheed

    astropheed

  • Tech Issues Solved: 2
  • Joined: 08-December 11
  • Location: Sydney, AU

Posted 14 May 2014 - 01:49

Answer me a question. Which is the simplest language, C or Python? Also take into account Python's rather unique white space indentation.

 

Python.



#35 simplezz

simplezz

    Neowinian Senior

  • Tech Issues Solved: 8
  • Joined: 01-February 12

Posted 14 May 2014 - 02:04

Such as?

I don't know about newer incarnations (vb.net etc), but in the past, things like lack of modular programming enforcement. No static typing and so forth. Goto abuse. I've seen some horrific VB code. It's very forgiving, too forgiving, and that can lead to bad habits and sloppy code.

#36 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 14 May 2014 - 02:19

We're not talking about a CS course where writing such things is mandatory. There's little motivation for a beginner learning a higher level language to rewrite data structures and algorithms. They probably won't even know what they are, they'll just be calling built-in functions. So they're missing out on learning the fundamentals of software programming.

If their goal is to learn the fundamentals of CS, then they can do that in any language. If their goal isn't to learn the fundamentals of CS, then why would they choose C?
 

None of which is necessary and yet more complexity to burden the beginner.

... no more necessary than explicit memory allocation, header files, linker errors, pointer arithmetic and having to compile the entire program every time (lack of REPL). The difference is that one type of complexity is mostly accidental and useless to CS purposes, the other introduces generally applicable concepts and encourages to think in a functional way. By the way, tail recursion is necessary to write recursive algorithms that cannot blow the stack. C explicitely discourages this.
 

You keep repeating that. Which set of features?

The set of features supported by the C language: its data types, evaluation model, control structures.
 

Answer me a question. Which is the simplest language, C or Python? Also take into account Python's rather unique white space indentation.

Python has more features than C, but these features can be learnt more incrementally, meaning that Python effectively appears simpler. Also, the presence of an REPL greatly encourages exploratory programming and learning through trial-and-error. For example, the canonical "Hello World" program in Python can be achieved by opening the REPL and typing
 
print "Hello World"
This code contains nothing more than a function name and its argument. The student gets immediate feedback on what he has written including underlining any mistyped character.

Whereas the equivalent task in C requires:
1) Creating and saving a text file somewhere
2) Writing the following code: 
 
#include<stdio.h>
int main(){ 
    printf("Hello World");
}
This code contains mostly noise and will be very mysterious to a neophyte.

3) Opening a command prompt
4) Invoking the C compiler on the text file with something like
 
g++ hello.cc -o hello
This reports errors by providing a line number and column, which the student then has to look up in his text file.

5) Running the executable thus produced

Which is simpler?

#37 simplezz

simplezz

    Neowinian Senior

  • Tech Issues Solved: 8
  • Joined: 01-February 12

Posted 14 May 2014 - 03:17

... no more necessary than explicit memory allocation

FILE *f = malloc ( sizeof ( FILE ) ); 
vs
MyClass mc = new MyClass ( ... );
I don't see a lot of difference.
 

header files

Namespaces, imports.
 

linker errors. pointer arithmetic

Which errors? Missing library errors? You can get them in other languages too. Pointer arithmetic is quite simple once you get the hang of it. You have references etc in other languages as well.
 

and having to compile the entire program every time (lack of REPL).

I guess you've never used a Makefile, GNU autotools, or Maven before then?
 

By the way, tail recursion is necessary to write recursive algorithms that cannot blow the stack. C explicitely discourages this.

Most of the time recursion is a poor solution anyway. Iteration is often superior and makes for cleaner and more predictable code.
 

The set of features supported by the C language: its data types

typedef struct {
  
  int   a;
  char  b;
  float c;

} Object;
vs
public class Object {

  int   a;
  byte  b;
  float c;

};
A lot of difference there..
 

evaluation model, control structures.

Such as?
 

Python has more features than C, but these features can be learnt more incrementally

Really? So you don't have to learn procedural and OOP at the same time and all that entails? I could say the same about C. You can learn it incrementally as well ;)
 

meaning that Python effectively appears simpler. For example, the canonical "Hello World" program in Python can be achieved by opening the REPL and typing

print "Hello World"
This code contains nothing more than a function name and its argument. The student gets immediate feedback on what he has written including underlining any mistyped character.

Whereas the equivalent task in C requires:
1) Creating and saving a text file somewhere
2) Writing the following code:

Oh Really?
 
 
$ c-repl
> puts ( "Hello World!" );
Hello World!
or:
$ gdb
(gdb) printf "hello world\n"
$1 = "hello world"

3) Opening a command prompt
4) Invoking the C compiler on the text file with something like
 

g++ hello.cc -o hello
This reports errors by providing a line number and column, which the student then has to look up in his text file.
Which is simpler?

Acually it's gcc. g++ is for C++. But anyway, they should be using automake, then it's only one command - make, which can be done inside of Vim without leaving the editor.

I was talking about the language itself. C is clearly simpler in terms of keywords, concepts, paradigms, syntax, library, etc. Regardless of whether you learn it incrementally, there's still much more to learn and it's easier to get overwhelmed.

#38 astropheed

astropheed

    astropheed

  • Tech Issues Solved: 2
  • Joined: 08-December 11
  • Location: Sydney, AU

Posted 14 May 2014 - 04:01

Regardless of whether you learn it incrementally, there's still much more to learn and it's easier to get overwhelmed.

 

What did you learn first?



#39 Torolol

Torolol

  • Joined: 24-November 12

Posted 14 May 2014 - 04:27

use "GOTO" the unconditional branch, those who get used to it are better at creating program/drivers with low level language as Assembly for ARM or x86.
but again the last fruitful usages of assembly was on Gameboy Advance games.

#40 seta-san

seta-san

    Neowinian Senior

  • Joined: 17-February 05

Posted 14 May 2014 - 05:07

gets you used to 'c' syntax which is widely used in other languages such as 'c++' java, javascript, c#, etc..

gets you used to functional programming before expecting you to get familiar with more abstract ideas like objected oriented programming or some other abstract ideas that you find in c++..

it'll force you to really learn about pointers, referencing, dereferencing, etc. c++ and other languages in a lot of ways helps you avoid the headache but in that same breathe sort of denies you the knowlege and experience.

getting into more advanced things it'll teach you about the stack and the heap and memory management



#41 vhane

vhane

    Neowinian

  • Tech Issues Solved: 1
  • Joined: 15-August 04

Posted 14 May 2014 - 12:41

gets you used to functional programming before expecting you to get familiar with more abstract ideas like objected oriented programming or some other abstract ideas that you find in c++..

 

I think you mean procedural/imperative programming.



#42 simplezz

simplezz

    Neowinian Senior

  • Tech Issues Solved: 8
  • Joined: 01-February 12

Posted 14 May 2014 - 14:40

What did you learn first?

Pascal and Smalltalk. Admittedly not the best of starting languages, but it was a uni course so.. If only I could go back and talk some sense into myself :D

#43 Max Norris

Max Norris

    Neowinian Senior

  • Tech Issues Solved: 22
  • Joined: 20-February 11
  • OS: Windows 2012R2, 7, BSD Unix, Arch Linux
  • Phone: HTC One (Work) Lumia 1020 (Home)

Posted 14 May 2014 - 14:54

Pascal and Smalltalk. Admittedly not the best of starting languages, but it was a uni course so..

Pascal was fun, used to use Turbo Pascal back in the DOS days for a few years, FPC later off and on out of nostalgia.. think my old Xenix setup had it too but never used it there. Kind of a messy language though, and God help anyone who gets chained to Embarcadero's version of it. Missed out on Smalltalk though, although I know it heavily influenced languages that came later.

#44 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 14 May 2014 - 15:55

FILE *f = malloc ( sizeof ( FILE ) ); 
vs
MyClass mc = new MyClass ( ... );
I don't see a lot of difference.

I said that C relies on explicit memory management and that higher-level language do not. I don't know what you're trying to accomplish by showing two statements that don't do that same thing at all, i.e. one reserves a portion of memory and returns a pointer to it, the other creates an object of a particular type, which is two different levels of abstraction. 

 

As for data types, the data types supported by C are those supported directly or that have direct translations to hardware-level instructions (int, IEEE754 floating point, arrays), whereas higher-level languages also support things like arbitrarily sized integers (bigint), decimal floating point numbers, unicode strings, opaque reference types, closures. The control structures are those that can have very simple translations in assembly (if, while, etc., all map to cmp/jmp instructions), whereas higher-level languages support control structures that may have more complex translations (generic pattern matching, iterators, computation expressions, etc). The evaluation model is strictly imperative, with no lazy evaluation, again mapping to how CPUs work rather than to more general abstractions.

 
I didn't know about c-repl, but the project seems to have been dead since 2008. gdb is a debugger and doesn't allow defining new functions AFAIK, so its use as an REPL seems very limited. I have never seen a C book or online tutorial that explained how to use these tools for exploratory programming. It certainly isn't a focus of the language.
 
The number of commands or tools used for building are not what I'm referring to - one still has to compile C code, somehow, to run it. The lack of proper REPL support adds substantial overhead to the learning process and to the feedback loop.
 
Saying that recursion is a poor solution most of the time... how??? It's certainly a poor solution in C, but it's often the best solution in functional languages and generally speaking in the immutable style that is generally a better fit in today's multi-core world. If we're talking about data structures and algorithms, recursion is everywhere. Tree structures are defined recursively (even in C) and therefore operating on trees lends itself naturally to recursive implementations. Sorting algorithms are more often than not, defined formally in a recursive manner. For example wikipedia defines quicksort recursively.

I was talking about the language itself. C is clearly simpler in terms of keywords, concepts, paradigms, syntax, library, etc. Regardless of whether you learn it incrementally, there's still much more to learn and it's easier to get overwhelmed. 

 

I think I've illustrated well how the simplest C program introduces many more concepts than the simplest Python program, i.e. includes, headers, function definition syntax, brace scoping, etc. There are more features in Python, but they can learnt in better isolation from each other. Furthermore, Python is simpler to work with due to first-class REPL support, and Python contains fewer stumbling blocks due to its very lightweight syntax and support for concise expression of list comprehensions, iterating over arbitrary collections and the like. This means that learning Python is more productive as it introduces more useful and generally applicable concepts.



#45 simplezz

simplezz

    Neowinian Senior

  • Tech Issues Solved: 8
  • Joined: 01-February 12

Posted 14 May 2014 - 18:16

I said that C relies on explicit memory management and that higher-level language do not. I don't know what you're trying to accomplish by showing two statements that don't do that same thing at all, i.e. one reserves a portion of memory and returns a pointer to it, the other creates an object of a particular type, which is two different levels of abstraction.

You said explicit memory allocation was an unnecessary complication in C, or words to that effect by implication. My example was to demonstrate that in reality, there's not much difference in complexity, in terms of syntax and function calls, between malloc-ing a block of memory in C and creating an instance of a class in a higher level language. What the relevant functions / language constructs do is immaterial.
 

As for data types, the data types supported by C are those supported directly or that have direct translations to hardware-level instructions (int, IEEE754 floating point, arrays), whereas higher-level languages also support things like arbitrarily sized integers (bigint), decimal floating point numbers, unicode strings, opaque reference types, closures.

All of which at some point has to be reduced to machine code. I'm not sure what your point is here. The integral data types for most languages behave the same way. If C has less abstraction and executes quicker, that's a positive attribute.
 

The control structures are those that can have very simple translations in assembly (if, while, etc., all map to cmp/jmp instructions)

Any higher level language worth its salt should translate those same simple constructs (if/else, loop/while, switch), which are present in most languages, into similar machine code eventually.
 

whereas higher-level languages support control structures that may have more complex translations (generic pattern matching, iterators, computation expressions, etc). The evaluation model is strictly imperative, with no lazy evaluation, again mapping to how CPUs work rather than to more general abstractions.

Yet they still have similar simpler constructs, just like C. All of which can probably be reduced to the same cmp/jmp instructions.

Abstractions hide what's happening and complicate the thought process. That's why I'd never suggest someone start with an OOP language. I don't think that's suitable for a beginner who wants to learn how software and computers work.
 

I didn't know about c-repl, but the project seems to have been dead since 2008.

If you want something more modern and less hacky, then Cling (http://root.cern.ch/...l/content/cling) would probably be a better solution. It's an interactive C++ interpreter, built on the top of LLVM and Clang libraries.

And don't forget there are also C shells available on *nix style OS' that allow the execution of inline C-like code.
 

gdb is a debugger and doesn't allow defining new functions AFAIK. I have never seen a C book or online tutorial that explained how to use these tools for exploratory programming. It certainly isn't a focus of the language.

That's true, but it does allow the loading of libraries and elf binaries. So it's easy enough to call a range of functions and play around with them. It's not a full REPL-like environment, but it's a good interface for experimentation without having to recompile and rerun programs.

As a very capable debugger, one can jump to different functions and code, as well as executing and examining arbitrary functions that have been loaded. GDB is very useful. Once you learn the commands, much like vim, it makes you a very efficient programmer.
 

The number of commands or tools used for building are not what I'm referring to - one still has to compile C code, somehow, to run it. The lack of proper REPL support adds substantial overhead to the learning process and to the feedback loop.

A programmer doesn't write a full blown program in a REPL, not that I know of anyway. It's for experimentation and testing, or at least, that's what I use them for. If I'm writing a real program, I'll use a text editor like vim, and some kind of build system that compiles only changed or added code, manages dependencies, and execute custom scripts. REPL isn't practical for such real projects.
 

Saying that recursion is a poor solution most of the time... how??? It's certainly a poor solution in C

Yes it's a poor solution in C in a lot of situations, not all. I've seen it misused often when iteration could have just easily done the job.
 

For example wikipedia defines quicksort recursively.

And if you look further down it states:
The disadvantage of the simple version (recursive version) above is that it requires O(n) extra storage space, which is as bad as naïve merge sort. The additional memory allocations required can also drastically impact speed and cache performance in practical implementations. There is a more complex version which uses an in-place [Iterative] partition algorithm and can achieve the complete sort using O(log n) space (not counting the input) on average (for the call stack).

As I said, iteration is often the better choice. Functional languages are a whole different category.
 

I think I've illustrated well how the simplest C program introduces many more concepts than the simplest Python program, i.e. includes, headers, function definition syntax, brace scoping, etc. There are more features in Python, but they can learnt in better isolation from each other.

If your only ambition is to play around with a REPL, yes. If someone wants to write real python code that's executable and purposeful, no, because you're going to want to write modular code and the same kind of boiler plate things you mentioned like function signatures, white space indentation, imports, classes, etc.