C भाषा में, char टाइप वेरिएबल को int में बदलने के तीन तरीके हैं। ये निम्नलिखित हैं:
- sscanf()
- atoi()
- टाइपकास्टिंग
C में उदाहरण
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str = "12345";
char c = 's';
int x, y, z;
sscanf(str, "%d", &x); // sscanf का उपयोग
printf("\nx का मान: %d", x);
y = atoi(str); // atoi() का उपयोग
printf("\ny का मान: %d", y);
z = (int)(c); // टाइपकास्टिंग का उपयोग
printf("\nz का मान: %d", z);
return 0;
}
आउटपुट
x का मान: 12345 y का मान: 12345 z का मान: 115
C++ भाषा में, char टाइप वेरिएबल को int में बदलने के दो तरीके हैं:
- stoi()
- टाइपकास्टिंग
C++ में उदाहरण
#include <iostream>
#include <string>
using namespace std;
int main() {
char s1[] = "45";
char c = 's';
int x = stoi(s1);
cout << "x का मान: " << x;
int y = (int)(c);
cout << "\ny का मान: " << y;
return 0;
}
आउटपुट
x का मान: 45 y का मान: 115