PDA

View Full Version : Newbie Programming in C




Futhark
Jul 5, 2012, 10:48 AM
This might sound silly but it's a question I don't know the answer to :-) I'm learning C and I've written several basic math program's, I've learned to compile them using GCC

I type GCC myapp.c -o myapp and it compiles it and creates the program for me. If I want to share my app with friends to test is it the file it creates I send? Does it matter if they are MAC or PC users or does it not matter? I know on a mac terminal opens the program so if it works on a PC what runs it?

As a mac user will all my apps I write in C only open in terminal or will I be able to create an independent app that runs outside of terminal? Something when opened say will open a window with my app running?

I hope I've explained this ok lol :) I know what I'm trying to ask for but not sure I've worded it well enough for you to understand hehehehe Fingers crossed I have.



robvas
Jul 5, 2012, 11:04 AM
If you compile a program on a Mac, it's only going to run on a Mac. Same goes for Linux or Windows. You could send them the source code and they could compile it themselves, though.

Other languages such as Java have the goal of 'write once, run anywhere'.

macuser1232
Jul 5, 2012, 11:20 AM
This might sound silly but it's a question I don't know the answer to :-) I'm learning C and I've written several basic math program's, I've learned to compile them using GCC

I type GCC myapp.c -o myapp and it compiles it and creates the program for me. If I want to share my app with friends to test is it the file it creates I send? Does it matter if they are MAC or PC users or does it not matter? I know on a mac terminal opens the program so if it works on a PC what runs it?

As a mac user will all my apps I write in C only open in terminal or will I be able to create an independent app that runs outside of terminal? Something when opened say will open a window with my app running?

I hope I've explained this ok lol :) I know what I'm trying to ask for but not sure I've worded it well enough for you to understand hehehehe Fingers crossed I have.umm why use gcc when you have Xcode or Code::Blocks?

Futhark
Jul 5, 2012, 11:49 AM
umm why use gcc when you have Xcode or Code::Blocks?

I use both but since I'm trying to learn the fundamentals I'm using GCC alongside MacVim. I'll use Xcode once I understand the language better and move onto Objective-C which is my goal. :)

Futhark
Jul 5, 2012, 11:51 AM
If you compile a program on a Mac, it's only going to run on a Mac. Same goes for Linux or Windows. You could send them the source code and they could compile it themselves, though.

Other languages such as Java have the goal of 'write once, run anywhere'.

That's good to know thanks. So when I write my small program's are they destined to only be opened in terminal?

ghellquist
Jul 5, 2012, 12:56 PM
That's good to know thanks. So when I write my small program's are they destined to only be opened in terminal?

Yes, and no. It sort of depends.

When you write "bare" C programs they rely on a library of standard functions. These functions include allowing you to write characters to the terminal using printf, getting characters from the terminal, connecting to an internet port and so on. These functions are more or less the same on the source code level on just about any operating system or computer today, thanks to standardisation. Incidentally the c-compiler more or less automatically includes these functions, but in c you still need to #include <stdio.h> or other functions. ( By the way, it is quite interesting to try to understand what happens in stdio.h, beware that it has some quite advanced concepts ) .

But the standard c libraries will not allow creating windows or getting events when you mouse click on a button or pinch with your fingers on an iPad, just two of many examples. These functions requires you to use other libraries. These libraries are very different on different operating systems: quite different on OSX compared to Windows 7 as one of many examples.

The difference between different operating systems when it comes to "windows" makes it quite difficult to write programs that work in several environments ( several Os-es ) . What the big guys does probably do is to divide the program into two parts: one part doing the actual work, and another exchangeable part that interfaces with the operating system and creates windows and buttons and menus and such stuff. This allows the software house to have different programmers for the different parts, maybe one programmer doing the actual mathematical algorithm ( or whatever ) and a different programmer handling windows and yet a different programmer handling OS X. It is quite possible that the OS X windows parts are written in Objective-C while the Microsoft Windows part might be written in C# and the algorithms written in C.

So, for now, if you are a starting programmer, stay in the terminal with C. If you want to program windows for OS X the best way is to move over to Objective C and Cocoa (the windows framework on OS X ). Objective is part very close to C and actually allows you to include "pure" C functions ( not the main program though ).

If you want to transfer over to Microsoft Windows, I would suggest you go for the not quite obvious solution of programming in Visual Basic. This is probably the fastest way to get things up and running in Windows.

Anyway, my five cents of thoughts, probably worth as much. Over to you ( or to other fellow forum thinkers ).
//Gunnar

