C भाषा में, चार प्रकार के चर को 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++ भाषा में, चार प्रकार के चर को एक 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