[Courses] [C] Beginner's Lesson 4A: Arrays, Qualifiers, and Reading Numbers

Eugene Teo eugene.teo at eugeneteo.net
Fri Oct 11 10:19:34 EST 2002


<quote who="KWMelvin">
u> On Thu, Oct 10, 2002 at 05:59:53PM -0400, Morgon Kanter wrote:
u> > The third:
u> > When you do:
u> > printf("Enter a line: ");
u> > fgets(line, sizeof(line), stdin);
u> > printf("The length of the line is: %d\n", strlen(line));
u> > 
u> > You can do
u> > printf("The length of the line is: %d\n", strlen(line) - 1);
u> > to get the true size of the line, not with the \0 at the end.

But do make it a good habit to take strlen(line) value instead.
Most newbies forgot about \0 because of this.

u> 			if (scanf("%d", &n) == 1)

Always remember to leave a space before %d, i.e.
scanf(" %d", &n)

Eugene

u> 	Luckily for us, Ctrl-C works great in Linux!

It works in any sane OSes.

-- 
eMail: eugeneteo at eugeneteo.net, eugeneteo at null.cc.uic.edu
gpg pub_key: http://null.cc.uic.edu/~eugeneteo/eugeneteo.asc
main(i){putchar(182623909>>(i-1)*5&31|!!(i<7)<<6)&&main(++i);}




More information about the Courses mailing list