Skip to content

Instantly share code, notes, and snippets.

@dukenmarga
Forked from duken-blog/char2int.c
Last active April 2, 2020 11:39
Show Gist options
  • Save dukenmarga/699e889fb1eeabad5f84902cfbcf45d2 to your computer and use it in GitHub Desktop.
Save dukenmarga/699e889fb1eeabad5f84902cfbcf45d2 to your computer and use it in GitHub Desktop.
Convert Character to Integer (C Language)
#include <stdio.h>
int main(){
char c[5] = "23452";
char d[5] = "23452";
int result=0, factor=3;
int numeric;
// take and convert 1 character from an array
numeric = c[2] - '0'; // --> 4
result = factor*numeric; // result=12
printf("Result : %d\n", result);
// convert array of char to int
numeric = atoi(d); // --> 23452
result = factor*numeric; // result = 70356
printf("Result : %d\n", result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment