LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2007, 05:43 AM   #1
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Question C, C++, moving from Python


I've been programming small programs with different tools for quite some time, mainly to fill gaps where I didn't have a tool to automate some simple process. I've been using Python lately, and it's suitable for a lot of things I've done, but now I've set off to learn C. I've played around with it for a long time now, but never actually got farther than "Hello, world!" (well, the programs I tried to write were almost as simple as that). The reason I started learning C, finally, is physics - nowadays it's inevitable that one needs to use computers with the more complex things, and apparently it's C that is being used there (or something very close to C).

Anyway, yesterday I started over again with C. Now that I've used Python and my overall knowledge about programming (syntax, "the way it's done") is better, it felt a lot easier to write in C. However there are a few things that struck me as odd, and about which I'd like to know more:

- C doesn't know about "strings"; does this mean that I really have to use something like for loops with char arrays all the time if I need strings, and is it really efficient?
- there seem to be some differences between the C compilers; how do I know, if I want to write as general code as possible, that I'm not using something "gcc-only" stuff for example?
- C++ seems to be mostly C + something in addition; does this mean that if I learn C well, I'll have less trouble writing C++ also, or is it so that after learning C++ I'll know C too? Which one is more sensible? Or both at the same time?
- If I compile a program on Linux with gcc (for example), how likely will it run on some other Unix variant, and how likely does it compile on Windows? This is probably a total newbie question when it comes to programming questions, but as I've used interpreted languages like Python so far, I'll ask it to make sure..
- When writing programs in english it's fine, but how about if I wanted to use some charset/locale that is non-english? How does C work with non-english characters inside the code (of course I won't be using them in variable names etc., but in strings and such?)

I'm a quick learner, and Python has helped a lot in understanding the overall ideas in programming, but there seems to be a lot in C that is more or less completely new to me. I'll probably be getting a book about C somewhere too at some point, but the basics I'll try to learn "myself"; I've found it's the most teaching way. Having said that, I see a lot of books about C at the stores, some of them are a lot more recent than others; has C changed at all, or how much, during the years, and if I happen to read a somewhat old book, is it still accurate?

Thanks for comments, ideas and thoughts - everything.
 
Old 07-17-2007, 05:58 AM   #2
Tux-Slack
Member
 
Registered: Nov 2006
Location: Slovenia
Distribution: Slackware 13.37
Posts: 511

Rep: Reputation: 37
C doesn't have strings, at least I think. It's been a while now(4-5years) since I programed in C. But C++ does have strings. Maybe C does to, try including string.h, sorry forgot it.

There are diferences yes, but I've never ran across them.

C++ was first name C with classes. So basicaly it is C with addition. But it's not that much similarity. In example to print text on screen C syntax would be printf("Hello World!") while C++ syntax would be cout << "Hello World" << endl;
If you learn C++ there is no guarantee that you will know C too. I'm using C++ mainly now, for 4-5 years, before it was C, and I've forgoten almoast everything from C.

If you compile a simple application, like insert number and then the app calculates something from this number and prints the result it will run on all platforms, only that you need to compile it seperetly on unix, win aso.
If you write a more complex app that uses some specific Linux only libs i.e. then you will have to fix the code on windows i.e. to compile correctly.

Using non-standard ASCII chars could be a problem sometimes. I've had a bunch of problems with this. Mainly because C compilers don't know unicode, but I've solved this issue by compiling against Qt libs witch is a real great "add-on" for C++ while programing GUI.
 
Old 07-17-2007, 08:09 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
why are you moving?
I don't really get it, are you having performance problems?


you will find moving from python to C++ a very unpleasant experience.
C++ is ugly and awkward.

Original C is still a nice simple concise language for when you really need it.

If you are talking scientific stuff you will find it a lot simpler to express your
problem in python than C++. I assume the goal is to solve the problem rather than
sit for hours on a keyboard.

I would say C is good to learn C++ best to avoid

Last edited by bigearsbilly; 07-17-2007 at 08:11 AM.
 
Old 07-17-2007, 08:20 AM   #4
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Original Poster
Rep: Reputation: Disabled
Interesting opinions (thanks for them!)

Basically there are two main reasons I'm moving (but not completely) from interpreted languages (like Python) to compiled ones (C); the other one is that C is, or so I've heard, more efficient, and the other one that besides writing&compiling pure C code, some of the software used in my area is made so that (because physics problems are hardly ever exactly like something you encountered before) you'll write parts of the "solving machine" yourself, and that's where C comes into picture - somebody has decided it's the language you talk to the rest of the program.
 
