Jump to content

I want to learn C(++?)


Recommended Posts

Guest Adriano

Here's the correct code for your program:

/* hello.c */
#include <stdio.h>
int main(void) { /* The brace can go here or on a line of its own, it's just more compact this way */
printf("hello, world!\n");
return(0);
}

 

Compile it with the following command:

gcc -g -Wall -o hello hello.c

in a Linux console, and execute it with ./hello

 

Now let's see it a bit closer, line by line.

 

/* hello.c */ is a comment describing the name of the program. Good.

 

#include <stdio.h> is a preprocessor command. It adds to your program the contents of the standard library file stdio.h. This is a collection of important Input/Output functions and definitions.

 

int main(void) { opens the definition of the main() function. Every C program has to have a main() function. int main(void) means this functions returns (outputs) an integer (a number without a decimal part) on exit. This output is the "return(0);" line. the "void" word means the main function takes no arguments. I.E. the command "./hello --help" will do nothing different from "./hello".

 

printf("hello, world!\n"); is an interesting line. Printf() is a function from the stdio.h file (that's the reason to call it in the first place, some lines ago). It prints to the standard output (normally your screen) the string between double quotes. \n is a symbol for a carriage return.

Notice how the call to the function has a semicolon at the end. The semicolon is used in C to end a statement such as a call to printf, a variable assignment, and much more. Notice the line "int main(void){" has no semicolon: it's because the braces open a block of statements.

 

return(0); Finally, as said above, we call return with a value of 0 to tell the operating system that our program exited correctly; then close the main() block with "}"

My version of GCC, when compiling with the options mentioned, asks for an extra line after the closing brace of main(). Don't know why, but that's why I added it here, too.

 

I hope this helped.

Link to comment
Share on other sites

As it seems to be quite widely used and quite powerful, I'd like to learn C.  (Maybe C++, but I just don't even know the difference.) 

 

Isn't one of the main differences that C is purely procedural whereas C++ can be Object Oriented?

 

If so - go for C++ first. getting into a preocedural mind set will make it much harder to switch to OO programming...

Link to comment
Share on other sites

Well, I spent some time trying to learn C programing.  I coudln't even get hello world to compile.  I get all sorts of error messages no matter what I do.  I'm running mandrake 10.0.  This is the source code of my little program.  I have no idea what I'm doing wrong.

;; This buffer is for notes you don't want to save, and for Lisp evaluation.

;; If you want to create a file, visit that file with C-x C-f,

;; then enter the text in that file's own buffer.

 

/* hello.c */

/*#include <stdio.h> I commented this out cause it was giving me all sorts of errors*/

int

main(void)

{

  printf("hello, world!\n");

  return 0;

}

 

Hey, I got it to work, remmber to delete the stupid nots at the top of the emacs, it messes up compiling real bad and will turn you into a flustrated newbie.  :lol:

 

Or you can do this in C++ way

 

#include <iostream>

 

using namespace std;

 

int main(){

cout << "Hello World! << endl;

return 0;

}

 

compile it g++ your name.cpp

Edited by rescue
Link to comment
Share on other sites

Guest Adriano

I'm going through (almost) all the exercises in the book "Practical C programming" I mentioned earlier (for the lazy: edition == 3rd, editor == O'reilly, author == Steve Oualline). When they're all solved I'll publish them in my website.

 

I've heard that for some people, a drawback for the book is that it doesn't show you the answers to its exercises. That's why I'm doing this.

Link to comment
Share on other sites

  • 2 months later...

Ok, my contribution :D

 

Do you want to learn the programming language?

 

-or-

 

Do you want to learn how to write programs?

 

Those two things are completely different! If you want to learn the programming language then you should be looking for the books written by those who invented the language, ie,

 

C Programming Language by Kernighan & Ritchie

C++ Programming Language by Stroustrup

 

(although Stroustrup seems to be written primarily for people who develop the compilers)

 

C is primarily a structural language. C++ is C plus object orientation. C++ is not a pure object oriented language. Look at a language called smalltalk instead.

 

If you want to learn how to write programs, I would recommend not using a visual development environment. In my experience (alas limited to Win32) these hide some of the detail away from you. gcc is your friend :D

Link to comment
Share on other sites

whatever you do, don't get 'C++ All In One Desk Reference For Dummies'

I like www.cplusplus.com's tutorial for the basics...after that I just learned mostly from www.cpp-home.com...

oh, and if you want a good graphics book, get 'Game Programming All In One (2nd Edition)'

Link to comment
Share on other sites

"The C Programming Language" by Kernigan and Ritchie (Dennis Ritchie being the Creator of C) and

"The C++ Programming Language" by Bjarne Stroustrup (the creator of C++)

are both outstanding books for any C or C++ programmer, and "must haves".

 

However, neither are great for newbies. For that, any number of C/C++ online tutorials are good. For a book that is a great C++ introduction I recommend "Accelerated C++" by Konig and Moo. This book is outstanding, and teaches crucial and useful higher level concepts (like the STL) right off the bat.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...