Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!ucsd!nosc!crash!pnet01!kevin From: kevin@pnet01.cts.com (Kevin Hill) Newsgroups: comp.sys.mac Subject: C-tutor Message-ID: <2584@crash.cts.com> Date: 9 May 90 03:16:41 GMT Sender: root@crash.cts.com Organization: People-Net [pnet01], El Cajon CA Lines: 86 #include char string[] = "Hello"; _main() { int number = 65; char letter = 'A'; unsigned number2 = 40000; printf("%.3s %7s\n %d %c %c %c\n %u %x %X\n ",string,string,number, number,letter,string[1],number2,number2); } /*the output: Hel Hello --> ^^^ 1234567 65 A A e 40000 9c40 9C40 As you can see, the %.3s causes only 3 characters of Hello to be printed. The %7s causes the 2nd Hello to be padded with 2 extra spaces. 5 for the letters hello, and 2 blank spaces. The --> is where I put my own text in. Not the program. %d prints 65 as you would expect but, interestingly enough, the %c tied to number prints an A. WHAT? But number is an integer, not a character. Ahh, here's were C get's fun, C doesn't care that number is an integer, it just looks at the memory location were number stores its values and finds 65, the ascii code for A. So it prints an A. %c tied to letter prints an 'A' as expected. %c tied to string[1] prints an 'e'. You probably thought it should have been an 'H' but string[0] = 'H'. All of the arrays in C start with position zero. Then the unsigned 40000, and then printed in hexidecimal. Lowercase x for lowercase hexidecimal alpha numbers. and an uppercase X to force the alpha-numbers to be capitals. Also, if you copy this letter, and put try it in Think C, it will work only if you change _main to main. I have that change so that my program will create a file of the output for me. If you are curious how I did it, send me some mail and I will answer for you. Also, You will notice that I have all of my numbers initialized to something. If I had not done that then all of my output would have been random as in C, when you declare a variable it is not initialized. It contains garbage. You will notice that I declared char string[] = "Hello"; outside of the main() routine. I did this because C dictates that to initialize a string like that you have to declare it as a global variable. Don't ask me why, but I have to do it as the compiler dictates. Try switching it to the inside of the function when you compile it and you will get an error. If anyone has any additions, please do so. If you have any questions, don't be afraid to ask, I'll try to answer them as best I can. Also, if you are asking a question, please post them to the conversation and don't e-mail them. If someone else knows the answer feel free to answer it. To create a new project in Think C. First start the C application then hit the New button at the dialog. Select the folder that you wish the project to reside in and then name and save it. Go to the source menu, and select the Add item. Find the file MacTraps and select that. After a second or two it will add it. Now find the file named stdio, ( NOT STDIO.H) and add that. Then type cancel after it is done with that. You should now be able to run the project with no problems! 5-8-90 pt.1.c */ UUCP: {hplabs!hp-sdd ucsd nosc}!crash!pnet01!kevin ARPA: crash!pnet01!kevin@nosc.mil INET: kevin@pnet01.cts.com ------------------------------------ ! San Diego State University ! ------------------------------------