printf() and scanf() 
Author Message
 printf() and scanf()

hello everyone,
i was wondering i know that with scanf() you are supposed to pass it the
address in memory where you want the value assigned like if it was a variable:

int a;
[snip]
scanf("%d", &a);

and if it where all ready a pointer:

int *a;
[snip]
scanf("%d", a);

but what about printf()?
normally you just pass it the name of the variable:

int a;
[snip]
printf("%d", a);

so does that mean when working with a you do:

int *a;
[snip]
printf("%d", *a);

or:

int *a;
[snip]
printf("%d", a);

i have heard that scanf() is just the complete opposite of printf() but i did
not take it literally! does this mean that you pass printf() the actual value
and that you pass scanf() the address in memory? are they really truely
opposite like that?

note: if they really are opposite like that thsi would be the right one:

int *a;
[snip]
printf("%d", *a);

but i do not know

thank you everyone and i hope my question was not so stupid

-art  



Thu, 24 Jul 2003 06:51:16 GMT  
 printf() and scanf()


Quote:
>hello everyone,
>i was wondering i know that with scanf() you are supposed to pass it the
>address in memory where you want the value assigned like if it was a variable:

>int a;
>[snip]
>scanf("%d", &a);

This gets an integer into a...

Quote:
>and if it where all ready a pointer:

>int *a;
>[snip]
>scanf("%d", a);

...and this gets an integer into the int that a points to.  Note that
that's not exactly the same thing, though in each case what your code
snippets do is usually The Right Thing.

Quote:
>but what about printf()?
>normally you just pass it the name of the variable:

>int a;
>[snip]
>printf("%d", a);

This prints the int that is stored in a...

Quote:
>so does that mean when working with a you do:

>int *a;
>[snip]
>printf("%d", *a);

...And this prints the int that a points to, which is the equivalent of
the scanf snippet that uses a pointer above.

Quote:
>or:

>int *a;
>[snip]
>printf("%d", a);

