# C++: error: narrowing conversion of 'x' from 'int' to 'char' inside { } [-Wnarrowing]

**Code:** `narrowing conversion` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

Brace initialisation `{}` refuses conversions that can lose data, such as `double` to `int` or a runtime `int` to `char`.

## Meaning

List-initialisation was designed to catch lossy conversions. Constants that fit are allowed (`char c{65};`), but a variable or an out-of-range literal is an error in C++11 and later. Use the right type, or make the conversion explicit with `static_cast` when you really want it.

## Common causes

- Initialising a smaller type from a variable with braces (`int` to `char`, `long` to `int`)
- Braced initialisation of an integer from a floating-point value
- Value outside the target type's range
- Signed to unsigned conversion of a negative or non-constant value in `{}`

## Quick fix

1. Use the matching type (`int` instead of `char`)
2. Add `static_cast<T>(x)` where narrowing is intended
3. Use `=` initialisation only if you accept silent truncation (not recommended)

## Detailed fix by platform

### C++

1. `char c{static_cast<char>(n)};` makes the intent explicit
2. Prefer `auto` or the exact type returned by the function you call

## Code examples

### Braces catch lossy conversions

```cpp
int n = 300;
// char c{n};                       // error: narrowing conversion of 'n' from 'int' to 'char'
char c{static_cast<char>(n)};       // OK, truncation is explicit (value 44)

double d = 3.9;
// int i{d};                        // error: narrowing conversion
int i = static_cast<int>(d);        // 3
```

## How to diagnose

1. **Types** — Source and target types in the message?
2. **Constant** — Is the value a compile-time constant that fits?
3. **Intent** — Is truncation really wanted?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/narrowing-conversion)
