Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the ASCII value of the uppercase character 'A' using implicit conversion in C language?
Implicit type conversion occurs when the compiler automatically converts a smaller data type to a larger data type without explicit casting. In C, when a char is used in an arithmetic operation, it gets implicitly converted to an int, allowing us to access its ASCII value.
Syntax
int ascii_value = character + 0; // Implicit conversion from char to int
Example 1: ASCII Value of 'A'
The following example demonstrates finding the ASCII value of uppercase character 'A' using implicit conversion −
#include <stdio.h>
int main() {
char character = 'A';
int number = 0, value;
value = character + number; // implicit conversion
printf("The ASCII value of A is: %d<br>", value);
return 0;
}
The ASCII value of A is: 65
In this example, when character + number is evaluated, the char 'A' is implicitly converted to its ASCII value 65, and the expression becomes 65 + 0 = 65.
Example 2: ASCII Value of 'P'
Let us consider another example with a different character to see the implicit conversion in action −
#include <stdio.h>
int main() {
char character = 'P';
int number = 0, value;
value = character + number; // implicit conversion
printf("The ASCII value of P is: %d<br>", value);
return 0;
}
The ASCII value of P is: 80
Here, the character 'P' gets implicitly converted to its ASCII value 80, making the expression 80 + 0 = 80.
How It Works
During implicit conversion, the compiler promotes the char data type to int when it participates in arithmetic operations. This automatic promotion allows us to retrieve the ASCII value without explicit casting.
Conclusion
Implicit type conversion in C automatically converts characters to their ASCII values during arithmetic operations. This feature provides a simple way to access ASCII values without explicit casting, making the code cleaner and more readable.
