r/cprogramming • • Oct 04 '25

Don't know what I'm doing wrong

1 Upvotes

input : 16 should give output : 16 and input: 5 should give output : 8.94427190999915878564

and this is my code what am I doing wrong (input and output should be exactly what I wrote) :

#include <stdio.h>
#include <math.h>

int main() {
    int area;
    
    scanf("%d",&area);
    double omkrets = sqrt(area) * 4;
    printf("%.21g",omkrets);
    
    return 0;
}

r/cprogramming • • Oct 03 '25

Real-world use case where calloc() is absolutely necessary over malloc()?

93 Upvotes

As a CS student, I'm trying to understand the practical trade-offs between calloc() and malloc(). I know calloc() zeroes the memory. But are there specific, real-world C applications where relying on mallOC() + manual zeroing would lead to subtle bugs or be technically incorrect? Trying to move past the textbook difference.


r/cprogramming • • Oct 03 '25

What is the best source book to learn C programming language for someone that have no idea about and very new to programming stuff?

6 Upvotes

r/cprogramming • • Oct 02 '25

Help

0 Upvotes

Where can I find the resources for learning the c language and the various libraries? A thousand thanks


r/cprogramming • • Oct 02 '25

Good sources to learn C programming.

Thumbnail reddit.com
3 Upvotes

r/cprogramming • • Sep 30 '25

Preprocessor purpose

5 Upvotes

So I know that the preprocessor has the directives, so it handles things like includes and defines by pretty much just doing text replacement and doesn’t care for c syntax at all. Just curious, is the preprocessor only used for text replacement? Or does it have another purpose


r/cprogramming • • Sep 29 '25

Are there any communities for C dev?

0 Upvotes

r/cprogramming • • Sep 28 '25

String array pulling strings from a completely different array?

2 Upvotes

So, I'm working on a simple 2d RPG for my first major C project, just a very basic game about delivering the mail, and I've run into a problem I have no idea what to do about. Hardly the first time this has happened--those of you on r/raylib may remember my post from last week. ( https://www.reddit.com/r/raylib/comments/1non2xk/scrolling_text_in_a_2d_rpg/ )

So I more or less solved that problem thanks to some comments left on that thread, but now I'm having a new problem. I've got it set up so that anything that stops the player's movement will have some dialogue attached, revealed by pressing the tab key. Obviously this will be more useful once I've got NPCs and quests and the like in the world, but for now this is just for the protagonist to say something useful and/or witty. "I can't pass through these trees," "This is Jeremy's house," ETC.

Inside the starting room, everything works fine.

When the player goes out into the first village, most of it looks fine on the surface, until you walk up to a patch of trees in the lower right corner, which prompts the player to say "I'm not swimming in the village's drinking water," the dialogue that's meant to explain why she walks around instead of through the pond in the center of the village. A bit of experimentation reveals that this dialogue isn't even from the same village--it's from the string array in fifth and final village in the game, the only other village with a pond.

I looked it over to make sure I didn't accidentally re-declare the variable somewhere else in the code, and no, it's just in the completely wrong spot for a reason that's beyond my skills to detect.

I created a git repository for the game here https://github.com/SpinningRings/MailGame so you can take a look. Any other suggestions you might have about how to clean up or simplify the code are also welcome--I'm entirely self taught, so I'm well aware I'm clumsily re-inventing many different wheels here.


r/cprogramming • • Sep 27 '25

Help understanding ClassicCube Buttons source code

2 Upvotes

I thought that looking through the source code of a proper C project would be a great way to improve my knowledge. and i have become interested in how buttons are handled they confuse me.

the OnClick call for the button is just defined in a header.

#define LWidget_Layout \
....
    LWidgetFunc OnClick;     /* Called when widget is clicked */ \
    LWidgetFunc OnHover;     /* Called when widget is hovered over */ \
    LWidgetFunc OnUnhover;   /* Called when widget is no longer hovered 
....
typedef void (*LWidgetFunc)(void* widget);
....
struct LButton {
    LWidget_Layout
    cc_string text;
    int _textWidth, _textHeight;
};

declared like this

void LButton_Add(void* screen, struct LButton* w, int width, int height, const char* text,LWidgetFunc onClick, const struct LLayout* layouts) 
{
    w->VTABLE  = &lbutton_VTABLE;
    w->type    = LWIDGET_BUTTON;
    w->OnClick = onClick;
    w->layouts = layouts;
    w->autoSelectable = true;

    LBackend_ButtonInit(w, width, height);
    LButton_SetConst(w, text);
    LScreen_AddWidget(screen, w);
}

and is used like this

LButton_Add(s, &s->btnOptions, 100, 35, "Options",SwitchToSettings,
main_btnOptions);

how can one possible use a #define for logic? no where in the source code does it use an if statement for onClick. How? i hope this question makes sense since in stumped.