# String concatenation

**URL:** <https://verificationacademy.com/forums/t/string-concatenation/41527>\
**Category:** SystemVerilog\
**Tags:** string-concatenation, SystemVerilog\
**Created:** [April 15, 2023, 7:29am UTC](https://verificationacademy.com/forums/t/string-concatenation/41527 "2023-04-15T07:29:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jianfeng.he](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/e47774/32.png) [@jianfeng.he](https://verificationacademy.com/forums/u/jianfeng.he)\
**Post date:** [April 15, 2023, 7:29am UTC](https://verificationacademy.com/forums/t/string-concatenation/41527/1 "2023-04-15T07:29:21Z")

</div>

Can you please tell me why comb1 gives an error in the following code, while comb2 does not? Thank you!

```verilog

module top;
  string s1 = "hello";
  string s2 = "world";
  int num = 65;
  string comb1 = {s1, " ", s2, num};
  //string comb2 = {"hello", " ", "world", num};
  initial begin
    $display("%0s",comb1);
    //$display("%0s",comb2);
  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![sbellock](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sbellock](https://verificationacademy.com/forums/u/sbellock)\
**Post date:** [April 16, 2023, 1:52pm UTC](https://verificationacademy.com/forums/t/string-concatenation/41527/2 "2023-04-16T13:52:20Z")

</div>

_In reply to [jianfeng.he](https://verificationacademy.com/forums/t/string-concatenation/41527):_

comb1 works for me.

> # hello worldA

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 16, 2023, 3:05pm UTC](https://verificationacademy.com/forums/t/string-concatenation/41527/3 "2023-04-16T15:05:55Z")

</div>

_In reply to [sbellock](https://verificationacademy.com/forums/t/string-concatenation/41527/2):_

Technically, they are both illegal according to the LRM. A concatenation must be one of

- All integral expressions, resulting in an integral type
- All string types or string literals, resulting
- An unpacked array concatenation in the context of an assignment to an unpacked array.

A string literal is considered either as an integral type or string type depending on its context.

In the first concatenation, num is an integral type and cannot be concatenated with string types. It needs to be cast to a string type first.

In the second concatenation, all its operands are integral types, so the result is integral. But you cannot assign an integral type to a string type without a cast.

---

<div class="post-metadata">

**Author:** ![jianfeng.he](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/e47774/32.png) [@jianfeng.he](https://verificationacademy.com/forums/u/jianfeng.he)\
**Post date:** [April 16, 2023, 3:40pm UTC](https://verificationacademy.com/forums/t/string-concatenation/41527/4 "2023-04-16T15:40:44Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/string-concatenation/41527/3):_

> _In reply to [sbellock](https://verificationacademy.com/forums/t/string-concatenation/41527/3):_  
> In the second concatenation, all its operands are integral types, so the result is integral. But you cannot assign an integral type to a string type without a cast.

Let me understand, are you saying that in comb2, the concatenation operator {} will treat “hello” and other strings as integers, resulting in an integer output? If we want to assign it to a string type comb2, we need to convert it using string’(). Is that correct?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 18, 2023, 3:43am UTC](https://verificationacademy.com/forums/t/string-concatenation/41527/5 "2023-04-18T03:43:16Z")

</div>

_In reply to [jianfeng.he](https://verificationacademy.com/forums/t/string-concatenation/41527/4):_

Correct. “hello” and “world” are both 40-bit integral values