This is incorrect; if you want to print the integer value, you'd want
to pass *a as above.  It's also possible to print a pointer itself:
printf("%p",(void *)a);
This prints a pointer value, which on many systems will look something
like '0xdeadbeef' (which is a hexadecimal value representing the address
that the pointer points to).  (This probably isn't what you were asking
about, but it's interesting and sometimes even useful.)

Quote:
>i have heard that scanf() is just the complete opposite of printf() but i did
>not take it literally! does this mean that you pass printf() the actual value
>and that you pass scanf() the address in memory? are they really truely
>opposite like that?

They're not exact opposites, since some conversion characters (such as %i)
act differently for scanf than the opposite of printf, but in the case
of whether to pass pointers or values it's probably easiest to think of
them as opposites.  (Of course, it's probably even better to not bother
with scanf at all and use other, more robust ways of getting input.)

dave

--

So would you say that this is sickly contorting, maximally probable code?
                                             --Kaz Kylheku in comp.lang.c



Thu, 24 Jul 2003 07:22:30 GMT  
 printf() and scanf()


Quote:
>hello everyone,

>but what about printf()?

>int *a;
>[snip]
>printf("%d", *a);

perfect.

Quote:
>int *a;
>[snip]
>printf("%d", a);

no.

Quote:
>i have heard that scanf() is just the complete opposite of printf()

Its not - scanf must take pointers to its arguments, so that it can
store data in them and return the modified data. printf only needs the
args themselves, so it can read from them
but i did

--
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>



Thu, 24 Jul 2003 07:48:51 GMT  
 printf() and scanf()
thanks for your help!

Quote:
>printf("%p",(void *)a);

this make perfect sense to me except for one part:

(void *)

i gather you can put any data type such as (char *) or (int *)
but hwat does this do and why is it needed? if you could explain i think it
would help me alot? my hypotheses for right now is that it multiplies that data
type by itself and the value ends up being that like:

(float *) would evaluate to:

4

or

(double *) to:

8

thanks and please tell me if my guess is wrong!



Thu, 24 Jul 2003 08:12:25 GMT  
 printf() and scanf()


Quote:
>thanks for your help!

>>printf("%p",(void *)a);

>this make perfect sense to me except for one part:

>(void *)

>i gather you can put any data type such as (char *) or (int *)
>but hwat does this do and why is it needed?

%p requires a pointer. void* is a generic pointer. So you're casting a
to a pointer.

Quote:
>if you could explain i think it would help me alot?
>my hypotheses for right now is that it multiplies that data type by itself

Not sure what that means !

Quote:
>and the value ends up being that like:

>(float *) would evaluate to:
>4
>or
>(double *) to:
>8

No, it will cast the value to a pointer type, and hten printf will
print out hte value of that pointer.

--
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>



Thu, 24 Jul 2003 08:14:13 GMT  
 printf() and scanf()


Quote:


>>>printf("%p",(void *)a);

>>this make perfect sense to me except for one part:

>>(void *)

>>i gather you can put any data type such as (char *) or (int *)
>>but hwat does this do and why is it needed?

>%p requires a pointer. void* is a generic pointer. So you're casting a
>to a pointer.

In this case, a was already a pointer (to int), but %p specifically
requires a pointer to void, and since printf is a variadic function[1]
the conversion to (void *) needs to be done explicitly.

In general, for a type T, (T)foo tells the compiler you want to convert
foo to type T.  Often this is done automatically, but in some cases
(like passing arguments to variadic functions) it isn't so you need
to specifically tell the compiler to do it.  Casts can also be used
to tell the compiler ``Yes, I really want to do this, and know exactly
what I'm doing'' for a conversion that it would otherwise warn about;
doing this is usually a Bad Thing, and needing a cast to silence a
compiler warning is usually a sign that you're doing something wrong.

[1] A variadic function is one that can take varying numbers of arguments;
    the relevance here is that the default argument promotions are applied
    to arguments after the last explicitly specified one (e.g. a short
    is converted to an int), and the default promotions don't convert
    pointers (a pointer to <foo> is always passed as a pointer to <foo>).

dave

--

Absolutely right. Actually telling people they're in the wrong newsgroup for
their question would contravene their constitutional right to be ignorant.
                          --Richard Heathfield roasts a troll in comp.lang.c



Thu, 24 Jul 2003 08:38:26 GMT  
 printf() and scanf()
ok so say i did this:

int *a;
[snip]
printf("%c, (char *)*a);

this would change the data type of the value a points to to a char data type?(i
know that this is not needed i did this just for example purposes) and does it
change it to the char data type for the rest of the program or just for that
function?

thanks!

-art



Thu, 24 Jul 2003 09:36:25 GMT  
 printf() and scanf()

Quote:

> ok so say i did this:

> int *a;
> [snip]
> printf("%c, (char *)*a);

> this would change the data type of the value a points to to a char data type?(i
> know that this is not needed i did this just for example purposes) and does it
> change it to the char data type for the rest of the program or just for that
> function?

> thanks!

> -art

    What (char *) *a is going to do is take the value pointed to by a and treat it
as though it were a pointer to a character.  The actual data type doesn't get
changed.  Only the way it is treated at that particular time.

Christopher Lansing



Thu, 24 Jul 2003 10:17:47 GMT  
 printf() and scanf()


Quote:
>ok so say i did this:

>int *a;
>[snip]
>printf("%c, (char *)*a);

>this would change the data type of the value a points to to a char data type?

No, it would only convert the value of the int that a points to to
a pointer to char.  (Casts work the same way as other operators;
a+1 would give you a pointer to the next int past the one a points to
without changing a.)  It would then pass the pointer to char to printf.
You probably don't want to cast an int to a pointer, especially not here
since %c expects a char; what you're looking for here is
printf("%c",(char)*a);

Quote:
>   (i
>know that this is not needed i did this just for example purposes) and does it
>change it to the char data type for the rest of the program or just for that
>function?

A cast returns the value that is the result of converting the value
you're casting to whatever type you're casting it to; it doesn't change
any data types, it only converts values.

dave

--

Absolutely right. Actually telling people they're in the wrong newsgroup for
their question would contravene their constitutional right to be ignorant.
                          --Richard Heathfield roasts a troll in comp.lang.c



Thu, 24 Jul 2003 10:27:10 GMT  
 printf() and scanf()

Quote:





>>>>printf("%p",(void *)a);

>>>this make perfect sense to me except for one part:

>>>(void *)

>>>i gather you can put any data type such as (char *) or (int *)
>>>but hwat does this do and why is it needed?

>>%p requires a pointer. void* is a generic pointer. So you're casting a
>>to a pointer.

>In this case, a was already a pointer (to int),

I wondered if that might be the case, but too much context was snipped
unfortunately.

Quote:
>but %p specifically
>requires a pointer to void, and since printf is a variadic function[1]
>the conversion to (void *) needs to be done explicitly.

--
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>


Thu, 24 Jul 2003 20:56:59 GMT  
 printf() and scanf()
GLOBAL LINK 2001 a crit dans le message

Quote:
>i was wondering i know that with scanf() you are supposed to pass it the
>address in memory where you want the value assigned like if it was a
variable:

>int a;
>[snip]
>scanf("%d", &a);

If you insist to use scanf() (which is really a bad idea, as you fell not to
understand), at last do it proberly, and check the return value.

Quote:
>and if it where all ready a pointer:

>int *a;
>[snip]
>scanf("%d", a);

Wrong. The pointer must point to something valid.

int b;
int *a = &b;
   scanf("%d", a);

Quote:
>but what about printf()?
>normally you just pass it the name of the variable:

>int a;
>[snip]
>printf("%d", a);

Yes, assuming a holds a valid value and not a trap representation. Also, for
portability reasons, better to finish lines with '\n'

   printf("%d\n", a);

Quote:
>so does that mean when working with a you do:

>int *a;
>[snip]
>printf("%d", *a);

Horribly wrong. Neither a nor *a are initialized.

Quote:
>or:

>int *a;
>[snip]
>printf("%d", a);

Horribly wrong again. a is still not initialized. If you want to printf() a
valid pointer, use "%p" and the (void*) cast.

int b;
int *a = &b;
   printf("%p\n", (void*)a);

Quote:
>i have heard that scanf() is just the complete opposite of printf() but i

did

You heard badly. Don't learn by rumors. Better to understand the C-language.

Quote:
>not take it literally! does this mean that you pass printf() the actual
value
>and that you pass scanf() the address in memory? are they really truely
>opposite like that?

Just read a good C-reference, and comply with the definition of the
functions:
http://www.dinkum.com/htm_cl/lib_prin.html
http://www.dinkum.com/htm_cl/lib_scan.html

Quote:
>note: if they really are opposite like that thsi would be the right one:

What opposite? They are different. Period.

Quote:
>int *a;
>[snip]
>printf("%d", *a);

>but i do not know

You should know that dereferencing a non-initialized pointer is a Bad Thing.

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Thu, 24 Jul 2003 21:25:09 GMT  
 printf() and scanf()

Quote:

> Also, for portability reasons, better to finish lines with '\n'

>    printf("%d\n", a);

I do not understand this advice. Portability with what?
Please elaborate.

willem



Thu, 24 Jul 2003 21:49:40 GMT  
 printf() and scanf()

Quote:


> > Also, for portability reasons, better to finish lines with '\n'

> >    printf("%d\n", a);

> I do not understand this advice. Portability with what?
> Please elaborate.

A text stream is composted of lines, a line is terminated by '\n'.

It is implementation-defined if the last line need a terminating '\n' or
not.

--
Tor <torust AT online DOT no>



Thu, 24 Jul 2003 22:37:09 GMT  
 printf() and scanf()

Quote:


> > Also, for portability reasons, better to finish lines with '\n'

> >    printf("%d\n", a);

> I do not understand this advice. Portability with what?
> Please elaborate.

From 7.19.3:

When a stream is line buffered, characters are intended to be
transmitted to or from the host environment as a block when a new-line
character is encountered.

If you want to guarantee that your prompt will appear before your
program starts blocking for input, the newline (or an fflush) is a good
plan.

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Thu, 24 Jul 2003 22:50:18 GMT  
 printf() and scanf()

Quote:



> > > Also, for portability reasons, better to finish lines with '\n'

> > >    printf("%d\n", a);

> > I do not understand this advice. Portability with what?
> > Please elaborate.

> When a stream is line buffered, characters are intended to be
> transmitted to or from the host environment as a block when a
> new-line character is encountered.

> If you want to guarantee that your prompt will appear before your
> program starts blocking for input, the newline (or an fflush) is a
> good plan.

printf("\nHey, Richard");
printf("\nSee, I can count to ten too:");
for (i = 0; i < 10; i++) printf("%3d", i);
printf("\nI do not always want \n, because that breaks up your line");
printf("\nSo what is wrong with this syntax?"
       "\nEven though it has no linefeeds at the end ..");
printf("\nwillem (you'll even see this when I exit or even sooner :)";


Thu, 24 Jul 2003 23:26:38 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Beginner question - printf and scanf

2. Q:printf() and scanf()....

3. printf and scanf in IBMC on IBM 3090

4. printf and scanf help!

5. printf and scanf of long doubles

6. Details about printf() and scanf()!

7. printf and scanf woes...

8. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

9. Simple scanf/printf question

10. skipped printf/scanf ???

11. tiny question on format of scanf and printf

12. Examples of format strings for scanf/printf ?

 

 
Powered by phpBB® Forum Software