Ad Space — Top Banner

?TYPE MISMATCH ERROR

Commodore Commodore 64

Severity: Minor

What it means

?TYPE MISMATCH ERROR means you used a string where a number was expected, or a number where a string was expected.
For example: LET A = "HELLO" + 5 mixes a string and a number, which BASIC cannot handle.

Affected Models

  • Commodore 64
  • Commodore 64C
  • Commodore 128 (C64 mode)
  • VICE emulator (C64)

Common Causes

  • Adding a string variable to a numeric variable (e.g. A$ + B where B is numeric)
  • Passing a string to a function that expects a number (e.g. SQR(A$))
  • Passing a number to a string function (e.g. LEFT$(5, 2) instead of LEFT$(A$, 2))
  • Using = to compare a string variable to a number
  • INPUT storing a string into a numeric variable (user typed letters when number was expected)

How to Fix It

  1. Check whether your variables are numeric or string types.

    In C64 BASIC, variable names ending in $ are strings (A$, NAME$).
    All others are numeric (A, X, SCORE).
    You cannot mix them in expressions.

  2. Use VAL() to convert a string to a number when needed.

    If A$ = "42" and you need to do arithmetic: use VAL(A$) instead of A$ directly.
    VAL("42") returns the number 42.

  3. Use STR$() to convert a number to a string when needed.

    If you need to join a number into a string: PRINT "SCORE: " + STR$(SCORE) works because STR$(SCORE) converts the number to a string.

  4. When using INPUT, validate what the user typed before using it as a number.

    If your program does INPUT A and the user types HELLO, A contains a string value that will cause TYPE MISMATCH if used in arithmetic.
    Add a check or use INPUT A$ then convert with VAL().

Frequently Asked Questions

Why does C64 BASIC use $ to mark string variables?

This convention comes from Microsoft BASIC, which Commodore licensed for the C64.
The $ suffix is a shorthand way of telling the interpreter that the variable holds text rather than a number — no type declaration needed.

Can I store a number like 42 in a string variable on the C64?

Yes.
A$ = "42" stores the text 42 in a string variable.
But you cannot do arithmetic on it directly — use VAL(A$) to get the numeric value first.