C 언어에서는 char 유형 변수를 int로 변환하는 세 가지 방법이 있습니다. 이들은 다음과 같이 주어집니다 -
- sscanf()
- 아토이()
- 타이프캐스팅
다음은 C 언어에서 char를 int로 변환하는 예입니다.
예시
#include<stdio.h>
#include<stdlib.h>
int main() {
const char *str = "12345";
char c = 's';
int x, y, z;
sscanf(str, "%d", &x); // Using sscanf
printf("\nThe value of x : %d", x);
y = atoi(str); // Using atoi()
printf("\nThe value of y : %d", y);
z = (int)(c); // Using typecasting
printf("\nThe value of z : %d", z);
return 0;
} 출력
출력은 다음과 같습니다.
The value of x : 12345 The value of y : 12345 The value of z : 115
C++ 언어에서 char형 변수를 int −
로 변환하는 두 가지 방법이 있습니다.- 스토이()
- 타이프캐스팅
다음은 C++ 언어에서 char를 int로 변환하는 예입니다.
예시
#include <iostream>
#include <string>
using namespace std;
int main() {
char s1[] = "45";
char c = 's';
int x = stoi(s1);
cout << "The value of x : " << x;
int y = (int)(c);
cout << "\nThe value of y : " << y;
return 0;
} 출력
다음은 출력입니다.
The value of x : 45 The value of y : 115