Files
web/content/notes/c-snippets.md

39 lines
856 B
Markdown

---
tags:
- notes
- programming
- c
author: TrudeEH
draft: false
showToc: true
title: C Snippets
---
## Cast Strings to Numbers
The `atoi()` function in `stdlib` has a similar implementation to the one below.
ASCII encodes numbers in order, after special characters.
The encoded value for `'0'` is 48, so subtracting any numeric char by 48 outputs its real numerical value.
```C
char number = '7';
int result = number - 48;
int same_result = number - '0';
```
Algorithm to convert strings to numbers:
```C
int str_to_int(char *str) {
int result = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] < '0' && str[1] > '9') return -1; // Error if NaN
result = (result * 10) + (str[i] - '0');
return result;
}
```
`(result * 10)` is shifting the previous number to the left, as it is an order of magnitude above the following digit.