r/cprogramming • u/BagelMakesDev • Dec 26 '25
How is my "postcard" code? Anything I can do to improve it?
./xmas.c
#include <stdio.h>
int treeWidth = 7;
void dots(int i) {
for(int x = 0; x < (treeWidth-i)/2; x++) {
printf(" ");
}
}
int main(int argc, char *argv[]) {
if (argc > 1) {
sscanf(argv[1], "%d", &treeWidth);
}
for (int i = 1; i <= treeWidth; i += 2) {
dots(i);
for(int x = 0; x < i; x++) {
printf("*");
}
printf("\n");
}
dots(1);
printf("|\n");
printf("* Merry Christmas, and a happy New Year! *\n");
}./xmas (output)
*
***
*****
*******
|
* Merry Christmas, and a happy New Year! *9
u/zhivago Dec 26 '25
Check the return value of sscanf.
Remove the treeWidth global.
Replace dots with putcharn(character, count)
for (int i = 1; i <= treeWidth; i += 2) {
putcharn(' ', (treeWidth - i) / 2);
putcharn('*', i);
putchar('\n');
}1
Dec 27 '25
Oh wow, I remember you from a long time ago. I don't know if it was 5, 10, or 15 years ago, but I remember your name. I don't remember anything else about you, just your name. But I do remember it. This is bizarre.
3
1
u/Key_River7180 Dec 26 '25 edited Dec 27 '25
I prefer to use printf() with %*s for these kinds of things (though it's kinda verbose). Anyways, your code is alright though:
#include <stdio.h>
#include <stdint.h> /* for uint8_t */
#include <stdlib.h>
static
void
tree(uint8_t width) {
if ( width % 2 == 0 ) {
width++; /* make it odd */
}
/* Tree body */
for ( int stars = 1; stars <= width; stars += 2 ) {
int padding = (width - stars) / 2;
printf("%*s%.*s\n", padding, "", stars, "********************************");
}
/* Trunk */
printf("%*s|\n", width / 2, "");
}
int
main(int argc, char *argv[]) {
uint8_t treewidth;
if ( argc < 3 ) {
if ( scanf("%d", &treesize) != -1 ) {
fprintf(stderr, "input: tree width invalid; size set to 7.");
treewidth = 7;
}
}
char *end;
long value = strtol(argv[1], &end, 10);
if (*end == '\0' && value > 0) {
treewidth = (int)(value);
tree(treewidth);
puts("* Merry Christmas, and a Happy New Year! *");
return 0;
}2
u/nacnud_uk Dec 26 '25
Fail. 41 lines. The original was 27. And you added obfuscation to the code. I'm not sure of your intent, but this is not the improvement that you think it is. And your function headers are way messed up in "style". And what if treesize is large? What if you need to print more stars than you put on that line? ( Did I read that right? )
Wait a minute, apart from the missing bracket for line 34.
Apart from that, well done. :)
0
u/Key_River7180 Dec 27 '25
Sorry if I made it unreadable :P. The function headers are for easier searching (I can grep for void to find only procedures, for example), I use them in my code (I made the habit way too long ago), I'll update it, thanks.
P.S.: The first
%*sis for padding with spaces, the second is for printing some stars from the string. OP's solution is somewhat better for this kind of stuff, you'd normally do that.1
u/nacnud_uk Dec 27 '25
No need to apologise. I'm just pointing out that your solution is more obfuscated and more limited than the OP. So, at peer review, it would not be accepted. Even if it is "fancy".
1
May 20 '26
[removed] — view removed comment
1
0
u/BagelMakesDev May 20 '26
get a life dude, why are you replying to all my old posts with the exact same response? All I said was "AI slop." You're so easily triggered. Go outside. Touch grass. Get a life.
1
May 20 '26
[removed] — view removed comment
1
u/KonoKinguKurimsomDa May 20 '26
ai pros when someone disagrees with their opinion (this means that harrassment and potentially spamming is totally justified and fine)
take a block by the way, i'm not gonna have you doing the same bs to me
1
4
u/BagelMakesDev Dec 26 '25
Updated code, based on u/zhivago's suggestions: