scanf() requires the address of the variable it will change. printf() does not.
& is the address of operator.
Adak
Posting Virtuoso
1,640 posts since Jun 2008
Reputation Points: 456
Solved Threads: 196
Skill Endorsements: 7
You will never use & in printf() unless you plan on printing out raw memory addresses, which you will almost never do.
Never say never. ;) printf() supports a %n specifier that assigns the number of characters written up to that point:
#include <stdio.h>
int main(void)
{
int n = 0;
#ifdef _MSC_VER
// Visual Studio disables %n by default and only provides a runtime way to enable it...
_set_printf_count_output(1);
#endif
printf("Printing 22 characters%n\n", &n);
printf("Printed %d characters\n", n);
return 0;
}
However, that specifier is viewed as a security risk, and some compilers (notable is Visual Studio, as in the example) will disable it.
A string esssentially is a memory address that points to the first character in a group of characters
Noting, of course, that this is a conversion that happens implicitly when arrays are used in value context and it's important to recognize that strings in general aren't inherently pointers.
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
except what deceptikon posted :(
Sadly, that happens a lot. Today at work I was getting frustrated because a client just didn't seem to grok my explanations about how macro translation in some of our software's text areas worked... :(
Like I said, you will never use them (as in mattboy64). ;)
Yeah...want to know how many times I was told I'd never use something when I was a beginner that I use often now? ;)
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57