Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-8081-

[C]: printf / scanf functions are evil

Name: Anonymous 2009-10-19 20:47

I'm just starting to learn C and the first exercise made in class was to make a program that prints the sum of two integers.

This program:

#include <stdio.h>
#include <stdlib.h>
int main (void) {
  int n1,n2;
  printf("1st int: ");
  scanf("%d",&n1);
  printf("2nd int: ");
  scanf("%d",&n2);
  printf("Sum = %d\n",n1+n2);
  return EXIT_SUCCESS;
}


I showed it to some of my older classmates and went batshit insane with it saying: DO NOT USE PRINTF AND SCANF, THEY ARE NOT FUCKING SAME and other stuff.

Will someone please input on this matter?
Thank you!

Name: OP 2009-10-19 20:53

>>1

s/SAME/SAFE/

Sorry!

Name: Anonymous 2009-10-19 20:53

I think YOU SHOULD READ SICP, it's a book. Here's the URL:
http://mitpress.mit.edu/sicp/

Thank you!

Name: Anonymous 2009-10-19 20:57

Input output stream has producer/consumer problems basicly. Sometimes you dont know what the fuck you are gonna get.

Use putchar or getchar

Name: Anonymous 2009-10-19 20:58

>>4
Producer what?

Name: Anonymous 2009-10-19 21:03

he's an idiot.
printf() is perfectly safe, as long as you don't do anything stupid.
i don't use scanf(), so i can't really comment on that though.

Name: Anonymous 2009-10-19 21:06

printf is safe as long as you don't let the user control the format string, otherwise it's exploitable.

Name: Anonymous 2009-10-19 21:39

printf and scanf are safe if you use them right.

Name: Anonymous 2009-10-19 22:05

Ignore them, they're of the BAWWWWWW IT'S UNSAFE BECAUSE I'M TOO MUCH OF AN IDIOT TO USE THEM PROPERLY type.

Name: Anonymous 2009-10-19 22:11

scanf is fine for quick hacks, but never, ever use it without checking its return value.

Name: Anonymous 2009-10-19 22:16

>>10
I think that's true for all input, whether you are checking it explicitly or inside the function call.

Name: Anonymous 2009-10-19 22:50

Only a moron would use printf() in an unsafe way.

Just tell them to stay away from the C programming language because it isn't for them.  Tell them to use Python or something.

Name: Anonymous 2009-10-19 23:52

printf is fine, tell your friends to stop being idiots.
scanf isn't unsafe (as long as you wrap it in some error recovery code), but it's basically worthless for actual input.  Use readline or fgets, then sscanf the result or parse it yourself.

Name: Anonymous 2009-10-20 1:34

>>9
that

>>13
scanf isn't unsafe
scanf isn't unsafe; format specifiers may be.

Name: Anonymous 2009-10-20 2:27

Guns are safe, its the bullets that re dangerous.

Name: Anonymous 2009-10-20 2:49

>>15
I'M PISTOL WHIPPING YOU. WHAT NOW, BIYATCH?!

Name: Anonymous 2009-10-20 2:50

>>16
One leg kick to the nuts.

Name: Anonymous 2009-10-20 2:59

>>17
Explicit write to EIP.

Name: Anonymous 2009-10-20 4:20

call get_>>15's_thread
mov [eax + thread.eip], 0xBA115

Name: OP 2009-10-20 5:49

OP here.

Thanks for the replies, guys.

So... is my code okay? Or it could be improved?

Thanks!

Name: Anonymous 2009-10-20 5:52


push new_ip
ret

Name: Anonymous 2009-10-20 6:14

>>20
in general use puts() instead of printf() when all you're doing is outputting a string. it's a lot more efficient.

Name: Anonymous 2009-10-20 6:17

>>22
or write()

Name: Anonymous 2009-10-20 6:23

>>22
gcc even replaces printf("...") with puts("...")

Name: Anonymous 2009-10-20 6:26

>>24
really?
that's awesome

Name: Anonymous 2009-10-20 6:26

>>24
But replacing printf("%s\n") with puts("%s\n") will result in correct behaviour?!

Name: Anonymous 2009-10-20 6:29

>>26
You are free to try. Your program is broken anyway, you can't blame gcc for making it do something else and not what you never intended to do.

Name: Anonymous 2009-10-20 6:43

>>26
i think what >>24kun meant to say is : it replaces printf() with puts() when you specify only a string without formating arguments .

besides string handling in C is generaly shit , dont use C if you can use a highlevel language for string operations .

Name: Anonymous 2009-10-20 7:55

besides string handling in C is generaly shit ,
Is that your way of saying "I don't understand programming"?

