C/Console/Console Output Char

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Output the char"s ASCII code

<source lang="cpp">

  1. include <conio.h>
  2. include <stdio.h>

int main(void) {

 char ch;
 printf("Enter a char: ");
 ch = getchar();
 printf("\n Its ASCII code is %d", ch);
 return 0;

}


      </source>


Simple printf demo

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 printf("a");
 printf("b");
 printf("c");
 return 0;

}

      </source>


Use getchar() to read user choices

<source lang="cpp">

  1. include <stdio.h>

int main(void) {

 int a, b;
 char ch;
 printf("Choice:\n");
 printf("Add, Subtract, Multiply, or Divide?\n");
 printf("Enter first letter: ");
 ch = getchar();
 printf("\n");
 printf("Enter a: ");
 scanf("%d", &a);
 printf("Enter b: ");
 scanf("%d", &b);
 if(ch=="A") printf("%d", a+b);
 if(ch=="S") printf("%d", a-b);
 if(ch=="M") printf("%d", a*b);
 if(ch=="D" && b!=0) printf("%d", a/b);
 return 0;

}

      </source>


Use getche() to receive user selection

<source lang="cpp">

  1. include <conio.h>
  2. include <stdio.h>

int main(void) {

 char ch;
 printf("Do you wish to continue? (Y/N : ");
 ch = getche();
 
 if(ch=="Y") {
  /* continue with something */
 
 }
 return 0;

}

      </source>