Guide on C Program to Determine the ASCII Value of a Character

Finding the ASCII value of a character is a basic yet fundamental operation in C programming. ASCII values are numerical representations of characters that computers use to store text. This article provides a detailed exploration of various methods to find the ASCII value of a character in C. We’ll cover multiple examples with different solutions and outputs. The explanations will be clear and structured with necessary headings.

Prerequisites

Before diving into the examples, ensure you have the following:

  • Basic understanding of C programming.
  • Familiarity with data types and variable declarations in C.
  • Knowledge of basic input and output functions in C (printf and scanf).

1. Finding ASCII Value of a Character Using Direct Input

1.1 Explanation

This method involves directly taking a character input from the user and printing its ASCII value using the printf function.

1.2 Program: Direct Input Method

C
#include <stdio.h>

int main() {
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);

    printf("ASCII value of %c is %d\n", ch, ch);

    return 0;
}

1.3 Output

C
Enter a character: A
ASCII value of A is 65

2. Finding ASCII Value of a Character Using Function

2.1 Explanation

This method uses a function to find the ASCII value of a character. The function takes a character as an argument and returns its ASCII value.

2.2 Program: Using a Function

C
#include <stdio.h>

int getAsciiValue(char ch) {
    return ch;
}

int main() {
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);

    printf("ASCII value of %c is %d\n", ch, getAsciiValue(ch));

    return 0;
}

2.3 Output

C
Enter a character: b
ASCII value of b is 98

3. Finding ASCII Value of a Character from a String

3.1 Explanation

In this method, we will take a string input from the user and print the ASCII values of all characters in the string.

3.2 Program: ASCII Values of Characters in a String

C
#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    gets(str);

    for(int i = 0; i < strlen(str); i++) {
        printf("ASCII value of %c is %d\n", str[i], str[i]);
    }

    return 0;
}

3.3 Output

C
Enter a string: Hello
ASCII value of H is 72
ASCII value of e is 101
ASCII value of l is 108
ASCII value of l is 108
ASCII value of o is 111

4. Finding ASCII Value of a Character Using Pointers

4.1 Explanation

This method involves using pointers to find the ASCII value of a character. The pointer points to the character variable and the ASCII value is accessed using pointer dereferencing.

4.2 Program: Using Pointers

C
#include <stdio.h>

int main() {
    char ch;
    char *p;
    printf("Enter a character: ");
    scanf("%c", &ch);

    p = &ch;

    printf("ASCII value of %c is %d\n", *p, *p);

    return 0;
}

4.3 Output

C
Enter a character: Z
ASCII value of Z is 90

5. Finding ASCII Value of a Character Using Macros

5.1 Explanation

In this method, a macro is defined to find the ASCII value of a character. This macro can be reused throughout the program.

5.2 Program: Using Macros

C
#include <stdio.h>

#define ASCII_VALUE(c) ((int)c)

int main() {
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);

    printf("ASCII value of %c is %d\n", ch, ASCII_VALUE(ch));

    return 0;
}

5.3 Output

C
Enter a character: k
ASCII value of k is 107

Conclusion

This article covered various methods to find the ASCII value of a character in C, including direct input, using functions, extracting from strings, using pointers, and employing macros. Each method offers a unique approach to achieving the same goal, demonstrating the versatility of C programming. Understanding these methods will help you choose the most appropriate one for your specific needs and improve your overall programming skills in C.