Futhark
Jul 5, 2012, 01:08 PM
Yes, and no. It sort of depends.

When you write "bare" C programs they rely on a library of standard functions. These functions include allowing you to write characters to the terminal using printf, getting characters from the terminal, connecting to an internet port and so on. These functions are more or less the same on the source code level on just about any operating system or computer today, thanks to standardisation. Incidentally the c-compiler more or less automatically includes these functions, but in c you still need to #include <stdio.h> or other functions. ( By the way, it is quite interesting to try to understand what happens in stdio.h, beware that it has some quite advanced concepts ) .

But the standard c libraries will not allow creating windows or getting events when you mouse click on a button or pinch with your fingers on an iPad, just two of many examples. These functions requires you to use other libraries. These libraries are very different on different operating systems: quite different on OSX compared to Windows 7 as one of many examples.

The difference between different operating systems when it comes to "windows" makes it quite difficult to write programs that work in several environments ( several Os-es ) . What the big guys does probably do is to divide the program into two parts: one part doing the actual work, and another exchangeable part that interfaces with the operating system and creates windows and buttons and menus and such stuff. This allows the software house to have different programmers for the different parts, maybe one programmer doing the actual mathematical algorithm ( or whatever ) and a different programmer handling windows and yet a different programmer handling OS X. It is quite possible that the OS X windows parts are written in Objective-C while the Microsoft Windows part might be written in C# and the algorithms written in C.

So, for now, if you are a starting programmer, stay in the terminal with C. If you want to program windows for OS X the best way is to move over to Objective C and Cocoa (the windows framework on OS X ). Objective is part very close to C and actually allows you to include "pure" C functions ( not the main program though ).

If you want to transfer over to Microsoft Windows, I would suggest you go for the not quite obvious solution of programming in Visual Basic. This is probably the fastest way to get things up and running in Windows.

Anyway, my five cents of thoughts, probably worth as much. Over to you ( or to other fellow forum thinkers ).
//Gunnar

Wow now that's what I call a reply !!!! Thank you so much for going into so much depth and explaining yourself so well. I'm only a few weeks into C and loving it. I will progress into Objective-C once I feel I have a reasonable understanding of C my first ever programming language. What I've learned so far is maths based so I have created several small apps which I look forward to developing even further as my knowledge of the language progresses.

For example I've created a loan repayment app that lets me input how much of a loan was taken out and then asks if there is interest on the loan and it calculates how much I will have to pay back. I then get to make payments and when I input a payment it deducts it from my loan total but when I close the program it's all lost hehehehehe I've not learned yet how to save information that can be continued again once the program is restarted? Is this easy to do? I know my book does explain this but it's quite near the end and I don't want to skip chapters

AdrianK
Jul 5, 2012, 01:21 PM
I've not learned yet how to save information that can be continued again once the program is restarted? Is this easy to do? I know my book does explain this but it's quite near the end and I don't want to skip chapters
You could write it to a text file to save, then read the contents when the program starts up. How you format that completely depends on what fields you need to store. Using a fixed length record would be an easy way to go.

Futhark
Jul 5, 2012, 01:24 PM
You could write it to a text file to save, then read the contents when the program starts up. How you format that completely depends on what fields you need to store. Using a fixed length record would be an easy way to go.

I took a peak at the contents pages there and they mention saving to text file tutorials. Damn I want to skip chapters :D MUSTN'T SKIP CHAPTERS !!!

DrMotownMac
Jul 16, 2012, 09:21 PM
Interesting topic. I'm sort of a programming newbie too. When I was in college, 20+ years ago, I took a class in FORTRAN, and I learned a little BASIC and PASCAL back in high school. Anyway, I switched from engineering school to medicine, and that was the end of my programming. So, I've never programmed in C, C++, Java, Objective-C, or any more relevant languages, and I have no clue about object-oriented programming, Cocoa, XCode, etc.

So, per some suggestions I've read here and elsewhere, I just downloaded the iBook version of "Objective-C Programming: The Big Nerd Ranch Guide," and I just started reading it last week, along with working through the programs and challenges. Apparently, it walks you through several chapters of "essential C" before getting into Objective-C. Very interesting and quite a fun challenge!

Anyway, I'm curious which C book you're reading, and what you plan to do next. My goal is to become proficient enough to create my own app on the App Store...just to prove to myself that I'm an old dog who is still able to learn new tricks! Well, it's fun reading about others who are trying to learn this stuff too!