Radified Community Forums | |
http://radified.com/cgi-bin/yabb2/YaBB.pl
Rad Community Technical Discussion Boards (Computer Hardware + PC Software) >> PC Hardware + Software (except Cloning programs) >> Basic C programming: semicolon usage http://radified.com/cgi-bin/yabb2/YaBB.pl?num=1266346028 Message started by Rad on Feb 16th, 2010 at 12:47pm |
Title: Basic C programming: semicolon usage Post by Rad on Feb 16th, 2010 at 12:47pm
Can somebody tell me why there's no semicolon required at the end of the #include line and/or the main function declaration??
Code:
A semicolon only seems necessary at the end of the printf statement. Trying to understand the rules/logic/patterns. Also, some sources include the first solo 'int', while others do not. Some sources include the text found inside the main function parens, while others do not. Comment? |
Title: Re: Basic C programming: semicolon usage Post by Rad on Feb 16th, 2010 at 1:51pm
Here's another question .. regarding the formula to convert fahrenheit to celsius:
Code:
Why are there SPACES surrounding the minus sign, but not around the divisor? Or can you add and remove these spaces at will? (personal preference)?? |
Title: Re: Basic C programming: semicolon usage Post by MrMagoo on Feb 16th, 2010 at 2:18pm Rad wrote on Feb 16th, 2010 at 12:47pm:
Semicolons are required at the end of every statement. #include is a directive, or a special instruction to the compiler. Rad wrote on Feb 16th, 2010 at 12:47pm:
It is not necessary. Rad wrote on Feb 16th, 2010 at 12:47pm:
The variables declared in the parens describe the variables a function expects to be passed to it from whatever called it. In the case of main(), the calling function would be the shell that executed it, so the variables passed to it would be arguments passed to the program by the command line (or however the program was exectued.) Rad wrote on Feb 16th, 2010 at 1:51pm:
It's been a while since I'm looked at C seriously, but I think this is just a matter of style/personal preference. |
Title: Re: Basic C programming: semicolon usage Post by Spanky on Feb 16th, 2010 at 2:36pm
Thanks, bro.
|
Title: Re: Basic C programming: semicolon usage Post by Dan Goodell on Feb 16th, 2010 at 6:56pm
It's been a long time since I've worked with C, but . . .
Unless they're needed to separate words (e.g., "int c" vs. "intc"), spaces are often just to improve readability. For instance: int c = (5/9) * (f - 32) ; could just as legitimately be written: int c=(5/9)*(f-32); Note that spaces in a string, though, are literal, so "Hello world" is different from "Hello world". Think of the main() function like a subroutine. If you are passing variables when you call the subroutine, you need to describe what you're passing. (Note we're not talking about global variables here--which, strictly speaking, you wouldn't be "passing".) In the case of main(), the variables passed to it come from the command line. However, in the case of your "Hello world" example, the main() function isn't using any passed variables, so the text between the parens is superfluous and isn't necessary. And, if I remember my C, the solo "int" isn't really solo, it's part of "int main()", and could be written that way. If you are passing variables from a subroutine back to the calling function, you should be declaring the type of variable you're passing back (int, char, double, etc). Since the main() function is not passing anything back, the "int" declaration isn't strictly necessary. |
Title: Re: Basic C programming: semicolon usage Post by Rad on Feb 16th, 2010 at 7:59pm
Thanks for the clarification.
C is like a gateway language. |
Title: Re: Basic C programming: semicolon usage Post by Rad on Feb 16th, 2010 at 8:02pm
from Nigel:
Quote:
|
Title: Re: Basic C programming: semicolon usage Post by Rad on Feb 16th, 2010 at 8:03pm
cont'd
Quote:
|
Title: Re: Basic C programming: semicolon usage Post by Dan Goodell on Feb 16th, 2010 at 9:17pm
In response to your, "Some sources include the text found inside the main function parens, while others do not" . . .
Nigel said, "people got lazy and often tended to leave everything out altogether. [...] although in a pure C program you can still sometimes get away with this kind of thing, [...] good style should *always* include proper data type declarations for function arguments in the parentheses." Assuming there are function arguments, I agree that their data types should be declared. But what if there are no arguments? If there weren't any arguments, I didn't know using "main()" was being lazy. If main() is not expected to get any arguments from the command line (as in your "Hello world" example), does Nigel mean we should still include the "(int argc, char *argv[])" anyway? Or is he merely saying that *if* you have an argument, its data type should be declared? |
Title: Re: Basic C programming: semicolon usage Post by Rad on Feb 17th, 2010 at 11:47am Quote:
|
Radified Community Forums » Powered by YaBB 2.4! YaBB © 2000-2009. All Rights Reserved. |