Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 7th, 2005, 10:48 PM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
Weak typing (roughly) means that the compiler or interpreter of the language allows a variable to be treated as if it was a different type, or converted to that type. Strong typing means that the compiler or interpreter doesn't allow implicit conversions between types.

Weak typing is often viewed as more "programmer friendly", as a program is more likely to compile i.e. you can get something running quickly. Strong typing tends to require the programmer to take account of the type of a variable whenever it is used (eg it forces multiple functions to be written to do one thing, if those functions act on different types). Hence strong typing tends to be advocated by people who need to explicitly code everything (and know that no unintended effects will occur, due to quiet conversions).

Both C and C++ are weakly typed by a litteral interpretation of those definitions, as they both allow variables to be implicitly converted to different types.

If you view "typing" as a continuum from "weak" to "strong", then C++ is more stongly typed than C. There are other languages that are more weakly typed than C (scripting languages are typically in this category). There are also other languages (eg Ada) that are more strongly typed than C++.
grumpy is offline   Reply With Quote
Old Jul 8th, 2005, 2:13 AM   #12
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
Incidentally, int main() and int main(void) are the same thing in both C and C++.

The difference is that, in C++, calling main() [and it's supported variants] is forbidden. For example, main() cannot call itself recursively. In C, such calls are allowed (although, in practice, they are often considered to be bad style). IIRC, a side effect of this is that, in C++, computing the address of main() [i.e. as a pointer to function] is also forbidden.
grumpy is offline   Reply With Quote
Old Jul 8th, 2005, 8:07 AM   #13
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by grumpy
Incidentally, int main() and int main(void) are the same thing in both C and C++.
with VC++ 6.0 and Dev-C++ 2.9.8.7, you can test this yourself. Put this in a *.c file and compile it as a C program. No errors or warnings.
#include <stdio.h>

int main();

void foo()
{
	main(1);
}


Now change it to this, and the compiler will produce error that main() was declared without parameters.
#include <stdio.h>

int main(void);

void foo()
{
	main(1); <<< error here
}


Finally, make the same test programs in *.cpp program. The compiler will reject both programs.
Ancient Dragon is offline   Reply With Quote
Old Jul 8th, 2005, 9:19 AM   #14
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
<TANGENT>

Quote:
Originally Posted by Ancient Dragon
Well, you got a 66% on that test. the answer to 3) is maybe, depending on C or C++.

82.5%

Being that its maybe, implies a yes or no... since I answered yes, I was half right on the third question, which should supply a weight of half the possible points. At least I passed

</TANGENT>
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jul 8th, 2005, 10:45 AM   #15
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
1. int main() in C ONLY
means that main returns an integer and that main may have zero, one, two, three... arguments. This is confusing for a beginner. In practice it means you cannot ask the code inside main to use any arguments.

2. Any form of void main()... void main(int argc, char *argv[]) is a problem waiting to happen. Even if your compiler allows it.

All C code invokes something called startup. startup calls main. When main ends startup examines what it thinks main should have returned. It returns that value to the caller. What I've just described is part of the C standard (startup can variously be called _startup and other things like that).

The important part is the return. If main does not return anything, then startup returns what it finds after main exits. In this case what startup finds it will be garbage.

Most callers implicitly expect a return a value. If the return is -134455664 (garbage) instead of zero or maybe one, you can be in a world of hurt.
jim mcnamara is offline   Reply With Quote
Old Jul 8th, 2005, 1:46 PM   #16
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
main() does not have to return an int. It can return anything you want it to return. However, then control returns to the startup code as previously described by Jim, the program will treat whatever is returned as an int.
char* main()
{
	static char buf[80];
	return buf;
}

There are a lot of other incompatibilities between C and C++. Anyone interested can read some of them here

Last edited by Ancient Dragon; Jul 8th, 2005 at 2:01 PM.
Ancient Dragon is offline   Reply With Quote
Old Jul 11th, 2005, 8:48 AM   #17
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by jim mcnamara
1. int main() in C ONLY
means that main returns an integer and that main may have zero, one, two, three... arguments. This is confusing for a beginner. In practice it means you cannot ask the code inside main to use any arguments.
Only if you declare a prototype for main(). viz
int main();
In C++ there is no point in doing this (I suspect it may even be disallowed, but haven't checked), as a C++ programmer is not allowed to call any version of main() anyway. In C, you might do it, as calling main() is allowed.

When you define main() i.e. you do this
int main()
{
     /* the body of main */
}
this function accepts no arguments.

Quote:
Originally Posted by jim mcnamara
2. Any form of void main()... void main(int argc, char *argv[]) is a problem waiting to happen. Even if your compiler allows it.
It is only a portability problem, particularlly if you might eventually port your program to a compiler which doesn't support void main(). A lot of compilers do not support that form, whereas the int main() and int main(int, char **) [and equivalent] are required by the C and C++ standards.

There is no clause in the C or C++ standards that prevents a compiler from supporting other means of entering a program, such as void main() or [under windows] WinMain().

Quote:
Originally Posted by jim mcnamara
All C code invokes something called startup. startup calls main. When main ends startup examines what it thinks main should have returned. It returns that value to the caller. What I've just described is part of the C standard (startup can variously be called _startup and other things like that).
Incorrect. The C standard says nothing about anything called startup. The C standard requires that, when main() is called, that some conditions have been met (eg stdin and stdout initialised, among other things). It does not say how a compiler (or library) should achieve that, or what name is given to the code which does that work.

Quote:
Originally Posted by jim mcnamara
The important part is the return. If main does not return anything, then startup returns what it finds after main exits. In this case what startup finds it will be garbage.

Most callers implicitly expect a return a value. If the return is -134455664 (garbage) instead of zero or maybe one, you can be in a world of hurt.
What you describe is how some (broken) compilers do it. The standard requires that exiting from main without a return (i.e. falling off the end) is the same as returning zero.

The standard says nothing about how the return value from main() should be used, except that returning zero usually indicates "success". It is the operating system (or command line interpreter) which interprets that value.

Compilers that support void main() usually ensure some sensible value is given back to the operating system. But the standard is silent on what value should be given in this case, as it says nothing about void main().
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC