0
# C Functions
1
2
C standard library functions ported to JavaScript. This module contains 3 core functions focusing on mathematical operations and formatted output.
3
4
## Capabilities
5
6
### Math Functions (2 functions)
7
8
Core mathematical functions from the C standard library.
9
10
```javascript { .api }
11
/**
12
* C math functions
13
*/
14
c.math.abs(mixed_number) // Absolute value of a number
15
c.math.frexp(arg) // Extract mantissa and exponent from floating-point value
16
```
17
18
**Usage Examples:**
19
20
```javascript
21
const locutus = require('locutus');
22
23
// Absolute value
24
const result1 = locutus.c.math.abs(-42); // 42
25
const result2 = locutus.c.math.abs(-3.14); // 3.14
26
const result3 = locutus.c.math.abs(15); // 15
27
28
// Extract mantissa and exponent
29
const frexpResult = locutus.c.math.frexp(8.0);
30
// Returns array: [0.5, 4] where 0.5 * 2^4 = 8.0
31
```
32
33
### Standard I/O Functions (1 function)
34
35
Formatted string output function from C standard library.
36
37
```javascript { .api }
38
/**
39
* C standard I/O function
40
*/
41
c.stdio.sprintf(format, ...args) // Create formatted string
42
```
43
44
**Usage Examples:**
45
46
```javascript
47
const locutus = require('locutus');
48
49
// Format string with various types
50
const formatted = locutus.c.stdio.sprintf(
51
'Hello %s, you have %d messages and %.2f%% completion',
52
'John',
53
5,
54
87.5
55
);
56
// Result: 'Hello John, you have 5 messages and 87.50% completion'
57
58
// Integer formatting
59
const intFormatted = locutus.c.stdio.sprintf('%d %04d %x', 42, 42, 42);
60
// Result: '42 0042 2a'
61
62
// Float formatting
63
const floatFormatted = locutus.c.stdio.sprintf('%.2f %.4f', 3.14159, 3.14159);
64
// Result: '3.14 3.1416'
65
```
66
67
## Function Details
68
69
### abs(mixed_number)
70
71
Returns the absolute value of a number.
72
73
```javascript { .api }
74
/**
75
* Return the absolute value of a number
76
* @param {number} mixed_number - The numeric value to process
77
* @returns {number} The absolute value
78
*/
79
c.math.abs(mixed_number)
80
```
81
82
**Parameters:**
83
- `mixed_number` (number): The numeric value to get absolute value of
84
85
**Returns:**
86
- (number): The absolute value of the input
87
88
### frexp(arg)
89
90
Breaks a floating-point value into a normalized fraction and an integral power of 2.
91
92
```javascript { .api }
93
/**
94
* Extract mantissa and exponent from floating-point value
95
* @param {number} arg - The floating-point value
96
* @returns {Array} Array containing [mantissa, exponent]
97
*/
98
c.math.frexp(arg)
99
```
100
101
**Parameters:**
102
- `arg` (number): The floating-point value to break down
103
104
**Returns:**
105
- (Array): Two-element array `[mantissa, exponent]` where `mantissa * 2^exponent = arg`
106
107
### sprintf(format, ...args)
108
109
Returns a formatted string according to the given format string.
110
111
```javascript { .api }
112
/**
113
* Return a formatted string
114
* @param {string} format - The format string
115
* @param {...any} args - Values to substitute into format string
116
* @returns {string} The formatted string
117
*/
118
c.stdio.sprintf(format, ...args)
119
```
120
121
**Parameters:**
122
- `format` (string): Format string with conversion specifications
123
- `...args` (any): Values to be formatted and inserted
124
125
**Returns:**
126
- (string): The formatted string
127
128
**Format Specifiers:**
129
- `%d` - Integer
130
- `%f` - Float
131
- `%s` - String
132
- `%x` - Hexadecimal (lowercase)
133
- `%X` - Hexadecimal (uppercase)
134
- `%.nf` - Float with n decimal places
135
- `%0nd` - Integer padded with zeros to n digits
136
137
## Import Patterns
138
139
```javascript
140
// Full module access
141
const locutus = require('locutus');
142
locutus.c.math.abs(-42);
143
locutus.c.stdio.sprintf('%d', 42);
144
145
// Category-specific import (not available for C module)
146
// Individual function import
147
const abs = require('locutus/c/math/abs');
148
const sprintf = require('locutus/c/stdio/sprintf');
149
150
abs(-42); // 42
151
sprintf('%d', 42); // '42'
152
```