Page 1 of 1 [ 15 posts ] 

JakeGrover
Sea Gull
Sea Gull

User avatar

Joined: 21 May 2010
Age: 28
Gender: Male
Posts: 221
Location: Vermont, USA

25 Jun 2010, 4:53 pm

Hello everybody. I am learning C. I just have a small question. In what circumstances do you put semicolons on the end of the line of code? If somebody could answer this, It would be very helpful.



Jookia
Velociraptor
Velociraptor

User avatar

Joined: 7 Jan 2007
Age: 30
Gender: Male
Posts: 410

25 Jun 2010, 6:42 pm

After calling functions, setting variables and at the end of structures.

What're you learning C from that isn't teaching you this?



JakeGrover
Sea Gull
Sea Gull

User avatar

Joined: 21 May 2010
Age: 28
Gender: Male
Posts: 221
Location: Vermont, USA

25 Jun 2010, 6:58 pm

I just barely started. They probably do teach this somewhere, but, I was just asking ahead of time. By the way, I'm learning C from Masters of The Void. Very good. I learned it once from them, but then forgot it, so, I'm relearning it.



Apera
Veteran
Veteran

User avatar

Joined: 23 Feb 2008
Age: 36
Gender: Male
Posts: 871
Location: In Your Eyes

25 Jun 2010, 9:16 pm

I've only done C++ and VB, and I know that in C++ you end almost everything with a semicolon.


_________________
When I allow it to be
There's no control over me
I have my fears
But they do not have me


JakeGrover
Sea Gull
Sea Gull

User avatar

Joined: 21 May 2010
Age: 28
Gender: Male
Posts: 221
Location: Vermont, USA

25 Jun 2010, 9:18 pm

What is the difference between C++ and C?



LordoftheMonkeys
Veteran
Veteran

User avatar

Joined: 15 Aug 2009
Age: 37
Gender: Male
Posts: 927
Location: A deep,dark hole in the ground

25 Jun 2010, 9:38 pm

Every line except those that contain preprocessing directives, end with a curly bracket, or end inside of parenthesis.


_________________
I don't want a good life. I want an interesting one.


Fo-Rum
Velociraptor
Velociraptor

User avatar

Joined: 21 Sep 2008
Age: 42
Gender: Male
Posts: 435

25 Jun 2010, 10:32 pm

Anything that is a statement ends with a semi-colon. That is how tutorials and books always described it, and I've looked at quite a few introductions to C/C++ (not because I need them, I just enjoy browsing them).


_________________
Permanently inane.


JakeGrover
Sea Gull
Sea Gull

User avatar

Joined: 21 May 2010
Age: 28
Gender: Male
Posts: 221
Location: Vermont, USA

25 Jun 2010, 10:35 pm

Fo-Rum, thank you for that nice explanation.



lau
Veteran
Veteran

User avatar

Joined: 17 Jun 2006
Age: 77
Gender: Male
Posts: 9,798
Location: Somerset UK

26 Jun 2010, 6:42 am

JakeGrover wrote:
Hello everybody. I am learning C. I just have a small question. In what circumstances do you put semicolons on the end of the line of code? ...

Never.

The positioning of semicolons and the ends of lines of code have utterly no connection.

Other than for the purposes of error reporting, line breaks are merely white space, which is generally ignored, bar for lexical delimiting of identifiers and keywords from each other.

A single semicolon is used to terminate some statements.

White space is used to terminate some tokens.


_________________
"Striking up conversations with strangers is an autistic person's version of extreme sports." Kamran Nazeer


ruveyn
Veteran
Veteran

User avatar

Joined: 21 Sep 2008
Age: 89
Gender: Male
Posts: 31,502
Location: New Jersey

26 Jun 2010, 9:00 am

JakeGrover wrote:
What is the difference between C++ and C?


C++ is object oriented.

ruveyn



LordoftheMonkeys
Veteran
Veteran

User avatar

Joined: 15 Aug 2009
Age: 37
Gender: Male
Posts: 927
Location: A deep,dark hole in the ground

26 Jun 2010, 5:22 pm

JakeGrover wrote:
What is the difference between C++ and C?


C++ is the same thing as C; it just has some stuff added to it.


_________________
I don't want a good life. I want an interesting one.


pakled
Veteran
Veteran

User avatar

Joined: 12 Nov 2007
Age: 68
Gender: Male
Posts: 7,015

26 Jun 2010, 11:07 pm

to provide more than a comma's break in a sentence, or just before a list by commas...;)


_________________
anahl nathrak, uth vas bethude, doth yel dyenvey...


Fuzzy
Veteran
Veteran

User avatar

Joined: 30 Mar 2006
Age: 53
Gender: Male
Posts: 5,223
Location: Alberta Canada

30 Jun 2010, 11:50 pm

The semi-colon joins two statements with a disparate relationship

putOnCleanUnderwear();goOutAndPlay()

while the colon joins two sentences that are connected.

Though I knew you were really asking about programming: I could not resist.


_________________
davidred wrote...
I installed Ubuntu once and it completely destroyed my paying relationship with Microsoft.


WoodenBoy
Deinonychus
Deinonychus

User avatar

Joined: 12 May 2010
Age: 51
Gender: Male
Posts: 343
Location: Bristol

01 Jul 2010, 6:07 am

Oh and there's also a risk that after programming c/c++ for a while you start putting semicolons at the end of all your sentences; Like I'm doing here; It can be a bit embarrassing;



Ichinin
Veteran
Veteran

User avatar

Joined: 3 Apr 2009
Gender: Male
Posts: 3,653
Location: A cold place with lots of blondes.

01 Jul 2010, 6:25 am

Note: Code outside brackets should end with a semicolon.

Quote:
if (i!=10)
i++;


Code outside brackets (i.e. the IF statement below) does not need a semicolon since it's logical space is controlled by the brackets.

Quote:
if (i!=10) {
i++;
j++;
k++;
}


My tip is to experiment and compile and see what happens. Reading a book will make you booksmart and a generic worker, tinkering with stuff on your own beyond the book will make you awesome.

And a final note about C: Unless you want to spend your life programming dishwashers and embedded systems, go directly to C++ and do not look back.


_________________
"It is far better to grasp the Universe as it really is than to persist in delusion, however satisfying and reassuring" (Carl Sagan)