Skip to content

Instantly share code, notes, and snippets.

@shamiul94
Created February 20, 2023 22:34
Show Gist options
  • Save shamiul94/02d4957f410b138ef8aca6b7602d12b6 to your computer and use it in GitHub Desktop.
Save shamiul94/02d4957f410b138ef8aca6b7602d12b6 to your computer and use it in GitHub Desktop.
// C Program to convert string
// into integer using for loop
#include <stdio.h>
#include <string.h>
int main()
{
char* str = "4213\0";
int num = 0;
// converting string to number
for (int i = 0; str[i] != '\0'; i++) {
num = num * 10 + (str[i] - 48);
#num = 0 * 10 + (52 - 48) = 0 + 4 = 4
#num = 4 * 10 + (50 - 48) = 40 + 2 = 42
#num = 42 * 10 + (49 - 48) = 420 + 1 = 421
#num = 421 * 10 + (51 - 48) = 4210 + 3 = 4213
}
// at this point num contains the converted number
printf("%d\n", num);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment