Braincode
11th November 2011, 02:17 PM
gan ane lg belajar C language dan ane dah googling tp msh bingung mengenai ampersand ini "&"
Code:
#include
int main()
{
char name[20];
char color[20];
printf(�What is your name?�);
scanf(�%s�,name);
printf(�What is your favorite color?�);
scanf(�%s�,color);
printf(�%s�s favorite color is %s\n�,name,color);
return(0);
}
tuh yang di atas kok ga usa pake &name atau &color ,outputnya ok2 aja tapi utk code di bawah ini harus pake , kalo ga pake output printf-nya ga muncul.
Code:
#include
int main()
{
char key;
puts(�Type your favorite keyboard character:�);
scanf(�%c�,&key);
printf(�Your favorite character is %c!\n�,key);
return(0);
}
Dan ini hasil googling ane.
Spoiler for buka:
scanf() must have the address of the variable to store input into. This means that often the ampersand address operator is required to compute the addresses. Here's an example:
int x;
char * st = malloc(31);
scanf("%d", &x); /* & required to pass address to scanf() */
scanf("%30s", st); /* NO & here, st itself points to variable! */
As the last line above shows, sometimes no ampersand is correct!
maksudnya "& required to pass address to scanf()" itu kalo "x" nya ga ngepoint ke variable harus pake "&" ???
dan yang "scanf("%30s", st);" katanya no & here karena itu ngepoint ke variable dan di kode kedua diatas jg nge point ke variable "key" tetapi tanpa & malah ga jalan printf-nya.
Mohon dibantu para sesepuh . Thanx.
</div>
Code:
#include
int main()
{
char name[20];
char color[20];
printf(�What is your name?�);
scanf(�%s�,name);
printf(�What is your favorite color?�);
scanf(�%s�,color);
printf(�%s�s favorite color is %s\n�,name,color);
return(0);
}
tuh yang di atas kok ga usa pake &name atau &color ,outputnya ok2 aja tapi utk code di bawah ini harus pake , kalo ga pake output printf-nya ga muncul.
Code:
#include
int main()
{
char key;
puts(�Type your favorite keyboard character:�);
scanf(�%c�,&key);
printf(�Your favorite character is %c!\n�,key);
return(0);
}
Dan ini hasil googling ane.
Spoiler for buka:
scanf() must have the address of the variable to store input into. This means that often the ampersand address operator is required to compute the addresses. Here's an example:
int x;
char * st = malloc(31);
scanf("%d", &x); /* & required to pass address to scanf() */
scanf("%30s", st); /* NO & here, st itself points to variable! */
As the last line above shows, sometimes no ampersand is correct!
maksudnya "& required to pass address to scanf()" itu kalo "x" nya ga ngepoint ke variable harus pake "&" ???
dan yang "scanf("%30s", st);" katanya no & here karena itu ngepoint ke variable dan di kode kedua diatas jg nge point ke variable "key" tetapi tanpa & malah ga jalan printf-nya.
Mohon dibantu para sesepuh . Thanx.
</div>