ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It's not type-safe at all. GCC tries to give you pseudo-typesafeness warning, but it's completely optional. Stream insertion will always to the "Right Thing", as defined by the << overload for that type.
Does that really matter unless you're so dumb that you keep typing the wrong letters after the '%'?
Does that really matter unless you're so dumb that you keep typing the wrong letters after the '%'?
To be fair, anyone can make mistakes - you change an int to a long because you need a wider data type, and forget to change all the printf's it's in. Easily done.
Quote:
GCC tries to give you pseudo-typesafeness warning, but it's completely optional.
It's not optional at all - you could probably opt out of it with -Wno-<something>, but that's just asking for trouble:
Code:
$ cat test.c
#include <stdio.h>
int main(void)
{
long i = 5;
printf("%d\n", i);
return 0;
}
$ make test
cc test.c -o test
test.c: In function ‘main’:
test.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’
I meant that as long as you put in the right letters, it's impossible for something bad to happen, even if operating on user input.
You are not getting it - the right/wrong letters are better seen by compiler, and sometimes a variable "changes type", i.e. initially a programmer thinks he/she needs 'int', but then changes it to 'short' or 'long', but the programmer forgets to change the corresponding format specification in 'printf'.
My point is: absolutely never think you wont' make a mistake, always think you will, and use compiler to point out all possible stupid things you could think/make.
Last edited by Sergei Steshenko; 06-10-2010 at 05:44 PM.
I meant that as long as you put in the right letters, it's impossible for something bad to happen, even if operating on user input.
That's true.
However, you're assuming that programmers are infallible, and that's an assumption that every programmer I know (including - no, especially - myself) disproves on a regular basis.
For once, I agree with Sergei - please don't assume you'll always get it right just because it's easy, or that you won't change something in a project a few thousand lines long and fail to change every single instance of anything that's been affected by it.
Last edited by JohnGraham; 06-10-2010 at 05:41 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.