Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 13 Online
OpenStudy (anonymous):

please explain this piece of code #include #include int main () { int ch; ch=getch(); printf("%d",ch); printf (" %d", getch ()); return 0; }

OpenStudy (anonymous):

whoever explains me this to my satisfaction level gets a gold medal

OpenStudy (anonymous):

int ch; \\ just declaring int variable ch=getch(); \\ means read any single character from the keyboard but that character will not be seen in the screen . printf("%d",ch); \\ basically print the integer value of ch if there's integer in ch %d otherwise nothing . printf (" %d", getch ()); \\ I guess <I'm not sure> read again from the keyboard and if it's integer print it otherwise nothing

OpenStudy (anonymous):

%d means print the integer in the string that in the second location in the printf function

OpenStudy (anonymous):

printf (" %d", getch ()); \\ its not reading again from the keyboard instead its displaying some number. run it and see

OpenStudy (anonymous):

look at first ch=getch(); will take anycharacter from keyborad and put it in ch (converted to it ascii code) so when you write for ex. in first getch() A from keyborad it will convert to it ascii code which is 65 http://www.asciitable.com/ when u write 5 for ex. will be 53 (ascii code for 5 ) u get it ?

OpenStudy (unklerhaukus):

``` /* scode.c a * converts the first two charecters of input to ASCII code numbers * Example: ./scode Aa --> 65 97 */ //#include <conio.h> // console input-output library [only for Windows] #include <cs50.h> // computer science 50 library [has getchar instead of getch] #include <stdio.h> // standard input-output library int main() { int ch; // declares an integer 'ch' ch = getchar(); // sets 'ch' to the first charecter typed by user printf("%d", ch); // prints the integer that 'ch' corresponds in ASCII code printf(" %d", getchar()); // prints a space then the integer for the second charecter typed return 0; // quits } ``` for reference ASCII code

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!