Name: Anonymous 2009-10-20 8:00

>>29
String handling in C is like using club as a weapon. Its not that effective, but you can upgrade it or sharpen it with tools. It will remain a club though, while your neighbors sport Ak-47 and uzi's.
However when civilization is done, the resources are scarce and there is nothing much left, the club is best tool for the job.

Name: Anonymous 2009-10-20 8:03

>>29
As someone who loves C, I've got to say that string handling in C is painful at best.
However, that doesn't stop me from doing stupid things like trying to write a regex engine in it.

Name: Anonymous 2009-10-20 8:10

>>31
It would be much improved if C had length-prefixed strings

Name: Anonymous 2009-10-20 8:14

>>32
Just write a struct to do it, or use a real string library it's not as if there is a shortage of them

Name: Anonymous 2009-10-20 8:20

The all-new LISP from Microsuss inc. will blow your socks off!

Name: Anonymous 2009-10-20 8:20

>>32
Thati s such a good troll! I always rage reading this bullshit

Name: Anonymous 2009-10-20 8:24

>>19
Which memory addressing mode is this?

Name: Anonymous 2009-10-20 9:04

Im looking for a print function, it cant be printufu (thats japanese for printf) or any stdio function. It has to be of 2 or more parameteru (thats japanese for 2 parameters) and has be be chibi (small) sized. And has to be really kawaii (cute). Also It has to be about 10-20 bux. And you have to post documentation of it first (i want to make shure it's kawaii [cute]). And it would be nice if it came with matching input reading (WITH error handling). OH! and it CANNOT have any complicated syntax, or be made out of C. It has to be made of C++, or something like that. Also it would be nice if it was made in japan. and not in china or corea (korea) or whatever. I have found a function similar to the one im describing in sourceforge, but it was 1 parameteru, and i dont want my formatou (formatting) to touch my other things (it can get mixed up and i would not like that, plus 2 paraneters looks more kawaii)

Name: Anonymous 2009-10-20 9:09

>>37
KOREA HAS THE BEST PROGRAMMERS.
Why would you want Japanese pigdogs to do it?
Damn stupid weeaboos! Koreaboos are much smarter and better looking!!!!!

Name: Anonymous 2009-10-20 9:19

>>35
Admit it, the length prefix was cumbersome only in days where every byte was counted. Today, RAM is especially cheap.

Name: Anonymous 2009-10-20 9:29

>>38
Shut up, you .... KOREABOO

Name: Anonymous 2009-10-20 12:39

>>40
Hangboo?

Name: Anonymous 2009-10-20 13:47

>>38
Has never reversed any Korean protocol or executable. Return to the board whence you came, please.

Name: Anonymous 2009-10-20 14:56

>>42
Koreans have their own executable formats?

Name: Anonymous 2009-10-20 15:20

>>43
yes they come in 7.62 and 5.56 millimeter sizes in a variety of lengths

Name: Anonymous 2009-10-20 15:26

>>42
Yeahh, Japanese protocols and code are so much better!!

Name: Anonymous 2009-10-20 15:37

>>39
this

Name: Anonymous 2009-10-20 16:18

>>44
back to /k/, please

Name: Anonymous 2009-10-20 16:28

>>47
coders and guns
go together like
beer and breakfast
son

Name: Anonymous 2009-10-20 16:46

>>48
back to /howToBeAHaxxor/, esr

Name: Anonymous 2009-10-20 16:53

>>49
Say that to my Heckler and Koch MR556 in person and see what happens.

Name: Anonymous 2009-10-20 18:04

>>50
Are you serious? gtfo

Name: Meme Explainer 2009-10-20 19:08

>>51
>>50 was referring to the popular ``Say that to my face'' meme, which originated on the /v/ - Video Games - 4chan image boards.

Name: Anonymous 2009-10-20 19:09

>>51
 ____  _____ _   _ ___ ____ 
|  _ \| ____| \ | |_ _/ ___|
| |_) |  _| |  \| || |\___ \
|  __/| |___| |\  || | ___) |
|_|   |_____|_| \_|___|____/

Name: Anonymous 2009-10-20 19:36

>>53
______________
< U MENA PENIS >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Name: UMH memesmith !gNlkr4vCuc 2009-10-20 19:48

>>54
ffff

Name: Leah Culver !1LEahRIBg. 2009-10-20 19:53

>>55
Oh shut the hell up UMH memesmith it's out of your hands and bitching won't solve anything.

Name: Anonymous 2009-10-20 20:13

>>54-56
Failed attempt at … I hardly know where to start. A life, I guess.

Name: Anonymous 2009-10-20 21:10