Old 07-17-2007, 08:20 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Been studying Python for some time now, and I am totally impressed. One thing that keeps jumping out at me is that you don't need compiled languages for most programs. My Python books talk about using C modules as part of Python applications---where the same function in Python would be too slow.
 
Old 07-17-2007, 08:45 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
well, depends how you define efficiency.
if you are talking cruise missiles C maybe.

if you are talking "solve this while I make a coffee" then maybe not.

or are you talking someting you can program in python in a day taking 15 seconds
or something in C that takes 12 seconds but takes you 2 weeks with lots of debugger
sessions and hair pulling



I did a sudoku solver in lisp a while ago and granted it was not lightning fast
but it was OK and fun to do, I'd dread to do it in C++.
I had to do an expression evaluator in C++ recently and I still have nightmares.




I've done quite a bit of self study on this and (I use perl) found that
there is in perl only about a 10% speed (efficiency) drop over C.
In one simple case where I did the C properly with checks on file reads and writes
it was considerably slower. granted this is only anecdotal evidence.

C++ I imagine you would end up using the Standard Template Library (STL) and all the
fancy containers therein so therefore I would think they'd become as inefficient as interpreted code, though I haven't tested this assertion.

don't forget also that whilst your interpreter is starting up and parsing
(though probably a shared obj and already in memory) your executable still
has to be loaded by ldd and dynamically linked with it's shared objects
 
Old 07-17-2007, 09:49 AM   #7
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Original Poster
Rep: Reputation: Disabled
Quote:
Been studying Python for some time now, and I am totally impressed. One thing that keeps jumping out at me is that you don't need compiled languages for most programs. My Python books talk about using C modules as part of Python applications---where the same function in Python would be too slow.
That's one aspect I'll also be looking into, after I learn C better.

I may not have written my post to sound like I wanted it to; this wasn't supposed to be an argue about whether one language is better than the other, but then again, I think it's good to hear such arguments also. The main idea was that I could get some clarifications about C (and remember: I said I'm not going to move completely off Python - there's no sense, I've learnt it and it's of good use for me; there simply is no reason not to learn C as well).

And about the program execution times..well, I await that some programs I write do their job pretty quickly (like macros, so I don't have to play around with a calculator calculating similar problems all the time), some might take hours or days (hopefully not, though) to finish.

Any other comments about using non-english/ASCII characters in C programs? Or solutions to problematic situations?
 
Old 07-17-2007, 09:53 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
I may not have written my post to sound like I wanted it to; this wasn't supposed to be an argue about whether one language is better than the other,
agreed, sorry, got nothing to do here at work at the moment
 
Old 07-17-2007, 02:27 PM   #9
Tux-Slack
Member
 
Registered: Nov 2006
Location: Slovenia
Distribution: Slackware 13.37
Posts: 511

Rep: Reputation: 37
Quote:
Originally Posted by bigearsbilly
or are you talking someting you can program in python in a day taking 15 seconds
or something in C that takes 12 seconds but takes you 2 weeks with lots of debugger
sessions and hair pulling
I would have to disagree with that. I mean about debugger sessions and hair pulling. Ok I've pulled my hair a lot, but debugging is something else. I haven't used a debugger for so long that I've forgot how it all works.
I've started my "programing studies" with Pascal, then moved to C, and after when I "mastered" C I've moved to C++. Ok I did some Delphi, perl, python and others, but neither of them we're so great than C++, well at least in my experience.
Mainly because I don't care if I spend 5 hours or 5 days on a single app, all I want it is to work as I want it and with speeds that I want it. And being less hungry for resources as possible. I don't like apps that bunch up my CPU and RAM usage to the max. I'm not saying that python, perl or any other development tool does that, but I just prefer C++.
 
Old 07-17-2007, 03:14 PM   #10
RoughEdge
Member
 
Registered: Jan 2004
Location: Scotland
Distribution: Slackware 12
Posts: 67

Rep: Reputation: 15
I am currently studying Games Development at uni and we use a lot of C++, in fact, we use little else.
Quote:
C doesn't know about "strings"; does this mean that I really have to use something like for loops with char arrays all the time if I need strings, and is it really efficient?
C doesn't have a data structure to store strings like java does for example. While there are libraries for string manipulation, you define a string as such
Code:
char testString[25];
Quote:
- there seem to be some differences between the C compilers; how do I know, if I want to write as general code as possible, that I'm not using something "gcc-only" stuff for example?
More importantly, there are different versions of C. If you write code in ANSI C, then 99% of c compilers will run the code without any problems, assuming there are no strange hardware calls.
Quote:
- C++ seems to be mostly C + something in addition; does this mean that if I learn C well, I'll have less trouble writing C++ also, or is it so that after learning C++ I'll know C too? Which one is more sensible? Or both at the same time?
C++ is the superset to C. It is Object Oriented and allows the use of classes which makes making modular and re-usable code easier (in theory at least). I would advise anyone to learn C first if they want to go to C++. While some things are quite different, the experience from coding in regular C is just too good to pass over IMO.
Quote:
- If I compile a program on Linux with gcc (for example), how likely will it run on some other Unix variant, and how likely does it compile on Windows? This is probably a total newbie question when it comes to programming questions, but as I've used interpreted languages like Python so far, I'll ask it to make sure..
Similar to the second answer as far as I am aware. I have done very little programming of C/C++ on linux.
Quote:
- When writing programs in english it's fine, but how about if I wanted to use some charset/locale that is non-english? How does C work with non-english characters inside the code (of course I won't be using them in variable names etc., but in strings and such?)
No idea...sorry. I struggle to command English at the best of times let alone try and learn a new one.
In all honesty, pretty much any book written on C after 1995 will fair you well. A lot of the newer books make little changes except for a few "standards" changes. There are plenty of tutorials on the net that will get you up to speed with C in no time.
 
