Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:51:04+00:00 2026-06-17T04:51:04+00:00

In UTF8 I use to count characters (not bytes) using this function: int schars(const

  • 0

In UTF8 I use to count characters (not bytes) using this function:

int schars(const char *s)
{
    int i = 0;

    while (*s) {
        if ((*s & 0xc0) != 0x80) i++;
        s++;
    }
    return i;
}

Does this work on implementations where plain char is unsigned char?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:51:06+00:00Added an answer on June 17, 2026 at 4:51 am

    It should.

    You are only using binary operators and those function the same irrespective of whether the underlying data type is signed or unsigned. The only exception may be the != operator, but you could replace this with a & and then embrace the whole thing with a !, ala:

    !((*s & 0xc0) & 0x80)
    

    and then you have solely binary operators.

    You can verify that the characters are promoted to integers by checking section 3.3.10 of the ANSI C Standard which states that “Each of the operands [of the bitwise AND] shall have integral type.”

    EDIT

    I amend my answer. Bitwise operations are not the same on signed as on unsigned, as per 3.3 of the ANSI C Standard:

    Some operators (the unary operator ~ , and the binary operators << , >> , & , ^ , and | ,
    collectively described as bitwise operators )shall have operands that have integral type.
    These operators return
    values that depend on the internal representations of integers, and
    thus have implementation-defined aspects for signed types.

    In fact, performing bitwise operations on signed integers is listed as a possible security hole here.

    In the Visual Studio compiler signed and unsigned are treated the same (see here).

    As this SO question discusses, it is better to use unsigned char to do byte-wise reads of memory and manipulations of memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to What are the characters that count as the same character
I am trying to use XML::RAI perl module on UTF8 coded text and I
So I want to use SetThreadLocale to set a threads codepage to UTF8. Up
I'm trying to use eclipse encoding UTF-8, for polish characters. Unfortunately, I still have
If I'm using utf-8 encoding for a character set that would use the character
I am working on a HTML5 website, using UTF-8 charset. I tried to use
all the while, I am using setw for my ANSI text file alignment. Recently,
I try to deploy web service using axis1 and use in my web service
I am facing an issue Empty id attribute is not allowed in JSF while
I need to get the actual character count (not byte count), similar to what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.