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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:43:35+00:00 2026-05-26T14:43:35+00:00

This might sound a bit dumb but am confused. I know the strlen() would

  • 0

This might sound a bit dumb but am confused.
I know the strlen() would return the size of the character array in c. But there is something different going on with pointers to character.
This is my code:

void xyz(char *number)
{
    int i = 0;
    int length = strlen(number) - 2;
    while(i <= length)
    {
        printf("Number[]: %c",number[i]);
        i++;
    }
}

This prints the entire number I enter (Eg: 12345) but if I remove the -2 the result is not the same.
Could anyone tell me what am I missing?

  • 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-05-26T14:43:36+00:00Added an answer on May 26, 2026 at 2:43 pm

    There’s a good chance that you’re doing this to a string that you have obtained with fgets or a similar input function. In that case, it may well have the newline at the end still.

    If you change your code temporarily to:

    void xyz (char *number) {
        int i = 0, length = strlen (number);
        while (i < length)
            printf ("Number[%d]: %c (%d)", i, number[i], number[i]);
            i++;
        }
    }
    

    that should also show the numeric codes for all characters.

    The problem with encoding something like that - 2 in your function is that it will not work with:

    xyz ("123");
    

    since it will stop early, printing out only 12. The caller should be calling with valid data, meaning that it should adjust the value to be a numeric string before calling.


    You can see this happening in the following program:

    #include <stdio.h>
    #include <string.h>
    
    void xyz (char *number) {
        int i = 0, length = strlen(number) - 2;
        while(i <= length)
        {
            printf("Number[%d]: %c (%d)\n",i, number[i], number[i]);
            i++;
        }
        puts ("===");
    }
    
    void xyz2 (char *number) {
        int i = 0, length = strlen(number);
        while(i < length)
        {
            printf("Number[%d]: %c (%d)\n",i, number[i], number[i]);
            i++;
        }
        puts ("===");
    }
    
    int main (void) {
        char buff[100];
        printf ("Enter number: ");
        fgets (buff, sizeof (buff), stdin);
        xyz (buff);
        xyz ("12345");
        xyz2 (buff);
        xyz2 ("12345");
        return 0;
    }
    

    The (annoted) output of this, if you enter 98765, is:

    Enter number: 98765
    Number[0]: 9 (57)
    Number[1]: 8 (56)
    Number[2]: 7 (55)  # Your adjustment works here because of the newline.
    Number[3]: 6 (54)
    Number[4]: 5 (53)
    ===
    Number[0]: 1 (49)
    Number[1]: 2 (50)
    Number[2]: 3 (51)  # But not here, since it skips last character.
    Number[3]: 4 (52)
    ===
    Number[0]: 9 (57)
    Number[1]: 8 (56)
    Number[2]: 7 (55)  # Here you can see the newline (code 10).
    Number[3]: 6 (54)
    Number[4]: 5 (53)
    Number[5]:
     (10)
    ===
    Number[0]: 1 (49)
    Number[1]: 2 (50)
    Number[2]: 3 (51)  # And proper numeric strings work okay.
    Number[3]: 4 (52)
    Number[4]: 5 (53)
    ===
    

    If you’re looking for a robust user input function that gets around this problem (and avoids dangerous things like unbounded scanf("%s") and gets), I have one elsewhere on SO (right HERE, in fact) drawn from my arsenal.

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

Sidebar

Related Questions

This might sound a bit of an odd question but I know what I
I know this question might sound a little bit crazy, but I tough that
This might sound a bit of the topic, but i really wanna know whether
This might sound like a reaaaally dumb question but... why do browsers have a
I know this might sound really ridiculas but all of a sudden when I
This might sound a bit dumb. I always had this impression that web.config should
This might sound a bit un-realistic, but I've recently had to study the database
Now this might sound simple, but I'm a bit mixed up. I am trying
this question might sound a little bit weired... But I will try to explain:
I know this might sound that it should be trivial but I am finding

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.