LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2010, 10:19 AM   #31
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723

Quote:
Originally Posted by tuxdev View Post
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 '%'?
 
Old 06-10-2010, 12:23 PM   #32
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by MTK358 View Post
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’
("Look, no options!" )
 
Old 06-10-2010, 12:29 PM   #33
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
Quote:
Originally Posted by MTK358 View Post
What's so bad about printf()?
Nothing. This is also irrelevant to the matter that was being discussed.
 
Old 06-10-2010, 01:03 PM   #34
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
It's not optional at all - you could probably opt out of it with -Wno-<something>, but that's just asking for trouble:
Sorry, I meant "optional" as in "not required by the standard". Other compilers won't be so nice.
 
Old 06-10-2010, 03:44 PM   #35
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358
...Does that really matter unless you're so dumb that you keep typing the wrong letters after the '%'?
The dumbest thing a programmer can do is thinking of his/herself as being smart.
 
Old 06-10-2010, 04:10 PM   #36
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
The dumbest thing a programmer can do is thinking of his/herself as being smart.
That's not what I meant.

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.
 
Old 06-10-2010, 04:38 PM   #37
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
That's not what I meant.

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.
 
Old 06-10-2010, 05:22 PM   #38
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by MTK358 View Post
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.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need C++ help with overloading definition of a value dwhitney67 Programming 2 02-27-2009 09:40 AM
help overloading 'new' operator c++ qwijibow Programming 1 03-07-2007 04:58 PM
overloading vs polymorphism lbdgwgt Programming 1 03-06-2007 02:30 PM
overloading a template allomeen Programming 1 01-26-2006 06:59 PM
<< overloading lazyboy0001 Programming 2 04-10-2004 07:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration