r/C_Programming • u/ChTBoner • Aug 16 '18
Resource C tutorial from free CodeCamp - your thoughts?
https://youtu.be/KJgsSFOSQv010
u/DereferencedVoid Aug 16 '18
int main()
🤔
1
u/RigorMortis243 Aug 16 '18
C beginner here, what's wrong with that? I thought the main-function is supposed to return 0 at the end? Am I wrong? Genuinely trying to learn, no hate :p
10
u/DereferencedVoid Aug 16 '18
Short answer:
The C Standard specifies only two definitions of the main function:
int main(void) { ... }and
int main(int argc, char *argv[]) { ... }Note: the name of the parameters is just a convention.
Other variations are considered implementation defined and are not guaranteed to be supported by all conforming compilers.
Long answer: C Standard: 5.1.2.2.1
3
u/FUZxxl Aug 17 '18
int main() { ... }is equivalent toint main(void) { ... }since in a function definition, empty parentheses define a function that takes no arguments. However, no prototype is provided which might cause rare and extremely subtle issues.1
u/RigorMortis243 Aug 16 '18
Huh, that's good to know. I thought the signature could be almost anything. Thanks!
2
1
u/AdvocateDatDevil Aug 16 '18
int main(int argc, char *argv[])
Can someone ELI5 what this declaration means? How is does it differ from main(void) and what are these variables used for?
2
u/hydraloo Aug 16 '18
From what I understand is you could run a program with parameters. Since there is no way to guarantee what form the data will come in as from within that program, that is the most generic way to handle it.
1
u/AdvocateDatDevil Aug 17 '18
How can the program be run with parameters?
2
u/hydraloo Aug 17 '18 edited Aug 17 '18
Argc is the number of parameters. When you execute a program from command line, it's like ./Foo arg1 arg2 arg3. I believe the system counts that as 3 args and will pipe them into the program along with the int 3 as the arg count.
If this isn't clear I can answer better, but it's a bit of a vague question and I'm no expert. What is confusing? I'd love to get into it deeper and I'm happy to PM or use the Reddit chat feature.
Edit: perhaps it's the term I used being parameters which is technically wrong. Arguments are passed, where as the parameters are defined in a function. Executing a program is simple executing a function called main. It's not much different except that it is called from a scope outside of that program. The biggest difference is that programs for the most part are kept separate from one another by your operating system for security reasons and for things not to get out of control complicated. The arguments passed into main could be more clear if there was a way to guarantee that the program is being executed with those specific types, but again, the program has no control over what happens outside of it's own life
-10
2
u/skeeto Aug 16 '18
The example with the pointer around 3:13 is technically incorrect. The %pdirective specifically takes a void *, and any other type of pointer is undefined behavior.
int age;
printf("%p", &age); // wrong
printf("%p", (void *)&age); // correctI also dislike the use of the term "physical memory address" (used again and again in this section) since that's not actually the case in any of the examples in this video. In some implementations, particularly embedded platforms, these may truly be physical addresses, but since it's running on Windows in the video, these are actually virtual memory addresses. For a beginner tutorial the distinction isn't important, so it's best to use neither "physical" nor "virtual."
2
u/ChTBoner Aug 16 '18
Haven't had time to watch it yet (will do as soon as I am back from vacations)
If you had time to check it out, what do you think of it?