C String strupr() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
Extension / <ctype.h>

What You’ll Learn

The strupr() function converts every lowercase letter in a C string to uppercase in place. It is a convenient extension on some compilers, but beginners should also learn the portable toupper() loop that works everywhere.

01

Uppercase

In place.

02

char*

Same buf.

03

Not ISO C

Extension.

04

toupper

Portable.

05

Writable

char[].

06

vs strlwr

Lowercase.

Definition and Usage

strupr walks a modifiable null-terminated string and replaces each lowercase letter with its uppercase equivalent. Digits, spaces, and punctuation stay as they are. The function returns the same pointer you passed in because the transformation happens inside the original buffer.

For example, "Hello, C!" becomes "HELLO, C!"—only the letters H, e, l, l, and o change case.

⚠️
Portability Warning

strupr() is not part of ISO standard C. GCC and Clang on Linux typically do not provide it. Use a small toupper loop for portable projects, or _strupr() when targeting MSVC with appropriate #ifdef guards.

📝 Syntax

Common extension declaration (availability varies by compiler):

C
char *strupr(char *str);

Parameters

  • str — pointer to a modifiable, null-terminated string to convert.

Return Value

  • Pointer to str after in-place conversion (same address as the input).

Headers

  • strupr / _strupr<string.h> on toolchains that provide the extension.
  • Portable alternative — <ctype.h> for toupper().

⚡ Quick Reference

BeforeAfter strupr / toupper loop
"Hello, C!""HELLO, C!"
"abc123""ABC123"
"ALREADY UPPER""ALREADY UPPER" (unchanged)
writable bufferrequired (not string literal)
portabletoupper((unsigned char)c) per byte
Extension
strupr(buf);

If available

MSVC
_strupr(buf);

Windows

Portable
str_to_upper(buf);

toupper loop

Compare only
strcasecmp(a, b)

No modify

Examples Gallery

Examples use the portable toupper helper so they compile on Linux, macOS, and Windows. Where _strupr exists, it behaves the same way in place.

📚 Getting Started

Uppercase every letter in a writable char array.

Example 1 — Convert to Uppercase (Portable)

Turn "Hello, C!" into uppercase, matching the reference tutorial.

C
#include <stdio.h>
#include <ctype.h>

void str_to_upper(char *s) {
    for (; *s; s++) {
        *s = (char)toupper((unsigned char)*s);
    }
}

int main(void) {
    char text[] = "Hello, C!";

    str_to_upper(text);

    printf("Uppercase String: %s\n", text);

    return 0;
}

How It Works

Each byte is passed to toupper. Casting to unsigned char avoids undefined behavior if a byte is negative on platforms where char is signed.

Example 2 — Preserve the Original String

Copy into a buffer, then uppercase the copy.

C
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void str_to_upper(char *s) {
    for (; *s; s++) {
        *s = (char)toupper((unsigned char)*s);
    }
}

int main(void) {
    const char *original = "Hello, C!";
    char buffer[32];

    strcpy(buffer, original);
    str_to_upper(buffer);

    printf("Original:  %s\n", original);
    printf("Uppercase: %s\n", buffer);

    return 0;
}

How It Works

strupr always mutates its argument. Copying with strcpy first keeps the source literal unchanged while you transform the working copy.

📈 Practical Patterns

Format display text, understand toupper, and MSVC naming.

Example 3 — Format a Banner Label

Uppercase a title string before printing a header line.

C
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void str_to_upper(char *s) {
    for (; *s; s++) {
        *s = (char)toupper((unsigned char)*s);
    }
}

int main(void) {
    char title[] = "welcome to codetofun";

    str_to_upper(title);

    printf("======== %s ========\n", title);

    return 0;
}

How It Works

Uppercasing is useful for consistent display formatting. For locale-aware text (accents, Unicode), ASCII toupper is not enough—but it covers basic English tutorials.

Example 4 — toupper() on One Character

See what changes and what stays the same.

C
#include <stdio.h>
#include <ctype.h>

int main(void) {
    char letters[] = "a B c";

    for (int i = 0; letters[i]; i++) {
        unsigned char c = (unsigned char)letters[i];
        printf("'%c' -> '%c'  ", letters[i], (char)toupper(c));
    }
    printf("\n");

    return 0;
}

