Videos
You should be looking in glibc, not GCC -- it seems to be defined in strlen.c -- here's a link to strlen.c for glibc version 2.7... And here is a link to the glibc SVN repository online for strlen.c.
The reason you should be looking at glibc and not gcc is:
The GNU C library is used as the C library in the GNU system and most systems with the Linux kernel.
Here's the bsd implementation
size_t
strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s)
;
return (s - str);
}
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.
Check if this works for you --
void xyz(char *number)
{
int length = strlen(number);
while(i < length)
{
printf("Number[]: %c",number[i]);
i++;
}
}
and this function, if invoked as
xyz("1234");
should print out:
Number[]: 1
Number[]: 2
Number[]: 3
Number[]: 4
Is that what you really wanted ? If so, then let me point 2 mistakes.
1) "i" is not initialized. It is more a question of good practise. Explicitly initialize your loop control variable (to zero in this case), just don't assume it to be set. 2) your while loop condition with "<=" runs 1 extra cycle that it should.
Remember that arrays start from index '0' (zero), and an array of size 10, has valid index from 0 to 9, and C lang uses null character ('\0'), to terminate a string. So, your "1234" is actually stored as:-
string[0] = '1' string[1] = '2' string[2] = '3' string[3] = '4' string[4] = '\0' (<= NULL)
so if your loop-counter (control variable) i=0 at beginning of loop, for first iteration, you pick string[0], and for 2nd iteration (when i=1) you pick string[1]... and this way, the loop should run only 4 times, i.e. when i==4 (i.e. loopcounter < string-length), you must stop & exit loop.
Hope this clears up your doubt and help. If so, please don't forget to accept the answer.