Skip to content

Commit f33a271

Browse files
docs(program): Add hello program with colored output one also
Signed-off-by: Vaishnav Sabari Girish <vaishnav.sabari.girish@gmail.com>
1 parent 7d61371 commit f33a271

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

src/getting-started/hello-world.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,91 @@
11
# Your First Program
2+
3+
Now that your environment is set up and the Rust backend is compiled, let's
4+
write your first memory-safe C program using NextStd.
5+
6+
We are going to replace the traditional, unsafe `printf` function with NextStd's
7+
type-safe `ns_println` macro.
8+
9+
## The Basic Program
10+
11+
If you are working inside the cloned repository, create a new file named
12+
`hello.c` inside the `examples/` directory.
13+
14+
Add the following code to print basic text and variables:
15+
16+
```c
17+
#include "../include/ns.h"
18+
19+
int main() {
20+
// 1. Printing a standard string
21+
ns_println("Hello, World! Welcome to NextStd.");
22+
23+
// 2. Printing an integer safely without format specifiers
24+
int version = 1;
25+
ns_print("NextStd Version: ");
26+
ns_println(version);
27+
28+
// 3. Printing a floating-point number
29+
double pi = 3.14159;
30+
ns_print("Value of Pi: ");
31+
ns_println(pi);
32+
33+
return 0;
34+
}
35+
```
36+
37+
To compile and execute this program, open your terminal in the root of the
38+
`NextStd` repository and run:
39+
40+
```bash
41+
make hello
42+
```
43+
44+
## Adding Terminal Colors
45+
46+
NextStd also provides a dedicated, cross-platform color module to help you build
47+
beautiful CLI tools. You don't need to remember ANSI escape codes; you just
48+
include the header and use the macros.
49+
50+
Create a second file named `hello_color.c` in your `examples/` directory:
51+
52+
```c
53+
#include "../include/ns.h"
54+
#include "../include/ns_color.h" // Import the color macros
55+
56+
int main() {
57+
// Printing with a specific color and resetting it afterward
58+
ns_println(NS_COLOR_GREEN "Success: System initialized safely." NS_COLOR_RESET);
59+
60+
// Combining styles like bold text with colors
61+
ns_println(NS_COLOR_CYAN NS_COLOR_BOLD "NextStd is running..." NS_COLOR_RESET);
62+
63+
// Warning and Error colors
64+
ns_println(NS_COLOR_YELLOW "Warning: Low memory." NS_COLOR_RESET);
65+
ns_println(NS_COLOR_RED "Error: Connection lost." NS_COLOR_RESET);
66+
67+
return 0;
68+
}
69+
```
70+
71+
Compile and run this exactly like the first one:
72+
73+
```bash
74+
make hello_color
75+
```
76+
77+
## How It Works
78+
79+
If you are coming from standard C, the code above might look like magic. How
80+
does `ns_println` know whether to print a string, an integer, or a double
81+
without you typing `%s`, `%d`, or `%f`?
82+
83+
NextStd leverages C11's `_Generic` keyword to inspect the type of the variable
84+
at **compile time**. It then automatically routes your data to the correct,
85+
memory-safe Rust backend function (e.g., `ns_print_int`, `ns_print_double`, or
86+
`ns_print_str`).
87+
88+
Because there are no format strings, there is zero risk of mismatched types
89+
causing undefined behavior or reading garbage memory from the stack. If you try
90+
to print a type that NextStd doesn't support yet, the compiler will safely throw
91+
an error before the program ever runs.

0 commit comments

Comments
 (0)