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

KWMelvin kwmelvin at intrex.net
Thu Oct 10 22:07:14 EST 2002


On Thu, Oct 10, 2002 at 05:59:53PM -0400, Morgon Kanter wrote:
> Hey, its me again. This is just some various stuff I wanted to say.
> 
> First of all, could you please, in the next part of the lesson, include a 
> list of what each of the functions in the standard C library does? I want 
> to print something like this out to have on hand, but you only list a 
> couple each lesson! =)

Greetings Morgan,
	I am in the process of making a C Standard Library reference for
	myself.  I have made a tarball of what I have, and hopefully
	remembered to attach it to this email.  I don't know how it looks
	in any other browser except for lynx, so don't judge my HTML
	harshly if it doesn't look good in Netscape or any other graphical
	browser.  If you do a  tar -xzvf stdlib.tgz  the files should
	go in the directory you are currently in, so you may want to
	make a directory for them first, then change to that directory
	before extracting the files.  I hope this helps.

	I am introducing the functions in these lessons as they are
	presented in the book.  I did list most of these functions in
	Lesson Two: Addendum, so you might want to check that out from
	the list archives. 8^D
	
> 
> The first thing I had to say:
> Hey, I tried this out without the .0s at the end of the numbers and it 
> worked exactly the same. Why are you including .0s for the floats? I know 
> you have to do this in Java, but not here it seems. What I'm talking about 
> is when you wrote:
> data[0] = 34.0;
> data[1] = 27.0;
> data[2] = 45.0;
> data[3] = 82.0;
> data[4] = 22.0;
> Why not:
> data[0] = 34;
> data[1] = 27;
> data[2] = 45;
> data[3] = 82;
> data[4] = 22;

	Well, I guess that I can only say that I am pretty much following
	the example given in the textbook.  I know that a floating point
	variable will accept a number without the decimal point and fraction,
	but maybe it is just easier to identify a float if it has that stuff?
	I don't mind typing a wee bit more if it will make the program
	easier to read!
	
> And the second:
> Just a small detail excluded I guess, but after putting:
> strcpy(full, first);
> strcat(full, " ");
> strcat(full, last);
> Why don't you ask what happens if you put:
> strcat(full, " ", last);

	I guess because each library function has a very specific rule for
	putting the arguments in the parentheses.  If you try and do it
	a different way, the compiler won't accept it.  You always have
	to follow the rules for these predefined library functions.  If
	you write your own function, you can make it do that.  Maybe that
	would be a good exercise to practice on?  Have we learned enough
	of the C language yet?  I think we'll get to functions in Chapt.8.
	We're halfway there as soon as we finish Chapt.4.  If someone
	else would like to discuss writing your own functions, I sure
	wouldn't mind!  If it is over my head right now, maybe it won't
	be later?
	
> The third:
> When you do:
> printf("Enter a line: ");
> fgets(line, sizeof(line), stdin);
> printf("The length of the line is: %d\n", strlen(line));
> 
> You can do
> printf("The length of the line is: %d\n", strlen(line) - 1);
> to get the true size of the line, not with the \0 at the end.

	Great! That is fantastic. I didn't see it.  Now I do, thanks to you!
	
> And lastly:
> Please explain _format_, sscanf, and all that! I'm confused there.

	Normally, we use the variable `line' for lines read from the keyboard:

		char line[100];   /* line of keyboard input */

	When we want tp process input, we use the statements:

		fgets(line, sizeof(line), stdin);
		sscanf(line, format-stuff, &var1, &var2 ...);

	Here fgets() reads a line and sscanf() processes it.  format-stuff is
	a string similar to the printf() format string.  Note the ampersand
	(&) in front of the variable names.  This is used to indicate that
	sscanf() will change the value of these variables.  We'll find out
	more about why we need the ampersand in Chapt.12, "Simple Pointers".

	Here is an old example from a comp.lang.c posting by Steve Summit
	that might explain why we don't really want to use plain old
	scanf() when reading input:

	/* 
	Do not type this code into your computer.  After typing it in, do
	not compile it.  After compiling it, do not run it.  but most of
	all, when you run it, do not type the letter x or the string abc
	when it asks you to type a number.
	*/
	#include <stdio.h>
	main()
	{
		int n;
		while (1) {
			printf("enter a number:\n");
			if (scanf("%d", &n) == 1)
				break;
			printf("No, a *number*. Try again: ");
		}
		printf("You typed %d\n", n);
	}

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

	I hope that helps. Thank you. -- K
	
-------------- next part --------------
A non-text attachment was scrubbed...
Name: stdlib.tgz
Type: application/x-gtar
Size: 5447 bytes
Desc: not available
Url : http://linuxchix.org/pipermail/courses/attachments/20021010/2c8dd9e6/stdlib.tgz


More information about the Courses mailing list