>>57
Starting a life is a job for either a Lisp program, or a real woman with ovaries.

Name: Anonymous 2009-10-22 11:22

WILL SOME ONE JUST FUCKING FIX OP'S PROGRAM?!

Name: Anonymous 2009-10-22 11:24

>>59
No

Name: Anonymous 2009-10-22 12:48

>>59
Hi OP

Name: Anonymous 2009-10-22 13:51

;; >>59
;; Sure thing bro
(defun prompt (prompt)
  (princ prompt *query-io*)
  (read *query-io*))
(format t "Sum = ~a"
        (+ (prompt "1st int: ")
           (prompt "2nd int: ")))

Name: Anonymous 2009-10-22 13:52

>>62
Now do it with (let).

Name: Anonymous 2009-10-22 15:06

>>62

CL-USER> ;; Sure thing bro
(defun prompt (prompt)
  (princ prompt *query-io*)
  (read *query-io*))
(format t "Sum = ~a"
        (+ (prompt "1st int: ")
           (prompt "2nd int: ")))
1st int: #. (loop (print (eval (read))))
(princ "I haxed your anus!")
I haxed your anus!
"I haxed your anus!"

Name: Anonymous 2009-10-22 16:28

>>59
-- Sure thing bro
import System.IO
main = mapM (\p -> putStr p >> hFlush stdout >> getLine >>= readIO) ["Int 1: ", "Int 2: "] >>= print . sum

Name: Anonymous 2009-10-22 17:31

>>59
# Sure thing bro
say<>+<>

Name: Anonymous 2009-10-22 17:59

>>65
import System.IO
main = mapM ((>>readLn) . (>>hFlush stdout) . putStr) ["1st int: ","2nd int: "] >>= putStrLn . ("Sun = "++) . show . sum

Name: Anonymous 2009-10-25 20:46

>>66
Invalid perl code :(

Name: Anonymous 2009-10-25 21:00

>>66
This only works if you hit ctrl+D after typing in the numbers.

Name: Anonymous 2009-10-25 21:32

Sure thing, bros.

10 INPUT "1st int: ", n1%
20 INPUT "2nd int: ", n2%
30 PRINT "Sum = ", n1% + n2%

Name: Anonymous 2009-10-26 1:16

>>68
It works inside a -E at least.  If you're actually running it as a file you need a version declaration. :(
Or you could do it the old way: print<>+<>,"\n"
>>69
No, lern2perl.

Name: Anonymous 2009-10-26 22:26

>>70
Hook me up with the QBasic interpreter

Name: Anonymous 2009-10-27 0:34

Name: Anonymous 2009-10-27 7:49


(defmacro twice (f x)
 `(,f ,@`(,x ,x)))

(twice + (read))

Name: Anonymous 2009-10-27 11:39

>>74
And people say Perl has too many punctuation marks.

Name: Anonymous 2009-10-27 12:15

>> 75 eh, there's not much shit in the standard readtable

Name: Anonymous 2009-10-27 16:03

>>75
eval "eval \"eval \\\"print \\\\\"Sure, whatever\\\\\"\\\"\"";

Name: Anonymous 2009-10-27 16:08

>>77
EVAL MY ANUS

Name: Anonymous 2009-10-27 16:38

>>77
compose n f
  | n < 1 = f
  | otherwise = f . compose (n - 1) f

main = putStrLn . compose 9 show $ "Sure, whatever"

Name: Anonymous 2009-10-27 17:02

fix show

Name: Anonymous 2009-10-27 17:42

>>79
compose = (foldr1(.).).replicate

Name: Anonymous 2009-10-27 17:46

>>79
main = iterate (.show) print !! 9 $ "Sure, whatever."

Name: Anonymous 2009-10-27 19:03

C:\Users\haskal\Documents\Downloads\qbasic\qbasic.exe
This version of C:\Users\haskal\Documents\Downloads\qbasic\qbasic.exe is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need a x86 (32-bit) or x64 (64-bit) version of the program, and then contact the software publisher.


Windows doesn't even know what architecture it's running on!

Name: Anonymous 2009-10-27 19:31

>>83
Even stranger is that it should run fine on a 32-bit Windows¹, so it has to be x64.

¹ As far as I know.

Name: Anonymous 2009-10-27 19:33

>>82
main = print $ iterate show "Sure, whatever." !! 9

Name: Anonymous 2009-10-28 1:15

>>84
The "shit" version of Windows 7 breaks support for DOS apps.

Name: Anonymous 2011-02-02 23:22

Name: Anonymous 2011-02-17 20:09

that's cool and all, but check my doubles over there<

Don't change these.
Name: Email:
Entire Thread Thread List