Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [9:58:27 PM] David: K got that in
- [9:58:29 PM] David: now what
- [9:58:37 PM] Andrew Story: int main( int argc, char* argv[] )
- {
- }
- [9:58:42 PM] Andrew Story: That's "Proper"
- [9:58:55 PM] David: Proper formatting as well?
- [9:59:04 PM] Andrew Story: Well kinda, yea
- [9:59:04 PM] David: Explain argc and argv
- [9:59:08 PM] David: argv is an array
- [9:59:13 PM] David: of chars
- [9:59:15 PM] David: what does * do
- [9:59:20 PM] Andrew Story: argc is the number of arguments on the commandline
- [9:59:26 PM] David: arg Count
- [9:59:27 PM] David: ahaa.
- [9:59:30 PM] David: bitch
- [9:59:36 PM] Andrew Story: argv is an array of pointers to char types
- [9:59:50 PM] David: Ah right * means pointer
- [9:59:54 PM] Andrew Story: In C, a string or array is a pointer to a block of contiguous memory
- [9:59:59 PM] David: so argcount and argvars?
- [10:00:05 PM] Andrew Story: or argvalues
- [10:00:10 PM] David: Argvalues right
- [10:00:12 PM] Andrew Story: whatever makes sense to you
- [10:00:20 PM] David: I get pointers already if that helps
- [10:00:26 PM] David: It's like passing by reference
- [10:00:27 PM] Andrew Story: That's a HUGE help
- [10:00:36 PM] David: ByRef vs ByVal
- [10:00:37 PM] Andrew Story: That's what catches most people with C
- [10:00:46 PM] David: Alrighty well I'm good to go there already :)
- [10:00:55 PM] Andrew Story: Also in C, strings end with a NULL (o)
- [10:00:57 PM] Andrew Story: 0
- [10:01:11 PM] David: Right, null terminated
- [10:01:16 PM] David: knew that as well
- [10:01:21 PM] Andrew Story: so in memory, it's literally stored as "blah blah blah\0"
- [10:01:23 PM] David: don't know how that translates to code but
- [10:01:24 PM] David: Ah
- [10:01:35 PM] Andrew Story: To get the length, you have to count all the characters =p
- [10:01:55 PM] David: I'm assuming that takes seconds to write a method for though
- [10:02:02 PM] Andrew Story: There's strlen(char*) which does it for you, but is obviously not a O(1) function
- [10:02:07 PM] David: Right
- [10:02:27 PM] Andrew Story: So we may as well print hello world, right?
- [10:02:55 PM] Andrew Story: In the main function, type
- puts("HELLO ME");
- return 0;
- [10:03:20 PM] David: Puts = put string
- [10:03:24 PM] Andrew Story: yep
- [10:03:27 PM] David: I'm assuming that is to the console then
- [10:03:30 PM] Andrew Story: Yea
- [10:03:34 PM] David: Okay, easy enough
- [10:03:43 PM] David: How to run?
- [10:03:45 PM] Andrew Story: Puts also inserts a carriage return and a newline at the end
- [10:03:58 PM] David: So s.o.pln
- [10:04:07 PM] Andrew Story: F9 or the gear with the green triangle under it
- [10:04:10 PM] David: It's a printline
- [10:04:11 PM] David: kk
- [10:04:12 PM] Andrew Story: http://puu.sh/1u2cj
- [10:04:22 PM] David: How do I print sans crlf
- [10:04:37 PM] David: It ran as expected with the proper output.
- [10:05:02 PM] Andrew Story: printf(char*,...) will print a formatted string with no CRLF
- [10:05:29 PM] David: I am familiar with printf in other languages
- [10:05:33 PM] Andrew Story: So for a simple string like Hello World, you can just do printf("Hello World")
- [10:05:55 PM] Andrew Story: http://www.cplusplus.com/reference/cstdio/printf/
- [10:05:57 PM] David: The other arguments which are optional and unlimited are replacements yes?
- [10:06:03 PM] David: yep
- [10:06:04 PM] Andrew Story: That has a list of all the flags and what they do
- [10:06:30 PM] Andrew Story: Basically, if you know the syntax of C and have access to that site, that's all you really need
- [10:06:50 PM] Andrew Story: Just get really familiar with cstdio and that's all you really need to know
- [10:06:56 PM] David: Teach me the syntax
- [10:07:37 PM] David: How do I print anything which is not a string
- [10:07:44 PM] David: must it be strictly cast to a string?
- [10:07:47 PM] Andrew Story: With printf
- [10:07:55 PM] Andrew Story: printf("%i", inthere);
- [10:08:00 PM] Andrew Story: That prints an int
- [10:08:16 PM] Andrew Story: printf("%f",floathere); prints a float
- [10:08:29 PM] Andrew Story: %s for string, %c for char, etc
- [10:08:31 PM] David: Right right got that
- [10:08:48 PM] Andrew Story: Well, there are just a few fundamental types really
- [10:08:50 PM] David: Okay, should I hit a loop of some kind, how to I terminate the program?
- [10:08:59 PM] David: Or any other failure
- [10:09:12 PM] Andrew Story: The program will terminate on one of two conditions
- [10:09:28 PM] Andrew Story: Either the main function ends, or exit(int) is called
- [10:09:40 PM] Andrew Story: exit(int) is the same as return int; from main
- [10:09:52 PM] Andrew Story: however exit(int) can be called anywhere in the program
- [10:10:00 PM] David: Right
- [10:10:01 PM] David: Okay well
- [10:10:12 PM] David: What if I accidentally an infinite loop
- [10:10:21 PM] David: ctrl alt del kill process?
- [10:10:29 PM] Andrew Story: Just hit x on the cmd window
- [10:10:37 PM] David: WHAT IF THERE IS NO CMD WINDOW
- [10:10:48 PM] Andrew Story: THEN CTRL ALT DEL THE PROCESS
- [10:11:02 PM] Andrew Story: OR ADD AN OUT INSIDE OF THE LOOP
- [10:11:08 PM] David: OKAY SOUNDS GOOD YO
- [10:11:25 PM] Andrew Story: So loops are pretty much the same as anywhere else
- [10:11:43 PM] Andrew Story: You have label/goto, while, for, do/while
- [10:12:04 PM] Andrew Story: You can make labels with labelname:
- [10:12:14 PM] Andrew Story: goto labelname;
- [10:12:15 PM] David: So that's more assembly there
- [10:12:21 PM] Andrew Story: Yes
- [10:12:22 PM] David: but gotos are evil of course
- [10:12:23 PM] Andrew Story: Never use goto
- [10:12:26 PM] Andrew Story: =p
- [10:12:29 PM] David: because it fux wit da scope
- [10:12:34 PM] Andrew Story: exactly
- [10:12:43 PM | Edited 10:12:45 PM] Andrew Story: and can cause memory leaks
- [10:12:47 PM] David: mhm
- [10:13:02 PM] Andrew Story: while(condition!=0){}
- [10:13:13 PM] Andrew Story: in C, any integer can be used as a boolean
- [10:13:16 PM] David: for(int i =0; i < 1000; i++){} valid?
- [10:13:25 PM] Andrew Story: if it's 0, it's regarded as false, otherwise true
- [10:13:35 PM] David: So no true/false singletons?
- [10:13:38 PM] Andrew Story: Yes, but only in c99 and later
- [10:13:46 PM | Edited 10:13:50 PM] Andrew Story: There's no true or false in C
- [10:13:52 PM] David: kk
- [10:14:03 PM] David: i could define em :P
- [10:14:08 PM | Edited 10:14:11 PM] Andrew Story: You can always #define true 1 and #define false 0 though
- [10:14:18 PM] David: mhm
- [10:14:25 PM] David: [10:13 PM] David:
- <<< for(int i =0; i < 1000; i++){} valid?
- [10:14:32 PM] Andrew Story: [10:13 PM] Andrew Story:
- <<< Yes, but only in c99 and later
- [10:14:59 PM] Andrew Story: In codeblocks, you can enable c99 by going to project->build options
- [10:15:14 PM] David: Lets not touch that yet
- [10:15:18 PM] David: what's proper syntax then
- [10:15:19 PM] Andrew Story: Alright
- [10:15:39 PM] Andrew Story: int i; for( i = 0; i < 1000; i++ ){}
- [10:16:00 PM] Andrew Story: in c99, you can put the type declaration in the scope of the for
- [10:16:19 PM] David: Ah so the only thing different is the i declaration
- [10:16:24 PM] Andrew Story: yea
- [10:16:52 PM] David: code ran as expected
- [10:16:56 PM] Andrew Story: You can also do for( i=0, j=0, k=1; expression; runseveryloop )
- [10:16:56 PM] David:
- int main( int argc, char* argv[] )
- {
- int f = 1 + 1;
- printf("%i", f);
- int i;
- for(i = 0; i < 100; i++){
- printf("%i", i);
- }
- return 0;
- }
- [10:17:06 PM] David: Yep that is standard as well
- [10:17:37 PM] Andrew Story: in codegolf I try to pack my entire for statement in to runseveryloop
- [10:17:41 PM] Andrew Story: =p
- [10:17:47 PM] David: Everyone does ;0
- [10:18:27 PM] David: Okay so
- [10:18:36 PM] Andrew Story: Someone pointed out to me yesterday that -~a is a+1
- [10:18:37 PM] David: What else do I need to know
- [10:18:42 PM] David: Haha cool
- [10:18:43 PM] Andrew Story: and -~-~-~a is a+3
- [10:18:46 PM] Andrew Story: main(a,b,c){scanf("%i",&b);a=b;a=a<0?a:-a;for(c=0;a<~1;a=-~-~-~a)c=-~c;b=b<0?-c:c;}
- [10:19:00 PM] Andrew Story: That takes a number and divides it by 3 without arithmetic =p
- [10:19:14 PM] David: Totally cheating
- [10:19:15 PM] Andrew Story: if() is pretty standard
- [10:19:44 PM] Andrew Story: I also have an answer that doesn't use unary negate =p
- [10:19:45 PM] Andrew Story: I(unsigned a){a=a&1?I(a>>1)<<1:a|1;}main(a,b,c){scanf("%i",&b);a=b;a=a<0?a:I(~a);for(c=0;a<~1;a=I(I(I(a))))c=I(c);b=b<0?I(~c):c;}
- [10:20:03 PM] David: if(0){}elseif(0){}else{} <- valid?
- [10:20:09 PM] David: should run last else
- [10:20:21 PM] David: mandatory space between else and if?
- [10:20:36 PM | Edited 10:20:41 PM] Andrew Story: if(expr){}else if(expr){}else{}
- [10:20:46 PM] David: K so space.
- [10:20:48 PM] David: Now
- [10:20:49 PM] David: IO
- [10:20:52 PM] Andrew Story: Keep in mind, {} is a code block
- [10:20:55 PM] David: Teach me console IO
- [10:20:58 PM] David: yes of course
- [10:21:18 PM] David: Also whats this malloc shit I keep hearing about
- [10:21:21 PM] Andrew Story: In any application other than functions and structs, codeblock markers are OPTIONAL if it's only one line
- [10:21:33 PM] David: Right like any other language
- [10:21:45 PM] Andrew Story: so you can if(expr) foo() else bar()
- [10:21:53 PM] Andrew Story: elses stick to the nearest if
- [10:23:07 PM] Andrew Story: so in
- if(expr1){
- if(expr2)
- if(expr3)
- Junk
- else
- otherjunkifnotexpr3
- } else otherjunkifnotexpr1
- [10:23:33 PM] David: That other else is for the if(expr2)
- [10:23:35 PM] David: right?
- [10:23:47 PM] Andrew Story: There's a code block specifier
- [10:24:03 PM] David: Aha.
- [10:24:09 PM] David: So therefore expr1
- [10:24:12 PM] David: right
- [10:24:13 PM] Andrew Story: expr2 is in the code block so the last else corresponds to expr1
- [10:24:27 PM] Andrew Story: That's caught a few people up
- [10:24:36 PM] Andrew Story: So you want IO?
- [10:24:38 PM] David: Yea
- [10:24:45 PM] David: Console IO and malloc...
- [10:24:59 PM] Andrew Story: Let's start with IO
- [10:24:59 PM] David: Wait
- [10:25:05 PM] David: You do know this is C and not C++ right?
- [10:25:09 PM] Andrew Story: Yes
- [10:25:09 PM] David: Just double checking
- [10:25:19 PM] David: I feel like I should have allocated some memory somewhere by now O_O
- [10:25:26 PM] Andrew Story: C++ standard IO is dumb and I never use it
- [10:25:34 PM] David: Good cause this is C
- [10:25:35 PM] David: :P
- [10:25:37 PM] Andrew Story: So
- [10:25:49 PM] Andrew Story: Basic, dumb input is gets(char*)
- [10:26:09 PM] Andrew Story: that will read user input up to a newline character and stick it in to your char* buffer
- [10:26:21 PM] David: explain my char* buffer
- [10:26:27 PM] Andrew Story: usage:
- char buffer[1000]; gets(buffer);
- [10:26:45 PM] Andrew Story: Ok
- [10:26:54 PM] Andrew Story: There are a few fundamental types
- [10:26:56 PM] David: Okay I see
- [10:26:59 PM] David: Makes sense
- [10:27:13 PM] David: So because chars and strings are by reference we can pass like that
- [10:27:45 PM] Andrew Story: functions, structs, ints/shorts/chars, floats/doubles, and pointers to any of those types, pointers to any of those pointers, etc
- [10:27:54 PM] Andrew Story: Then there's the super special type, void
- [10:28:31 PM] Andrew Story: The size of a void is always one memory unit, however you cannot use void as a type to hold values
- [10:28:39 PM] David: Uh
- [10:28:40 PM] David: why
- [10:28:42 PM] David: whats the point
- [10:29:01 PM] Andrew Story: You can only use void to specify the returning of nothing, or to specify a generic pointer value
- [10:29:09 PM] David: K
- [10:29:20 PM] Andrew Story: The point is that void* is a very neutral pointer type to use
- [10:29:47 PM] Andrew Story: anything dealing with memory pass void pointers around
- [10:29:59 PM] David: Okay
- [10:30:02 PM] David: now we get to memory
- [10:30:13 PM] David: How do we deal with memory
- [10:30:23 PM] Andrew Story: There are a few modifiers that you can use on the int family and a few for the float family
- [10:30:39 PM] Andrew Story: ints can be made short, long, unsigned, and register
- [10:30:57 PM] Andrew Story: register types try to be kept in CPU memory for fast manipulation
- [10:31:11 PM] Andrew Story: long and short should be obvious
- [10:31:25 PM] Andrew Story: (un)signed specifies whether or not to make it signed
- [10:31:36 PM] Andrew Story: You can also use short and long on floats and doubles
- [10:31:42 PM] Andrew Story: So memory
- [10:31:51 PM] Andrew Story: There's two ways to allocate memory
- [10:32:00 PM] Andrew Story: Either on the stack or on the heap
- [10:32:17 PM] David: I understand a stack and I understand a heap but I do not understand them in this context
- [10:32:33 PM] Andrew Story: allocating memory on the stack is called static allocation since the size is fixed at compile time
- [10:32:53 PM] Andrew Story: I think C99 has arrays that can change size on the stack, but I've never used them
- [10:33:27 PM] Andrew Story: The benefit of static allocation is that when the function ends, all the memory is automatically released
- [10:33:52 PM] Andrew Story: This also means that you can't return a reference to statically allocated memory that was allocated within the function
- [10:34:15 PM] Andrew Story: So
- char ret[1000];
- return ret;
- IS A BIG NO NO
- [10:34:24 PM] David: Roight
- [10:34:29 PM] David: Ahhhh
- [10:34:34 PM] David: so we'd have to malloc for that
- [10:34:37 PM] Andrew Story: Yea
- [10:34:40 PM] David: so ret could be used elsewhere
- [10:34:49 PM] David: cheeky bastard
- [10:35:03 PM] Andrew Story: Also I'd like to point out that arrays are implicitly references
- [10:35:10 PM] David: Yes you mentioned I think
- [10:35:16 PM] Andrew Story: so char ret[] is a char* underneath
- [10:35:28 PM] David: Quick question
- [10:35:29 PM] Andrew Story: char ret[][] is a char** underneath, etc
- [10:35:33 PM] David: What is a struct?
- [10:35:44 PM] Andrew Story: A collection of variables
- [10:36:10 PM] Andrew Story: Think of it like a class but without any object oriented qualities applied to it
- [10:36:47 PM] Andrew Story: So it can have any variables that it wants in it, it can also house pointers to functions, etc
- [10:37:00 PM] David: So like an interface you can poke.
- [10:37:07 PM] Andrew Story: Kinda, yea
- [10:37:08 PM] David: For lack of a better term
- [10:37:11 PM] David: Alright makes sense
- [10:37:25 PM] Andrew Story: They're useful for organizing data
- [10:37:36 PM] David: They're classes without the classes
- [10:37:40 PM] Andrew Story: yep
- [10:37:55 PM] Andrew Story: So malloc
- [10:37:59 PM] David: Mallocccc
- [10:38:33 PM] Andrew Story: malloc(size_t) will return a void pointer to a block of memory of size_t (unsigned integer) length, or a null pointer on failure
- [10:38:54 PM] Andrew Story: This is heap allocation, or dynamic allocation
- [10:39:06 PM] Andrew Story: The allocation lives for as long as the program or until it's freed
- [10:39:43 PM] Andrew Story: It's VERY important to keep track of all mallocs and make sure they're freed when you're done
- [10:39:47 PM] David: Yes
- [10:39:54 PM] David: Otherwise you could BSOD yourself
- [10:40:06 PM] Andrew Story: Well, not since windows 95 =p
- [10:40:11 PM] David: :P
- [10:40:14 PM] David: Okay so wait
- [10:40:21 PM] Andrew Story: Now the program will just likely crash if you run out of memory
- [10:40:31 PM] David: going back to the char ret[1000]; return ret; quandary
- [10:40:56 PM] David: how do I then apply the memory I want to allocate to the ret array
- [10:41:02 PM] Andrew Story: you could do char *ret = malloc( 1000 * sizeof(char) ); return ret;
- [10:41:17 PM] David: aha
- [10:41:20 PM] Andrew Story: sizeof(value) returns the size of a type in memory units
- [10:41:34 PM] David: And it just knows to make that an array?
- [10:41:44 PM] David: or is that how arrays are stored anyway?
- [10:41:46 PM] David: Oh
- [10:41:47 PM] David: Duh
- [10:41:48 PM] Andrew Story: sizeof() is a compile time operation
- [10:41:49 PM] David: yeah
- [10:41:53 PM] David: arrays are stored that way anyway
- [10:42:00 PM] Andrew Story: arrays are just pointers to a block of memory :)
- [10:42:02 PM] David: Yeah yeah
- [10:42:44 PM] Andrew Story: C conventions are kind of silly
- [10:42:55 PM] Andrew Story: When specifying a pointer, you use * to specify such
- [10:43:06 PM] Andrew Story: When you want to dereference the pointer, you use *
- [10:43:09 PM] David: Why do you need it for when you malloc but not when you []
- [10:43:14 PM] David: Explain dereference
- [10:43:26 PM] Andrew Story: you have pointer a with value 1000;
- [10:43:38 PM] Andrew Story: say it's an int pointer
- [10:43:38 PM] David: OHHHH WAIT
- [10:43:43 PM] David: IT GETS THE VALUE BACK OUT
- [10:43:49 PM] Andrew Story: You dereference a and get the int stored at 1000
- [10:43:54 PM] David: RIGHT RIGHT K
- [10:43:57 PM] David: makes sense
- [10:44:00 PM] Andrew Story: You do that via *a
- [10:44:09 PM] David: So * before a variable is deref
- [10:44:13 PM] Andrew Story: yep
- [10:44:15 PM] David: * after a data type is pointer
- [10:44:28 PM] David: and you must dereference the array to create one in memory using malloc
- [10:44:29 PM] Andrew Story: Kind of
- [10:44:45 PM] Andrew Story: When specifying multiple variables, you need a * for each pointer
- [10:44:51 PM | Edited 10:45:08 PM] Andrew Story: So int *a, *b, c, d;
- [10:45:01 PM] Andrew Story: a and b are int pointers, c and d are ints
- [10:45:29 PM] Andrew Story: [10:44 PM] David:
- <<< and you must dereference the array to create one in memory using malloc
- [10:45:36 PM] Andrew Story: Err no
- [10:45:47 PM] Andrew Story: Malloc returns the address at which it allocated memory
- [10:46:06 PM] Andrew Story: So you store that in to an address variable (pointer)
- [10:46:10 PM] David: Ohh
- [10:46:22 PM] Andrew Story: you can then dereference the pointer
- [10:46:51 PM] Andrew Story: so in the case of any pointer, you can use arrayorpointer[index]
- [10:47:03 PM] Andrew Story: that's the equivalent of *(arrayorpointer+index)
- [10:47:18 PM] Andrew Story: In C, there's "pointer arithmatic"
- [10:47:34 PM] Andrew Story: so pointer+1 isn't pointers address plus one
- [10:47:54 PM] Andrew Story: It's pointers address plus the size of the pointed to type times one
- [10:48:04 PM] David: I
- [10:48:06 PM] David: am confused
- [10:48:14 PM] David: I think I learned enough about C for the evening
- [10:48:19 PM] Andrew Story: so with a 32 bit int, then it moves the address forward by 4 bytes
- [10:48:29 PM] David: Heck, this is probably 60x more what most will know entering the class
- [10:48:37 PM] David: but I'll be back tomorrow for more lessons for sure
- [10:48:40 PM] Andrew Story: =p
- [10:48:42 PM] Andrew Story: alright
- [10:48:47 PM] Andrew Story: Have a good night sleep
Advertisement
Add Comment
Please, Sign In to add comment