Char Type Introduction - C Language Basics

C examples for Language Basics:Variable

Introduction

The char type represents ASCII characters.

The character constants are enclosed in single quotes.

Demo Code

#include <stdio.h>

int main() {/*from   w  w w  .  j  a  v a 2 s .co  m*/
    char c = 'x'; /* assigns 120 (ASCII for x) */
}

You can use the %c format specifier to print out the ASCII character.

Demo Code

#include <stdio.h>

int main() {/*from ww  w .ja  v  a2s.  co  m*/
    char c = 'x'; /* assigns 120 (ASCII for x) */
    printf("%c", c); /* "x" */
}

Result

Use the %d specifier to instead display the numerical value.

Demo Code

#include <stdio.h>

int main() {//from   w w  w. j  av  a2s  .  c o m
    char c = 'x'; /* assigns 120 (ASCII for x) */

    printf("%d", c); /* "120" */
}

Result


Related Tutorials