Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial]An Intro To C
Index -> Programming, C++ -> C++ Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dan




PostPosted: Thu Mar 20, 2003 7:12 pm   Post subject: [Tutorial]An Intro To C

An Intro To C

The C language is very different than Turing in many ways, so you may have trouble adjusting to it if you have done a lot of work in Turing but practice makes perfect Wink

Ok, one of the most common assumptions is that C and C++ are the same language. They may be very similar but the coding will very from each one. You can wire C code in almost anything but you will need a c compiler to run it. You can usually find a free C compiler on the net, or if you are using Linux it will already be installed in your OS.

C is mostly used for console apps, which means they will be mostly text and run in a dos window. This may make many people think that C is a useless language, but truthfully it has a lot of applications still and is a very good introduction to c++.

We will start with what almost all c codes contain in them. C programs will have a function called main which is where the main part of your program will go. Also, before declaring this function you will have to tell the complier that you are using the C language and what libraries and header files to include.

This is what the start of a basic c program may look like:

code:

#include <stdio.h> // this tells the computer to use
//the C library almost every c program has this in it

int main () //declaration of the main function,
//this is needed in almost every c program
{
printf("Hello world!!!!"); // put the words "hello world" on the screen

return 0;
}


How it works

Ok, first off, one of the main things you will need to know in C is that after most statements you need to put a ;. Also, another important thing to remember in C is to put the include statement to call the C library. All the code for the main function have to be within the { and }. The return 0 code tells the main function that it has run its course and to exit out of it and end the program.


The code for displaying text on the screen goes as follows:

printf("put text here");

This code will display the text in between the "'s on the line that was last specified. To move down one line put a \n in the "'s when you want to go to the next line.

And as all ways don't forget the ; at the end of the printf statement.


Well that's the very simple b of c, If I get time I will post more info on c and c++. If you want to learn more there are a lot of good web sites out there and I would recommend the book "C Primer Plus".

EDIT: i chaged the code to have int main() insted of void main() to be more corect and to increas proiblaity
Sponsor
Sponsor
Sponsor
sponsor
jamez




PostPosted: Sat Apr 26, 2003 7:56 pm   Post subject: (No subject)

Also instead of
code:
printf("Hello World.")


You can use
code:
cout << "Hello World."


It's all about preference. Smile
octopi




PostPosted: Sat Apr 26, 2003 8:08 pm   Post subject: (No subject)

cout is only in c++

Dan posted a tutorial for c
Razz
yuethomas




PostPosted: Sat Apr 26, 2003 9:00 pm   Post subject: (No subject)

Actually, main() should (but does not have to) be declared as int. At the end of the program put return(0); to indicate success.

(And, isn't it empty inside the brackets? Why void main(void)?)
Tony




PostPosted: Sat Apr 26, 2003 10:05 pm   Post subject: (No subject)

thats because Dan doesnt know what he's talking about Confused

its
code:

int main()


and you use return 1 (or any other int) to show success...

or

code:

void main()


and you dont need to use return

basically void is a procedure in C++ and any other variable type is a function
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Asok




PostPosted: Sat Apr 26, 2003 10:09 pm   Post subject: (No subject)

well think about void as simply anything that returns nothing. This is more for like void Weapons() it doesn't return anything, it's just a storage command. Once the compiler finishes with it, it automatically will free the memory location because it has no use for it.
octopi




PostPosted: Sat Apr 26, 2003 10:27 pm   Post subject: (No subject)

The below is perfectly acceptable, if you use SHITTY software that your school forces on you, which was made over 12 years ago. (Like me and dan.)

code:
void main () {

}


but yes, it is more proper to use

code:
int main(){
 return 0;
}


but I think its only a warning in vc++

You will want to return 0 on success, and a non zero value on error, the returned value should be an error code to help the user understand whats happening.
Dan




PostPosted: Sat Apr 26, 2003 10:47 pm   Post subject: (No subject)

you are rigth it whode be int main() or int main(void)

my school teches to just use void main() so i must have done it by habit. i will edit the toruale soon with the chages.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Apr 27, 2003 12:07 am   Post subject: (No subject)

my school teachers int main()...

but I prefer using void for main as I dont see it as a function. Unless you're to call main from within itself, or for some reason from another function... I just dont see a point of having a result
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
octopi




PostPosted: Sun Apr 27, 2003 8:45 am   Post subject: (No subject)

the point of returning a value from main, is that is what is returned to the operating system.

you should return a value of zero, if everything went good.

when you start executing the program in another program it will rely on that return value to decide whether or not the program ran correctly.
PaddyLong




PostPosted: Tue Jun 17, 2003 6:48 pm   Post subject: (No subject)

my school doesn't teach c/c++ yess!
Prince




PostPosted: Tue Jun 17, 2003 7:33 pm   Post subject: (No subject)

i wish my skool did... id rather learn c++ instead of java next year
Thuged_Out_G




PostPosted: Thu Dec 25, 2003 11:53 pm   Post subject: (No subject)

lol,my school teaches turing for gr.10,11, AND 12 compsci lmao...but we might be getting java for next semester or next year.

i feel bad for the gr.12's...having to spend 3 yrs on turing...what a waste
DanShadow




PostPosted: Fri Jan 02, 2004 1:47 pm   Post subject: (No subject)

Same here, I learned Turing all gr.11 CompSci....he was going to teach us Java, 3/4 of the class were retards, and couldnt even figure out Turing. Im currently trying to get my teacher to teach CPP instead of Java...because for one, in most aspects, in more understandable. Like seriously:
Turing
code:

put "Text String"

C++
code:

cout << "Text String" <<endl;

Java
code:

System.out.printIn("Text String");

Turing is easy, because its pretty much english. C++ is just as easy, its just english put together. (c-out.."as I put it". Prints a text string in 'c' out to the page.) Java is more like things a computer would figure out faster. it prints in a text string from the system.
Thats why I hate my school...stupid Java...I dont want to learn that in Gr.12, I want CPP!
rizzix




PostPosted: Sat Jan 03, 2004 1:02 am   Post subject: (No subject)

i think u understood it all wrong Wink that is not what System.out.println means heh

but i'd agree java is a bit complicated.. and more difficult to drift away into some relaxed programming style.

which is what java is appreciated for. it is strict.


but trust me.. u'll need to learn java to understand what that statement really means.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 20 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: