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 6028461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:44:36+00:00 2026-05-23T04:44:36+00:00

Suppose i have array of characters. say char x[100] Now, i take input from

  • 0

Suppose i have array of characters. say char x[100]

Now, i take input from the user and store it in the char array. The user input is less than 100 characters. Now, if i want to do some operation on the valid values, how do i find how many valid values are there in the char array. Is there a C function or some way to find the actual length of valid values which will be less than 100 in this case.

  • 1 1 Answer
  • 3 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-05-23T04:44:37+00:00Added an answer on May 23, 2026 at 4:44 am

    Yes, C has function strlen() (from string.h), which gives you number of characters in char array. How does it know this? By definition, every C “string” must end with the null character. If it does not, you have no way of knowing how long the string is or with other words, values of which memory locations of the array are actually “useful” and which are just some dump. Knowing this, sizeof(your_string) returns the size of the array (in bytes) and NOT length of the string.

    Luckily, most C library string functions that create “strings” or read input and store it into a char array will automatically attach null character at the end to terminate the “string”. Some do not (for example strncpy() ). Be sure to read their descriptions carefully.

    Also, take notice that this means that the buffer supplied must be at least one character longer than the specified input length. So, in your case, you must actually supply char array of length 101 to read in 100 characters (the difference of one byte is for the null character).

    Example usage:

    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char *string = "Hello World";
        printf("%lu\n", (unsigned long)strlen(string));
        return 0;
    }
    

    strlen() is defined as:

    size_t strlen(const char * str)
    {
        const char *s;
        for (s = str; *s; ++s);
        return(s - str);
    }
    

    As you see, the end of a string is found by searching for the first null character in the array.

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

Sidebar

Related Questions

Suppose I have an array String[] which has 47 characters in each element. The
Suppose I have an array of size 10 characters (memset to 0), which I
I have an array of a structure which is supposed to store unique character
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns
Suppose I have an array that mimics a database table. Each array element represents
Suppose I have an array of nodes (objects). I need to create a duplicate
Suppose I have an array like this: $array = array(a,b,c,d,a,a); and I want to
I recently came across a question somewhere: Suppose you have an array of 1001
Suppose we have a vector/array in C++ and we wish to count which of
I have a question about Quantifiers. Suppose that I have an array and I

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.