Posted: 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
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
jamez
Posted: 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.
octopi
Posted: Sat Apr 26, 2003 8:08 pm Post subject: (No subject)
cout is only in c++
Dan posted a tutorial for c
yuethomas
Posted: 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
Posted: Sat Apr 26, 2003 10:05 pm Post subject: (No subject)
thats because Dan doesnt know what he's talking about
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
Posted: 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
Posted: 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
Posted: 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 CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Sponsor Sponsor
Tony
Posted: 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
Posted: 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
Posted: Tue Jun 17, 2003 6:48 pm Post subject: (No subject)
my school doesn't teach c/c++ yess!
Prince
Posted: 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
Posted: 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
Posted: 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
Posted: Sat Jan 03, 2004 1:02 am Post subject: (No subject)
i think u understood it all wrong 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.