Ceriwis  

Go Back   Ceriwis > HOBI > Komputer & Teknologi > Programming

Programming Share, tanya jawab, saling bantu antar programmer dengan berbagai macam bahasa pemrograman.

Reply
 
Thread Tools
  #1  
Old 11th November 2011
Braincode's Avatar
Braincode Braincode is offline
Ceriwis Addicted
 
Join Date: Nov 2011
Posts: 4,638
Rep Power: 20
Braincode mempunyai hidup yang Normal
Default [Ask] Ampersand "&" di C

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.



Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


 


All times are GMT +7. The time now is 04:54 AM.


no new posts