I don't quite get the ampersand '&' as in what is its function in the scanf/printf functions.
In the below code shows the prinf function printing out the char variable c without the ampersand.
The next code is the same as the above code but instead has the ampersand in the printf function.Code:# include <stdio.h> void main(void) { char c='c'; printf("The character is %c", c); // this prints 'c' return; }
Another code shown below stores and prints a string but without the ampersand in both the printf and the scanf.Code:# include <stdio.h> void main(void) { char c='c'; printf("The character is %c", &c); // this prints an upside down triangle return; }
This code has the ampersand in the scanf.Code:# include <stdio.h> void main(void) { char string[10]; scanf("%s", string); // Let's say I input 'Hello!' printf("Message: %s", string); // this prints 'Hello!' return; }
When printing strings, whether the ampersand is present or not doesn't make a difference. And when dealing with char and int variables, the presence of the ampersand makes a difference. So I don't really get the ampersand '&' and would gladly appreciate it if someone could explain it to meCode:# include <stdio.h> void main(void) { char string[10]; scanf("%s", &string); // Let's say I input 'Hello!' printf("Message: %s", string); // this prints 'Hello!' return; }![]()



1Likes
LinkBack URL
About LinkBacks



