Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asc and chr equivalent in C/C++

Tags:

c++

c

chr

Well the title pretty much sums it up. I want to use something like asc("0") in C++, and want to make the program platform independent so don't want to use 48! Any help appreciated.

like image 562
Appster Avatar asked Dec 17 '22 12:12

Appster


1 Answers

You can simply use single-quotes to make a character constant:

char c = 'a';

The character type is a numeric type, so there is no real need for asc and chr equivalents.

Here's a small example that prints out the character values of a string:

#include <stdio.h>

int main(int argc, char **argv) {
  char str[] ="Hello, World!";

  printf("string = \"%s\"\n", str);

  printf("chars = ");
  for (int i=0; str[i] != 0; i++) 
    printf("%d ", str[i]);
  printf("\n");

  return 0;
}

The output is:

string = "Hello, World!"
chars = 72 101 108 108 111 44 32 87 111 114 108 100 33 
like image 108
nibot Avatar answered Dec 19 '22 03:12

nibot