C/Console/Console Output Char

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

Output the char"s ASCII code

#include <conio.h>
#include <stdio.h>
int main(void)
{
  char ch;
  printf("Enter a char: ");
  ch = getchar();
  printf("\n Its ASCII code is %d", ch);
  return 0;
}


Simple printf demo

  
#include <stdio.h>
int main(void)
{
  printf("a");
  printf("b");
  printf("c");
  return 0;
}


Use getchar() to read user choices

  
#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;
}


Use getche() to receive user selection

  
#include <conio.h>
#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;
}