please explain this piece of code
#include
whoever explains me this to my satisfaction level gets a gold medal
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
%d means print the integer in the string that in the second location in the printf function
printf (" %d", getch ()); \\ its not reading again from the keyboard instead its displaying some number. run it and see
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 ?
``` /* 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
Join our real-time social learning platform and learn together with your friends!