Old 07-18-2007, 12:25 AM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
There is no native support for different character sets in C, so if you need to use strings using different character sets then it would be best to search for a library that supports this. However, I would say that using C to manage different languages is tricky, although there is support for it within various Linux desktops.
 
Old 07-18-2007, 10:07 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
C++ I imagine you would end up using the Standard Template Library (STL) and all the
fancy containers therein so therefore I would think they'd become as inefficient as interpreted code, though I haven't tested this assertion.
I am actually talking utter nonsense.
Just tested it

the C++ is much faster
 
Old 07-20-2007, 02:02 AM   #13
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Original Poster
Rep: Reputation: Disabled
Thank you for answers, again!

Quote:
More importantly, there are different versions of C. If you write code in ANSI C, then 99% of c compilers will run the code without any problems, assuming there are no strange hardware calls.
Now this..I did have the picture before that people who knew how to program "in C", programmed in one language. Now you're saying there are several different ones? I assume their syntax is still just the same, but they use different libraries or something - or am I wrong? That sounds like I'll go for ANSI C, but what are (some of) the other "choices", any examples?

Quote:
I've started my "programing studies" with Pascal, then moved to C, and after when I "mastered" C I've moved to C++.
Sounds good. How would you define or estimate when does one "master" C, enough to start studying C++ without messing it all up? Is it after you know everything told in a regular C cookbook, or after five years of everyday coding? Just curious how long it took you to feel you've mastered it.

I'd love to learn C++ also, but at the moment (and every time I take a look at it) it seems there's still such differences that it's better to stick with C for now, so I don't forget half what I've learned about C and then mix up the rest with C++, resulting in learning neither one well..
 
Old 07-20-2007, 02:55 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
different types of C? Not sure what that means.
I assume the chap is referring to obsolete K&R style C which went about 15 years ago.
Today there's just ANSI C.
Buy the Kernighan and Ritchie book and start from there.

I would steer clear of C++ at first until you get C.
It shouldn't take long for a good programmer to get it, it's a very small neat language.

just get on and play with it C if you like it

Last edited by bigearsbilly; 07-20-2007 at 02:58 AM.
 
Old 07-20-2007, 03:21 AM   #15
1slipperyfish
LQ Newbie
 
Registered: May 2007
Location: wigan
Distribution: mandriva
Posts: 29

Rep: Reputation: 15
i learned java a few years ago then tried C++ and couldn't get my head around it
then six months ago i learned perl and using references etc gave me the confidence to try it again, it's not as easy to learn as perl or java but if you can get your head around the concepts of pointers and manipulating data on the stack/heap you should be ok i've no idea about C but i thought it was supposed to be quite buggy?? but people still code in it so it can't be that bad??
paul
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM
Python Question - moving file across filesystems drj000 Programming 3 01-26-2005 11:36 AM
copying/moving stalls when moving a lot of data to a usb stick =X¥®µ§= Linux - Hardware 10 07-30-2004 05:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration