Sinisterly
My First C Program - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: My First C Program (/Thread-My-First-C-Program--61643)

Pages: 1 2


RE: My First C Program - Crypt - 07-04-2015

(07-04-2015, 03:27 PM)Eclipse Wrote: I did 5 of the exercises and did the rest of it from google. This isn't a program intended for actual use, rather just a testament to the fact that I'm learning C. This thread serves no purpose other than to get feedback and improve.

We all know you guys are leet and have been since you came out of your mum's cunt, but we're not all so gifted.

How the fuck do you expect to improve when, you even said it yourself, this program has no intended use? You're not going to get any better if you make pointless programs. If your purpose is to improve, why not make something people can use?


RE: My First C Program - Eclipse - 07-04-2015

(07-04-2015, 03:41 PM)nothing.nobody Wrote: How the fuck do you expect to improve when, you even said it yourself, this program has no intended use? You're not going to get any better if you make pointless programs. If your purpose is to improve, why not make something people can use?

I literally started learning a few days ago. What the fuck am I going to make that's going to help anyone?

This thread served its purpose. The first few posts clarified some things, improved some things, and suggested ideas for other basic programs. This is all shit I wouldn't have known if I didn't post this thread. Regardless of whether or not the program is useful, it's a piece of code in C that helped me learn something I didn't know.

Of course I see your point about learning by doing; after all, that's how I learned Python, but I need to know a little more of the language before attempting something like that, and C isn't the easiest language.


RE: My First C Program - roger_smith - 07-04-2015

I guess I'm not sure what everyone expects from a thread called "My First C Program". Really? Come on people. It's assholes bashing others over their first program that stifles creativity and learning. If you can't offer CONSTRUCTIVE criticism, don't post.


RE: My First C Program - 0xDEAD10CC - 07-04-2015

(07-04-2015, 03:41 PM)nothing.nobody Wrote: How the fuck do you expect to improve when, you even said it yourself, this program has no intended use? You're not going to get any better if you make pointless programs. If your purpose is to improve, why not make something people can use?

Did you not just see that he expects to improve by taking into consideration the feedback given on his code? Who cares what the code does, if there's improvements that can be made for what the code does, then it'll help him. He doesn't have to release an open source project that everyone else can use just so that he can improve his code... You clearly have no idea here.

I could write a function which just sorts data--as a program it's pointless, but for an algorithm, there's optimizations that might help improve the code, and these will still help that person become better at writing code.

If writing and releasing code that others can use is the only way to improve your coding skill, then you obviously have a very flawed way of thinking. That's ridiculous.

@OP: Without changing too much of the code. Usage is shown when the args don't match exactly 4, so I would do this:
Code:
#include <stdio.h> #include <stdlib.h> void usage() { printf("Usage: ./postcalc.c current_post_count time posts_per_day\n"); } int main(int argc, char *argv[]) { if (argc != 4) { printf("Too many arguments.\n"); usage(); exit(1); } int current_post_count = atoi(argv[1]); int time = atoi(argv[2]); int posts_per_day = atoi(argv[3]); int result = posts_per_day * 30.436 * 12 * time; result += current_post_count; printf("In %d years, your post count will be %d.\n", time, result); return 0; }
Also, the problem with the atoi() function here, since you have no protection on the pointer passed to it, is that if the value before the conversion may cause an overflow, it results in undefined behavior. You should use strtol() instead. You could create a wrapper function to suit your needs too:

Code:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <limits.h> #define CONVERT_SUCCESS 0 #define CONVERT_FAILURE 1 #define CONVERT_OVERFLOW 2 long int str_to_base10(const char *input, char **endptr, int *p_err) { const char *p = input; int digit_found = 0, is_zero = 1, flag = CONVERT_SUCCESS; long int result = 0; while (*p && isspace(*p)) ++p; while (*p) { if (isdigit(*p)) digit_found = 1; if (*p != '0') is_zero = 0; ++p; } if (digit_found && is_zero) goto end; result = strtol(input, endptr, 10); if (!result) { flag = CONVERT_FAILURE; } else if (result == LONG_MIN || result == LONG_MAX) { flag = CONVERT_OVERFLOW; } end: if (p_err) *p_err = flag; return result; } int main(void) { char *pstr = NULL; int err; long int result = str_to_base10(" ", &pstr, &err); if (err != CONVERT_SUCCESS) { fputs("An error had ocurred.\n", stderr); exit(1); } printf("result: %ld\n", result); printf("part after parsed number: %s\n", pstr); exit(0); }

Note: This is just a basic example, there are flaws still here, in the case of base being 0, or the number being in hexadecimal form, or containing a sign for +/-... If those are important to the usage of your wrapper function, then the complexity increases and this code needs modification. Leading whitespace and the value being 0 (in base 10) is already taken care of by my function. In the case of whitespace only within the function, that is also treated as an error.


RE: My First C Program - phyrrus9 - 09-03-2015

@Eclipse I do wish to say this:

You seem primarily like a Python programmer to me, and that's all fine, but with Python, whitespace determines functional blocks. In C (and C++), we use the { and } to contain them. With Python you are forced to tab everything out correctly or it will not function, with C that is not true.

You can save yourself a lot of trouble down the line if you don't do this:
Code:
void blarg() { //somecode }

and try using this more often:
Code:
void blarg() { //somecode }

Now, this is ENTIRELY a preference type deal. If the code is more readable to you with the first format, go right ahead and do what is comfortable and easy for you. But when your statement blocks or functions start growing in size, you may find yourself in the position of "where the fuck did I miss that opening/closing brace". I just find it easier to be able to line them up without looking in 2 different columns.


RE: My First C Program - Penis - 10-09-2015

What is a TEXET? Not an exploit I've ever heard of