Array's index and argc signedness
Posted
by tusbar
on Stack Overflow
See other posts from Stack Overflow
or by tusbar
Published on 2010-03-14T14:08:58Z
Indexed on
2010/03/14
14:15 UTC
Read the original article
Hit count: 196
Hello,
The C standard (5.1.2.2.1 Program startup) says:
The function called at program startup is named main. [...]
It shall be de?ned with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters [...] :
int main(int argc, char *argv[]) { /* ... */ }
And later says:
The value of argc shall be nonnegative.
- Why shouldn't
argc
be defined as anunsigned int
,argc
supposedly meaning 'argument count'? - Should
argc
be used as an index forargv
?
So I started wondering if the C standard says something about the type of array's index. Is it signed?
6.5.2.1 Array subscripting:
One of the expressions shall have type ‘‘pointer to object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.
It doesn't say anything about its signedness (or I didn't find it). It is pretty common to see codes using negatives array indexes (array[-1]
) but isn't it undefined behavior?
- Should array's indexes be unsigned?
© Stack Overflow or respective owner