#How to make your own construct in C
73 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
you're going to have to expand on that. what is the skyscraper project, and what do you mean by "make your own function"? "function" to most programmers means the constructsh return_type name(argument list) { // function_body } and, well, that's mostly how you make your own function. a trivial example could be```c
void helloWorld() {
puts("hello, world");
}
Yeah, likewise I am confused with the question.
Can you rephrase it differently?
Skyscraper puzzle solver 4x4 in C project
My bad. What I am trying to say. I saw some people are doing Skyscraper puzzle solver 4x4 in C project but they have their own construct.
I’m still in learning process about C language
I wonder how they construct
How to make your own construct in C
I can give a general, non-skyscraper example.
Let's say I have this program
#include <stdio.h>
int main() {
int input = 0;
scanf("%d", &input);
int fact = 1;
for (int i = 1; i <= input; i++) {
fact *= i;
}
printf("Factorial of %d is %d", input, fact);
return 0;
}
Really simple. It calculates the factorial of something that the user enters.
This works fine as it is.
But then I may think...
"might other parts of the code or even other people like to calculate the factorial of a given number maybe?"
Would I expect these people to copy and paste my calculation of the factorial?
Or should I do the honourable thing and refactor my code and make a new function that calculates the factorial of any given number?
That way, I have one function that my program can use.
But other parts of the code can too.
I'd have to write it only once and use it many times.
That's the fundamental purpose of a function, one could argue.
So the refactored code would become
#include <stdio.h>
int factorial(int arg)
{
int fact = 1;
for (int i = 1; i <= arg; i++) {
fact *= i;
}
return fact;
}
int main() {
int input = 0;
scanf("%d", &input);
printf("Factorial of %d is %d", input, factorial(input));
return 0;
}
Notice how function main() has come a lot shorter.
And arguably also a lot more understandable to anyone reading it?
Yeah. It is very understandable to read it
@simple dewi suggest learning a different first language
python is a great starter
you will learn it much faster and then when you eventually get back to C you will learn C much faster too
Sidenote: another compelling reason for writing functions is that you can write unit tests around the function.
Example:
Given the latter, refactored example you could:
assert(factorial(0) == 1);
assert(factorial(1) == 1);
assert(factorial(5) == 120);
// etc
You couldn't do that if all that code was written inline inside your main().
So maybe thinking "How can I test this code?" is a good way to work out how you refactor your code into functions.
i disagree. i think C is much simpler than python, and therefore can feel more approachable. that was definitely true for me, and why i picked it as a beginner language
Thank you for sharing. I learn a lot from you ☺️
From all of us?
The best way to use this Discord server is to consider many opinions and to form your own from them.
And eventually share yours back.
that's a lot of "why function" from flathead. they're all good points, but may not answer your original question. "how function" might be better answered with this video, which (from what i saw of it) goes over "how function" in somewhat exhaustive detail (and what he doesn't mention there, he notes that he goes over in the next video)
#coding #programming #cprogramming
void happyBirthday(char name[], int age){
printf("\nHappy birthday to you!");
printf("\nHappy birthday to you!");
printf("\nHappy birthday dear %s!", name);
printf("\nHappy birthday to you!");
printf("\nYou are %d years old!\n", age);
}
int main() {
// function = A reusable section o...
True
@simple dew Has your question been resolved? If so, type !solved :)
i do want to say, though: really good explanation. downright inspired, even
A few thing to keep in mind to write great functions.
a) Not everything should be a separate function. Write functions when and where they make sense.
b) Write functions that "do one thing only". Cursed are those functions that try to do more than one thing, and I have written my fair share of them in my past.
Writing unit tests for functions that try to be too clever will often point out your mistake.
Disclaimer: gaming code is a discipline that often throws out these rulebooks, so consider this advice as guidelines only.
Thank you for sharing. I appreciate it ☺️
solved:)
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
thats just wrong
i am gonna be honest this is just objectively wrong
in python you dont need to worry about syntax or pointers. everything is much simpler thats why python is used because its fast to code in.
you don't have to worry about syntax or pointers
are we talking about the same programming language? references are pointers, just silently so; as such, you need to worry about them much more than in C. as to "don't have to worry about syntax"; although python has fewer keywords, the python built in types are hidden, and there are quite a few more of them. this adds complexity (and IMHO confusion). C has no built in functions (well, C89 auto-includes all of its headers, i guess). C has no notion of any sequence operations, beyond a few functions instring.h. C doesn't have iterators, unless you countva_arg(). C doesn't have exceptions, unless you countexit(). the python standard library is what is commonly referred to as a "kitchen sink" library -- the thing is pretty large. there are (by my rough estimation) at least 5x more standard library modules in python than there are standard library headers in C. even in the fundamental design decisions of the language, C is simpler -- it was designed with less context (i.e. no context-specific keywords)
to me, all this means that C is far and away simpler than python. i realize at this point in writing all of this that i should have led off with a definition of "simple", because i suspect our definitions differ. i borrow my definition from this video. by the definitions therein, python may be easy (and i have no doubt that it is), but it is not simple
I wish C and C++ had named arguments, but I know it'll never happen for sane reasons.
It's the topmost - possibly only - thing about Python that I envy.
You have to understand about how the lists and dics work
And also care about shallow/deep copy
Syntaxes appear to be simple but they'are actually not as simple as they seem
lists and dictionaries are the easiest things ever in python
the idea that C is a better starting language is like saying assembly is better than C as a starting language. its not.
That line of argumentation isn't valid, because with that exact same argumentation I could argue that starting with Python is worse than starting with ChatGPT prompts, because these are even easier to understand and in even more natural language
chatgpt isnt a programming language
and its a valid argument
C is objectively not a better starting language. its much more difficult than python.
it's much more difficult than python
Now that's just a blatant lie (except for the C preprocessor). C is extremely simple. Each statement can be easily translated to assembly.
Python is much more complex, just look at e.g. this Python code:
class Composition:
def __init__(self, f):
self.f = f
def __rshift__(self, g):
def wrapper(*args):
return g(self(*args))
return Composition(wrapper)
def __call__(self, *args):
return self.f(*args)
@Composition
def compose(*args):
return args[0] if len(args) == 1 else args
# example function
def flatten(*xss) -> list:
return [x for xs in xss for x in (flatten(*xs) if isinstance(xs, (list, tuple, set)) else (xs,))]
# example for function composition
none_nested = compose >> flatten >> any >> (lambda x: not x)
none_nested_print = compose >> none_nested >> print
# example for function composition calls
print(none_nested([[0, False], [("",)]])) # prints True
none_nested_print([0, [1]]) # prints False
All of this is pure Python, i.e. Python without needing to import anything.
C, without including any libraries, is MUCH simpler in what it can do
great you made incredibly complicated code that exists in 0.0001% of cases in python.
you are basically saying python has worse syntax than c and SO FOR SOME REASON the professional industry uses it FOR NO REASON by your logic NO REASON for python. why dont they use c? i mean come on its better at everything!!!!@!11!1 its not. python is a much better starting language.
the reason people use python is because its fast to code in, and its simple. and has readable syntax. THATS WHY IT EXISTS, THATS WHY COMPANIES USE IT
i mean go ahead tell some kid to start in c he wont understand shit go to chatgpt and chatgpt wont help him and he will get bored of programming and there he goes never coming back to it
also i am serious about this. code like that basically never exists in python.
there is a point to why python was created to be simple to read. so a hard to read piece of python is probably quite rare
"but nah you know? python is just worse at everything! even for beginners you know? IT HAS HARD TO READ CODE I MEAN COMEON C IS EASIER TO READ!11!!"
I love reading such passionate opinions.
I lament that they have to be expressed in a beginner's thread who isn't helped by this [IMO].
what passionate? his opinion is blatantly disingenuous, he says "uh but you see this python code is horrible to read and write absolutely horrific for beginners." as if python is a fake language that is not only slow but also bad to write and read. as if it has no purpose in this world.
if anything he is just making this thread worse, by being disingenuous
you are basically saying python has worse syntax than c
Never said that
SO FOR SOME REASON the professional industry uses it FOR NO REASON by your logic NO REASON for python
Chill down little boy. I never said that Python is bad. You're interpreting stuff into things.
why dont they use c? i mean come on its better at everything!!!!@!11!1
Never said that.
python is a much better starting language.
That's the original point we were talking about, yet you just repeat your opinion and apparently categorize all my other arguments as non-relevant regarding this opinion of yours, which is absurd from your point of view.
the reason people use python is because its fast to code in, and its simple. and has readable syntax.
I never said otherwise
THATS WHY IT EXISTS, THATS WHY COMPANIES USE IT
Again: Chill down little boy
i mean go ahead tell some kid to start in c he wont understand shit
If you get yourself a good book you will "understand shit"
go to chatgpt and chatgpt wont help him
People not being able to properly use LLMs is a common phenomenon and is the same across all languages. Like 30% of the questions in the Python Discord used LLMs to generate code. Anyone who uses ChatGPT to solve their homework/assignments for them is an idiot. Period.
and he will get bored of programming and there he goes never coming back to it
Ok. Perfect. More jobs for me.
also i am serious about this. code like that basically never exists in python.
Obviously that exact code example probably isn't common, but it showcases a bunch of functionalities Python provides like list-comprehension,
there is a point to why python was created to be simple to read
Again: Never said otherwise
so a hard to read piece of python is probably quite rare
🐒 Have you ever seen production Python code?
"but nah you know? python is just worse at everything! even for beginners you know? IT HAS HARD TO READ CODE I MEAN COMEON C IS EASIER TO READ!11!!"
You're quoting me wrong cause I never said that. The only part of that, that is actually from me (somewhat) is the "Python is worse for beginners" part. Also again: Chill out little boy.
Python is fine as a first language if all you want to do is write small scripts or proofs of concepts, but Python is a horrible first language for a computer scientist.
Python doesn't teach you anything about how the computer works.
As a tutor I used to speak with a multitude of people who started their comp science journey with Python and ~70% of them told me they wish they had started with another language. Typical areas of complaint were static vs dynamic typing, lists vs arrays, a real (project) structure, and a big ick of me personally is the GIL (don't get me wrong, I love Python, but the GIL sucks).
It's very similar for people starting with JavaScript, although so far I've only talked to 2 or 3 people who started with JS.
oh i am chill. you saying "chill out little boy" every time i use caps is infact to show how inaccurate you are. you say "have you ever seen production python code" by that i guess you mean ultra complicated unreadable shit? then why do they use python? is it still possibly hypothetically lets think this out. (maybe actually still alot better than other langauges) or are these multi billion dollar companies wasting their money on slow unreadable code. (production python code is very readable)
If you get yourself a good book you will "understand shit" a good book is still completely useless to a complete beginner, as a person who doesnt know anything it will be hard to start with C as of its low level nature, python is much easier since its a true high level language where you dont need to worry about the low level aspects of programming
"People not being able to properly use LLMs is a common phenomenon and is the same across all languages. Like 30% of the questions in the Python Discord used LLMs to generate code. Anyone who uses ChatGPT to solve their homework/assignments for them is an idiot. Period." 100% of the people in this server said to me chatgpt doesnt help with c c++.
"Ok. Perfect. More jobs for me." why are you here to help people if you are serious about this?
"Obviously that exact code example probably isn't common, but it showcases a bunch of functionalities Python provides like list-comprehension, " ok so dont use it as an example that python is unreadable.
"🐒 Have you ever seen production Python code?" first point.
you are saying this as if you need to know how a computer works in the deep end as if its a necessary piece of knowledge for anyone using high level languages like java python and javascript. its not, its not a necessary piece of knowledge to know how the computer works below and python is a great way to initially learn the idea of what programming is and then start learning the truth behind it.

🤓
assembly is arguably more complex than C TBH. there are 1-4 thousand assembly mnemonics (depending on how you count), and the build process for getting anything useful out of assembly language is a bit more complex than for C. the only reason assembly is arguably more complex is that assembly is just about as context-free as it is possible to be
the GIL is optional (the proposal for that is PEP 703), although the feature to turn it off is still very experimental and not really supported by many big libraries
did i say otherwise?
fair enough. my central argument is that C is a better beginner language because it is simpler than python. [this reply](#1372165744044937269 message) contains a clarifying argument as to why i don't just argue that assembly is the best beginner language
I can't say how good it is for beginners (I went TI-BASIC > tiny amount of C++ > python > C; with a lot of general computer science and more practical understanding of computers in between each language), but it's great for at least your second language; probably gonna be lower level than whatever else you know, which teaches you basics about how a computer works; simpler than python in some ways for sure, but some things you might want to do easily become more complex, and asm takes that a lot further, for better or worse, but probably a bit too far I'd say (idk why i typed out this massive answer here but it's too long to not post)
doesn't have to be x64 asm does it?