{{{#!IRC ==================== C CLASS BEGIN! ==================== Alright bgs100, what about my pointer questions? :p We have actually been using the file-related library th- >:| I answered those ok go on We will review homework at the end of class... this time We have actually been using the file-related library this whole time stdio.h k It stands for: Standard Input/Output Now When we call printf, puts, scanf, or whatever, we are actually writing/reading from files :OOO :o stdin/stdout? (In Unix, anyway) stout (output), stdin (input), and stderr (We haven't used this; it's for errors) are all actually files juju2143, Correct! I'm not actually surprised, I just thought you were going for that effect so I helped ya * bgs100 gives juju2143 one of pedro3005's old stars yay pedro3005, lol * bgs100 shines up the star So yeah Now bgs100, cool, makes a lot of sense Good, wait what if I do this... In C, there are actually two (main) ways to repre- wat pedro@pedro:~$ echo "a" > stdout uh You made a file congrats :P pedro3005, Try this: lol pedro3005, echo "pie" > /dev/stdout you may want to do > /dev/stdout * bgs100 juju2143 another star yay< * juju2143 pins it with te other alright so yeah I thought about /dev :( but I thought it'd result in permission error pedro3005, lol, nope bgs100, ok go on Otherwise it could be difficult to call printf() ;D alright kay. okay so yeah Now In C, there are actually two (main) ways to represent a file, now., * bgs100 retracts star from juju2143 what does echo "pie" > /dev/stdout do? seidos, Did you try it? I check to see if it put anything in /dev/stdout but my vim is blank bgs100, yeah Did it print out "pie"? * seidos nods Okay seidos, /dev/stdout is a special file, representing standard output. stdout is a peripherial represented as a file * bgs100 gives juju2143 star back OKAY In C, there are actually two (main) ways to represent a file, What they are? By integer (file descriptor), why isn't there anything in the file? and by a special structure seidos, It's special, okay? :p bgs100, all right, all right Actually seidos, Look at the bottom of vim Here, it says: ""/dev/stdout" is not a file" oooohhhhhh thar ya go that's weird So yeah sending Pie to it, but it's not a file Everything is a file Vim acknowledges that this one is different :P moving on. For the beginning of today, we will be dealing with function that have to do with the special structure. it's like the blackhole file :D No That is /dev/null :( :p lol. they are both "not files" according to vim MOVING ON all right, moving on, sorry np lol * bgs100 was kidding :p MOVING ON Alright, I'm oging to make an example pedro3005, ... going* hey bgs100, what is the most complex program, in C, that you have...understood? worked on? I dunno Oging The act of poking, jabbing or prodding someone with a blunt, hard object. * bgs100 retracts pedro3005's star seidos, hmm pedro3005, that's an actual word? seidos, nah, urban dictionary yes! I made the teacher think :P oh. I think ogling is a word. yeah spell checker has it Alright bgs100, do I already know the answer? your bot? ... My bot is written in Python :p oh lol. Okay, pastebining http://pastebin.com/LeKHSz6Q SCRO , and then run "cat lucky.txt" bgs100, easier than I expected julien@maelstrom:~/ctests$ gcc file.c -o file bgs100, filename? file.c:1:9: erreur: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token bgs100, lucky? juju2143, lolwut juju2143, I didn't have "<" anywhere in there seidos, 11.c ? :P juju2143, did you use the -Wall option with gcc? oh right I forgot the # in #include * seidos has no idea what -Wall does but he's been using it juju2143, .... xP bgs100, I want to be able to reference it later julien@maelstrom:~/ctests$ ./file What is your lucky number? OVER 9000 julien@maelstrom:~/ctests$ cat lucky.txt Your lucky number is 134513963, just in case you forgot. julien@maelstrom:~/ctests$ cat: lucky.txt: No such file or directory oh crap, didn't run the program loool. *facepalm* juju2143, ... cool, my lucky number is 76, just in case I forgot lol. juju2143, a specific number, please :p ok julien@maelstrom:~/ctests$ ./file What is your lucky number? 42 julien@maelstrom:~/ctests$ cat lucky.txt Your lucky number is 42, just in case you forgot. 42 vs 76 who wins? 42 :p That's what I picked :P I wonder if a grand poo bah of a masonic order would agree lol :P :P 1337 pwns both haha julien@maelstrom:~/ctests$ cat lucky.txt Your lucky number is 31137, just in case you forgot. pwned naaah I'm the teacher, I say not :P k then. bgs100, moving on :p Your lucky number is -1208653712, just in case you forgot. that is the number for "Pi" ... my computer must be broken seidos, "Pi" is not a number pi is a number. if you import math? bgs100, sure it is, it's an irrational transcendental number "Pi" is a string oh lowercase "P"? No "pi" is a string too Neither are number bgs100, there should be a C interpreter There is compiling is so *ugh* pedro3005, lol bgs100, where? pedro3005, that's probably sacrilegious I'm like "Ohh, I should try this out. But then I'd have to compile.... oh... *gives up*" pedro3005, The google power is obviously not with you. http://www.google.com/search?client=ubuntu&channel=fs&q=C+Interpreter&ie=utf-8&oe=utf-8 g2g bai. bai later Okay So yeah Moving away from pi So Let's look at this. Let's take a step back, and see it as pseudo code Include input/output library Define main: Print "What is your lucky number? " Scan a number from input into the variable 'num' Open the file "lucky.txt" for writing (that's what the "w" means) *file is a pointer? Yeap That's what file functions use and return One sec Print "Your lucky number is , in case you forgot." into file Close file. The end. So yeah We already know about printf/scanf, fprintf is obviously different fopen opens in a file in a mode (run "man fopen") seidos, Yes it makes sense, generally It is veyr similar though very* bgs100, do you use Geany? printf is basically fprintf with the first argument as "stdout" pedro3005, Not too often sometimes Much lighter than most IDEs Anyway fprintf is just like printf, but it outputs to a file of your choice In this case, file. fclose closes a file, writing any changes that may not have actually taken place yet as well. pointers trip me out, I know that file is related to *file, but not quite sure how uhhhh file is a variable it is a FIle * FILE * That is why there is a * in front of "file" when why declare it Okay? you declare *file, a pointer, and then you can use file the variable? *file is not the pointer There is no *file I asked earlier if it was, and you said it was ? Asked what? No *file is a pointer? Yeap *sigh* Okay pedro3005, was faster than me Look bgs100, maybe the yeap wasn't for me seidos, Here Look bgs100, I'll use the google pwr to search on pointers? seidos, No Listen (or rather watch) Take the line that says: bgs100, http://i.imgur.com/bS8rd.png FILE *file; pedro3005, green bar? seidos, Replace: FILE *file; with: bgs100, don't you see it? FILE* file; oh yeah pedro3005, I don't know :( seidos, Okay? yeah, see it Did you replace? roger Good This is exactly the same thing working on it Now, look at that You are declaring file as a FILE* Alright? dude, that's weird I just prefer to put the * next to the variable. you can put the * either place *more sigh* seidos, Yes That is correct come on! you gotta' admit that's weird C doesn't complain about whitespace that much so what is FILE? FILE is a struct why does it have a *? FILE is a struct pedro3005, and FILE* is a struct? No. FILE* is a pointer to a struct ... pedro3005, Okay, it has a * because we want it to be a pointer, and why do we want that? so what's the pointer? the *? Because fopen, fprintf, fclose, and all their friends all use FILE* FILE is a struct file is var argh :p seidos, * makes it a pointer to a FILE struct instead of a FILE struct. Okay? it doesn't make intuitive sense, but okay NOW * seidos shrugs Why do fprintf, fclose, fopen etc use FILE*'s? hmmm This is an educated guess, but probably because that way the can modify the actual FILE struct. bgs100, which file struct? ....... The one that fopen gave you a pointer to seidos, FILE* is different than FILE. We make a variable, file, that is a FILE*, and NOT a FILE bgs100, but I don't understand how can you assign something as a pointer before pointing it to anything Uh bgs100, I'm still tripping. we made a *file that was a FILE a few minutes ago, now we're make a file that is a FILE*, but they are the same You are assigning it so it can be pointing to something pedro3005, ^ bgs100, ok seidos, Forget the previous thing bgs100, what is pointing to what? file is pointing to a FILE okay? so file is a pointer Yes!!! I thought you said it was a variable is variable = pointer? IT IS!!!!!!!!! seidos, int a = 5; Okay a is a variable See that line. Stare at it seidos, And... * seidos stares with all his might a is also an integer! pointer is a data type! That doesn't mean that 5 by itself is a variable, though! brb seidos, YES!!! Yay man, that was sick what do you mean? :p too much work for me to see that it was a data-type :| lol took me like a friggin' week to get it okay, gotta' remember that, pointer is a datatype seidos, lol, yeah, I wrote it down in a text file I'm still don't think I fully grasp it, but that makes more sense after having it snap *I infact oh hai I might have my old notes on C maybe I should bring my notepad That were taken while I was learning * seidos waves @ Snova Hai Snova dude, can you paste them? Snova, I think someone just achieved pointer enlightenment * seidos shakes his head I don't think I'm "pointer enlightened" yet ohai I hope so; I don't want to have to answer too many more pointer questions xP * seidos waves @ juju2143 :P sorry bro, it's like now that I know it's a datatype, I don't really know what kind it is it's not as intuitive as int, you know? at least I remember you saying that it allows actual modification of the struct alright seidos, You can think of a pointer as a special datatype that modifes another pasting old notes which were actually really short hmmm that makes sense I think more "special" things Snova, You wanna explain for a minute? :P first files, now datatypes seidos: C has exactly one kind of data, and that is "bytes". everything else exists as programmer convenience :p back when you type "int x;" you get four bytes in a row to be used as an integer. operations such as +*-/ on it will trigger the appropriate processor instructions to actually use it as an integer if you type "int* x;" you still get four bytes in a row, but it's not meant to be used as a normal number, but a memory address containing the address of some other integer seidos, http://pastebin.com/wMiU0eNJ It's highlighted like bash because I couldn't find the plain text :p bgs100, ascii art? Lol, yeah Snova, I'm kind of sorry I asked. this "c rabbit hole" probably goes down further than I would like, doesn't it? seidos: the hole is pretty shallow, actually, once you hit "memory" I mean all data types are bytes? that just makes me want to ask more questions Snova, methinks you've crated more work :p on some level. the difference between "float" and "int" is 1) the number of bytes they take up, and 2) the instructions the compiler will generate on them created* Snova, so as soon as we get to "processor instruction" that is the explanation for why there are different types of data to the programmer? seidos, So you don't have to remember the number of bytes yourself. seidos: the x86 architecture has a set of integer instructions, and a set of floating-point instructions, but there is nothing to prevent you from applying one to the other (other than common sense) bytes seem so abstracted from int and float to me, I'm not sure how that helps me at this point probably not; ignore that then Lol * bgs100 brings out Men in Black memory-erasing pen * bgs100 erases seidos's memory of the last few lines and makes seidos type in /clear every "int" you create takes up memory somewhere. an "int*" is simply another int that "happens" to contain the memory address of another int no, I think it'll be useful I just have to mull it over, or work with it so that it settles into my consciousness as long as my brain doesn't vomit it up, which it likes to do far too often * bgs100 gives seidos some psychological pepto-bismol hehe * seidos prays it's a psychological placebo seidos: are you reasonably familiar with the concept of a function? even just the idea; has bgs demonstated them yet? I have somewhat. Snova, depends what you mean by "reasonable" good enough Only one example so far have we actually made a function, though seidos: when you pass an "int" to a function, what that function gets is a copy. if it changes it, it only changes the copy. this is not always desirable, and why we have pointers I don't know to be honest. I don't want to say that I have a good enough understanding when I don't hmmm, that's interesting seidos: if that function instead took an "int pointer", rather than having a copy of an integer, it would know where it was instead- and have the ability to modify the real thing so a pointer is a data type that allows a function to modify the data in the memory address directly sure but an int will modify a copy of it Well, thats what it's often used for (note that, if you pass it an "int pointer", you're actually giving it a copy of said pointer- you can have pointers to pointers and so on but this is uncommon) Snova, **argv :p bgs100: that's more of a pointer array eh well, same thing really multi-dimensional arrays in general. Snova, yeah So well... no, a multi-dimensional array has to be rectangular ? Isn't it an array of arrays? yes, but they do have to be the same size for it to work Oh. meh Anyway :p * bgs100 prepares next example. 99% of the time what pointers are actually used for is to modify someone elses variables, so describing them from that viewpoint is probably logical Yeah oh, and dynamic allocation, but rather than asking malloc to modify something you're asking it to create it for you... I suppose it's the other way around; malloc is returning something for /you/ to modify Snova, what's the most complex c project you've worked on? I realize there is a hierarchy to code, I'm just not sure of the layers dunno, I don't use C much like how high up is Firefox? I imagine it's at the top. Or gnome-power-manager Snova, python mostly? seidos, Gnome itself is written in C, if I recall correctly. in terms of complexity? firefox would be up there, certainly; something like gnome-power-manager I don't know... it has a fairly straightforward purpose, I guess, but it does a lot of things seems to mostly be a configuration tool bgs100, it looks like GNU/Linux is so hacked together it's hard to tell where one project begins and another ends for me it is :p lol like I don't really know where gnome ends and x begins, or I should probably say where X ends and gnome begins I have a faint idea, but I don't really know in detail I do remember pausing some processes awhile back just to see what would happen though mmm, X is a complicated beast seidos, I like to think of it as a complex hierarchy. Perhaps combined with GTK+, or maybe above it, and sitting on top of X, GNOME is also associated with many 'gnome" projects. that's true, like gnometris Like KDE is assocated with everytihng that starts with a K or has a capital K in it. heh :P that's a good point, I remember trying to program my blackjack game in python seidos, pedro3005 http://paste.pocoo.org/show/227450/ then saw the blackjack that came with gnome Now, save and compile, but DO NOT RUN and was just like blown away how little I knew pedro3005, That means DO NOT RUN it was actually quite depressing X touches a lot of different parts of the system; its roots go deep into the kernel... but X stops when things are being displayed on the screen properly, and Gnome picks up there to draw basically everything seidos, pshaw, the guys who made that blackjack game were once new too. 20:21:01 Now, oops but firefox doesn't need gnome to function does it? Snova, lolwut seidos, No, just gtk ah gtk as a dependency so you have to have gtk libraries installed seidos: nope. it uses certain gnome components (e.g. gtk) but gtk talks directly to X. also, things like gnome's window manager are technically separate components but rather imporatnt I don't really get that seidos, Did you save and compile? do libraries just sit on the hard disk until they're called for no sorry, will focus on that now np But don't run, okay? :p seidos: when you run firefox, Linux reads it off the disk; a program called the dynamic linker reads the necessary libraries off of disk and "links" them to the binary, then proceeds to continue execution of firefox bgs100, compiled Okay seidos, Run: echo "Hello, gentlemen! What a fine day it is" | ./whateveryounametheexecutable Snova, sounds like DLLs in windows. What came first libraries in Unix or libraries in Windows? seidos: unix is much older bgs100, can I copy and paste? No Replace ./whateveryounametheexecutable with whatever you named the executable I didn't think so. I typed it out. it capitalized it and? seidos: also, "DLL" is pretty much the same thing as "shared library" seidos, What about the exclamation mark? there are four exclamation marks Okay Good * seidos wonders if firefox would run faster without gnome running seidos: not likely technically yes, but not to a degree anyone would really notice yeah. I don't know why. firefox seems sluggish to me I think it would be faster on windows, but I could be trippin' Okay there would be fewer processes sharing the CPU (even though most of them are idle), more memory available, and the X server would be doing less, but none of that will have much of an impact seidos, In this, we introduce 4 new things but my install of ubuntu is kind of a hack too getchar, putchar, EOF, and ctype.h/toupper So, I noticed ctype.h and toupper but not getchar, putchar, EOF seidos, getchar gets one character from input. However No characters are "submitted" until you press enter And You will notice that "c" is an integer, as opposed to a char (standing for "character"), because EOF is equal to -1, which characters can't hold seidos, Okay? okay EOF means End of File, btw er, technically they can, but the problem is getchar() can otherwise return anything from 0 to 255- so would otherwise be possible to send it a fake EOF if getchar() returned one byte >_> what? you're wrong :) yeah, EOF is end of file, knew that Snova, ssshhhhhhh :P orite, teachers are always correct * Snova sits in the corner Snova, how do you learn about programming? bgs100, and how did you learn? Snova, Yeah, I don't want to remove any possible illusions of a wizards C hacker with a long beard. wizardly* seidos: a book, the internet, and a few years of tinkering back sorry seidos, Help on IRC and the K&R maybe I should check out a book from the library. I'm currently trying to read Darwin bgs100, K&R? seidos, http://en.wikipedia.org/wiki/The_C_Programming_Language_%28book%29 haha that c variable throws me off a little It's written by guys who created Unix and C (correct, Snova?) Hai pedro3005 bgs100, did you explain the code yet? pedro3005, Which example? I don't think my library would have this book seidos, :[ whoa, this book is a tome! I wasn't even born when it was written I very very luckily got it at a yard sale for 50 cents bgs100: yeah, I think so bgs100, the last one they might have it or something like it as I recall C was invented *for* unix I have this book, the second edition pedro3005, Doing so now pedro3005, So do I :D it's too bad good quality books aren't available for free on the Internet well, actually I "borrowed" it from my dad pedro3005, lol I bought the book with my own hard-earned 50 cents bgs100, ... 50 cents?! I used to have a c++ book when I took a class in college ;D In fact bgs100, where do they sell books this cheap? I should've kept it pedro3005, That was double the original price I decided to donate an extra quarter all right, I think I need to program something but what? * seidos ponders pedro3005, Lol I got it from my local church's yard sale I'll program my depression calculator xD Anyway ON WITH THE EXPLANATION We love getting side-tracked here :p seidos, pedro3005 Okay, putchar prints one character out. so yeah seidos, gonna need a 'long long' for that one :P lol And toupper(0 takes in a character and makes it uppercase (with letters) toupper()* do you have to put "int main ()" or will "main ()" work? Oh, cool main() technically will But it's not good style. okay alright seidos: "main()" is old-style; most compilers will accept it if they feel forgiving, but it's not technically valid anymore afaik it's from the days where, if you didn't specify a return type/variable type, it was int by default then, we check if c is the character '!' ('blah' represents a character as the actual character in C), and if so print out three additional exclamation marks So yeah, we're just transforming input here. seidos, pedro3005 Any questions? er, brb bgs100, not really nvm Snova, Any questions? :P * bgs100 scans room for questions you know you have to delete someone from your msn when you receive this [21:42:46] ­I <3 twilight ... That is not a question. :p pedro3005, gagaga that's how I laugh "gagaga" I'm team Justin personally I think edward is too gay ... everyone knows werewolves are cooler than vampires pedro3005, ^^ /* */ (no comment) haha bgs100: this is clearly a comment; also seidos is right Snova, About what? :| well, maybe not; depends on the rendition ... bgs100, no questions about the code back to 99 I think ? Snova, ...? Snova, something is wrong, I'm never right pedro3005, okay seidos: you're left? lol Snova, haha, didn't get that for a second well, maybe like 5 seconds * bgs100 turns seidos around Now that you're right do you get it quicker? :P * seidos looks around uneasily I'm ambidextrous Seriously? and I'm always confused I can type with both hands :D Lol bgs100, HW? You said we'd go over it at the end ...anyway... if we are done with both Twilight and directions seidos, You're ready for the end? :p Okay seidos, pedro3005 Homework, please? I'm ready for something bgs100, sure I should really start using gedit I can't copy a full page in vim bgs100, http://paste.ubuntu.com/452257/ bgs100, oh, right, I was supposed to link you my homework hm, I don't think &* is necessary. you just get what you started with it dereferences a pointer and then takes its address again; you just get an identical pointer * pedro3005 quickly removes it from his code Lol Snova, I'll take it out and try it bgs100, http://paste.pocoo.org/show/227453/ seidos, Good, no compiler warnings with wall and it works correctly. pedro3005, Good, also no compiler warnings and functional Also, yay for indenting Snova, I get warning when I take out the & bgs100, python trashed me for good :D I notice that you scanf("%d",&toGuess); to scanf("%d",toGuess); because now toGuess is already a pointer seidos, No, take out the & and the * Snova, oh, I have to take out both &* Although seidos has spaces between function arguments seidos: if you only took out that, you'd be dereferencing a pointer with the remaining *, which would go from int* to int. scanf expects an int* though (which is good :p) A+'s for both of you :D * bgs100 gives seidos and pedro3005 stars I don't think I understand what the & is for seidos: goes the other way around; takes an int's address to get an int* seidos, getting the location of something in memory seidos: so &* effectively does nothing; you go from int* to int and then back to int* I think I just found the greatest placebo playing like I can pretend I'm praying like He-man with his sword and grayskull and junk hehehe * seidos is crazy all right, doesn't really make sense in fact, I'm surprised I got a good grade on my assignment bgs100, so, moar? :p it's only been two hours :p it felt like nothing! Lol whoa it's been two hours?! pedro3005, You were gone for at least 30 minutes xP I mean, we only really learned about fopen and getchar Okay maybe not 30 wwwhhhhaaatttt 0_o what else did we learn about? I would have to go back to the code to see what I "learned" ctype.h? pedro3005: idt's, but you weren't there rehashed pointers Snova, wha? oh, we didn't? well then pedro3005, Um, I was hoping you learned about files, a bit more about pointers, some file operating functions, getchar, putchar, ctype.h and toupper not even I remember idt's At least. bgs100, I did man which one did we do functions in? oh yeah, toupper forgot about that one That is not "fopen" and "getchar" :p * bgs100 thinks pedro3005 is making little of his generous use of time. *hmpf* (jk :p) Okay bgs100, I did learn all of these! you can try me right now I'LL TAKE THE TEST RIGHT NOW * pedro3005 takes shirt off pedro3005, What's the square root of 7584378943757435784326786578436584365763241632? we should probably take a test, so I can epically fail it I don't even think my computer can calculate that pedro3005: compute the square root of that number through continued approximation, using only bitwise operators hey Snova, is there a class for the "teachers" like you and bgs100? taught by Eric Hammond or something? pedro3005, How many bits would it take to represent a google? seidos, ? 333 bgs100, 2^333 * bgs100 doesn't know who that is... pedro3005, No, it only takes 33 bits :P I think it's 333 bits 333* not 2^333 bits damn seidos, Correct hehe 2^333 is quite a bit larger than 333 Around a google, I hear. :p pedro3005, better luck next time chummmmm-p :( no frowny faces, it's the first time I've *ever* got a question that you didn't quite frankly it's kind of cheating, since I asked the original question Lol no glory for seidos :| * bgs100 gives seidos the Test-Winnar (misspell intentional) award You are now a Winnar more questions! * Snova gives bgs100 the Spellar award Wie thank yuo :P I know a question I still don't remember. what is the general form equating logs and exponents? * bgs100 gives Snova the Sarkastic Award Award * seidos looks at his notes seidos, I can't remember it for the life of me bgs100 wait Do you mean in C? waiting... seidos, ^ no, just in general Oh seidos: if x^y = z then x log z = y I only know of pow() in C Okay Homework I have if x = b^y then y = log(base b) x pedro3005, Are you around? seidos: sure Well I think the "mathematical" notation is "x log z" but however you remember it bgs100, yes x log z? that sounds like x * log base z I got it from wikipedia Here's the homework, suggested by our resident Assistant teacher and Award Givzer: Make a program that transforms input by making every other character uppercase. FOR BONUS POINTS, ah, wikipedia uses a subscript, which makes sesne Make the program also ensure that every other letter is lowercase, aaannnddd *thinks of other bonus* At the end tell the length of the input. (that is, after EOF is recieved) Okay? yeah, I typed that in my file already LiKe ThIs? Not quite LiKe tHiS? Because spaces count it would have tried to uppercase the whitespcae even though you can't uppercsae/lowercase them whitespace* Yes, what Snova said :p bgs100, so we fix that bug :p pedro3005, Okay, extra bonus, I guess alright? pedro3005: see also isspace() Everyone good on homework? alright, I'll work on it Snova, sure, thanks Snova, pedro3005 and isalpha() I know that one perhaps depends on how you want to do it, I guess yeah Okay seidos, Questions? meh, I'll take questions after class ======================== BEGIN DISMISSAL OF CLASS! ============================== ;p I'll see you all Monday, have a nice, uh, Saturday (just kidding) thank you bgs100 no questions Alright, cool :) thanks bgs100 You're welcome. Thank Snova, too :p thanks Snova :p thanks Snova }}}