How It Works

toupper only affects lowercase letters in the current C locale. Spaces and already-uppercase letters pass through unchanged—that is exactly what strupr does across a whole string.

Example 5 — Using _strupr() on Windows

When your toolchain provides the extension, call it directly on a writable buffer.

C
/* Windows / MSVC example */
#include <stdio.h>
#include <string.h>

int main(void) {
    char buf[] = "Hello, C!";

#if defined(_MSC_VER)
    _strupr(buf);
    printf("Uppercase: %s\n", buf);
#else
    printf("_strupr is not available on this platform.\n");
    printf("Use the portable toupper loop instead.\n");
#endif

    return 0;
}

How It Works

_strupr modifies buf in place and returns the same pointer. Guard with preprocessor checks so Linux CI builds still compile.

🚀 Common Use Cases

  • Display formatting — print headings or labels in uppercase.
  • Normalized keys — store identifiers in one case for lookup tables.
  • Simple protocol tokens — fold HTTP method names or command verbs.
  • Teaching case folding — connect character functions to whole-string transforms.
  • Legacy code — read programs that call strupr on Windows toolchains.

🧠 How strupr() Works

1

Start at str[0]

Walk until '\0'.

Scan
2

Apply toupper

Replace lowercase ASCII letters in place.

Fold
3

Leave others alone

Digits, space, punctuation unchanged.

Skip
=

char* str

Same buffer, now uppercase letters.

📝 Notes

  • Not part of ISO C—treat as a vendor extension unless your docs guarantee it.
  • Modifies the buffer in place; requires a writable char[], not a literal.
  • Uses the current C locale rules inside toupper—ASCII-centric in typical tutorials.
  • For case-insensitive comparison without mutation, prefer strcasecmp() or _stricmp().
  • Pair conceptually with strlwr() for the opposite transformation.

⚡ Optimization

Uppercasing a string is O(n) in the length of the text. For short identifiers the cost is negligible. Avoid repeated uppercasing of the same string in a hot loop—fold once and reuse the result.

Conclusion

strupr() uppercases a string in one call on compilers that provide it. For portable code, implement a tiny toupper loop and use _strupr only behind platform guards.

See also strlwr() for the lowercase counterpart.

💡 Best Practices

✅ Do

  • Use a portable toupper loop for cross-platform code
  • Pass writable char buffer[] arrays
  • Copy with strcpy when the original must stay unchanged
  • Cast bytes to unsigned char before toupper
  • Wrap _strupr in #if defined(_MSC_VER)

❌ Don’t

  • Assume strupr exists on every compiler
  • Call it on string literals
  • Expect Unicode or UTF-8 full case mapping from ASCII toupper
  • Uppercase both strings just to compare once—use strcasecmp
  • Forget that the original text is destroyed in place

Key Takeaways

Knowledge Unlocked

Five things to remember about strupr()

Use these points when folding strings to uppercase in C.

5
Core concepts
⚠️ 02

Extension

Not ISO.

Portability
🔢 03

toupper

Portable.

Pattern
📈 04

Writable

char[].

Safety
💬 05

vs strlwr

Lowercase.

Compare

❓ Frequently Asked Questions

strupr() converts every lowercase letter in a string to uppercase, modifying the string in place. It returns a pointer to the same buffer. Non-letter characters are left unchanged.
No. strupr() is a compiler-specific extension (common on older Microsoft and Borland toolchains). ISO C does not define it. For portable code, write a loop with toupper() from <ctype.h>.
toupper() converts one character. strupr() (where available) converts an entire null-terminated string in place. A portable helper loops over each byte and applies toupper().
No. String literals are read-only. strupr() modifies memory, so pass a writable char array or heap buffer.
Microsoft documents _strupr() in <string.h>. Some environments also expose strupr as an alias. Check your compiler documentation.
strupr() folds letters to uppercase; strlwr() folds them to lowercase. Both are non-standard extensions that mutate the buffer in place.
Did you know?

Standard C never added strupr or strlwr, but C++ offers std::toupper in <cctype> and higher-level case APIs in later standards. In pure C, a five-line toupper loop is the portable replacement students should memorize.

Continue the String Functions Series

Fold strings to uppercase with strupr(), then learn locale-aware transformation with strxfrm().

Next: strxfrm() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful