r/c_language • • Apr 26 '13

I'm a complete beginner in C, but it made me feel cool when I recognized what that little typo was!

Thumbnail
imgur.com
0 Upvotes

r/c_language • • Jan 03 '26

Programming objects in C in style of Go

0 Upvotes

Hi there,

What do you folks think about this approach of programming objects in C? It supports non-virtual methods and private members.

goc.h

#ifndef GOC_H
#define GOC_H

#define fn(obj, fn, ...) (obj).fn(&(obj), ##__VA_ARGS__)

#endif

woman.h

#ifndef WOMAN_H
#define WOMAN_H

#include "goc.h"

struct woman;

typedef struct w {
    struct woman *w;
    void (* hug)(struct w *self);
    void (* kiss)(struct w *self);
} woman;

woman new_woman(char *);
void free_woman(woman *);

#endif

woman.c

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "woman.h"

struct woman {
    char *name;
    bool hugged;
    bool kissed;
};

void hug(woman *self) {
    if (self->w == NULL) {
        printf("No woman allocated!\n");
        return;
    }

    printf("You're hugging %s\n", self->w->name);

    if (self->w->hugged) {
        printf("%s say: It hurts, get off me!\n", self->w->name);
        return;
    }
    if (self->w->kissed) {
        printf("%s say: Get off me, creep!\n", self->w->name);
        return;
    }

    self->w->hugged = true;
    printf("%s say: you're sweet!\n", self->w->name);
}

void kiss(woman *self) {
    if (self->w == NULL) {
        printf("No woman allocated!\n");
        return;
    }

    printf("You're kissing %s\n", self->w->name);

    if (!self->w->hugged) {
        if (self->w->kissed) {
            printf("%s is calling the police!\n", self->w->name);
            return;
        }
        self->w->kissed = true;
        printf("%s say: How dare you?\n", self->w->name);
        return;
    }
    if (self->w->kissed) {
        printf("%s say: Level up, boy!\n", self->w->name);
        return;
    }

    self->w->kissed = true;
    printf("%s say: Mwah!\n", self->w->name);
}

woman new_woman(char *name) {
    struct woman *w = calloc(1, sizeof(struct woman));
    w->name = name;
    woman women = {
        .w = w,
        .hug = hug,
        .kiss = kiss
    };
    return women;
}

void free_woman(woman *w) {
    free(w->w);
    w->w = NULL;
}

main.c

#include <stdio.h>

#include "woman.h"

int main(void)
{
    woman woman;

    printf("\033[1mThe right workflow:\033[0m\n");

    woman = new_woman("Alice");
    fn(woman, hug);
    fn(woman, kiss);
    fn(woman, kiss);
    free_woman(&woman);

    putchar('\n');

    printf("\033[1mThe wrong workflow:\033[0m\n");

    woman = new_woman("Miranda");
    fn(woman, kiss);
    fn(woman, hug);
    fn(woman, kiss);
    free_woman(&woman);

    putchar('\n');

    printf("\033[1mThe dreaming workflow:\033[0m\n");

    fn(woman, hug);
    fn(woman, kiss);
    fn(woman, kiss);

    return 0;
}

This program prints:

The right workflow:
You're hugging Alice
Alice say: you're sweet!
You're kissing Alice
Alice say: Mwah!
You're kissing Alice
Alice say: Level up, boy!

The wrong workflow:
You're kissing Miranda
Miranda say: How dare you?
You're hugging Miranda
Miranda say: Get off me, creep!
You're kissing Miranda
Miranda is calling the police!

The dreaming workflow:
No woman allocated!
No woman allocated!
No woman allocated!

r/c_language • • Oct 14 '26

C language

0 Upvotes

I am btech first year student and I want to learn C language. How can I learn it in easy way , can anyone suggest who have knowledge about it ????


r/c_language • • Dec 19 '23

The ONLY C keyword with no C++ equivalent

1 Upvotes

Interesting fact - there is a C keyword restrict which has no equivalent in C++

https://www.youtube.com/watch?v=TBGu3NNpF1Q


r/c_language • • Jun 10 '18

Interesting Facts about Macros and Preprocessors in C

Thumbnail
geeksforgeeks.org
2 Upvotes

r/c_language • • Sep 28 '14

how to use getchar() and putchar() in C?

0 Upvotes

i would really like to know how it is used to like read a phrase like how would that work say i input " The cat is gone" how can i make it so it only reads letters and not the spaces?


r/c_language • • Aug 13 '20

If you looking for jobs related to C Language, Here is a list of a few job openings for developers that auto-updates every day!

Thumbnail docs.google.com
0 Upvotes

r/c_language • • Nov 09 '15

[Beginner] Why it write out all chars?

0 Upvotes

I'm doing school homework in C. Program should rewrite input without brackets. My problem is that I don't know why program writes brackets even though I exluded them in if statement.

http://pastebin.com/xrhi6ABg


r/c_language • • Dec 30 '16

Purpose of completing move() in game of fifteen

0 Upvotes

I am supposed to complete a move() function in fifteen.c, a program with the goal of implementing Game of Fifteen. I just don't see where/how the move() function itself is used in the program.

int main(int argc, string argv[]) { // ensure proper usage if (argc != 2) { printf("Usage: fifteen d\n"); return 1; }

// ensure valid dimensions
d = atoi(argv[1]);
if (d < DIM_MIN || d > DIM_MAX)
{
    printf("Board must be between %i x %i and %i x %i, inclusive.\n",
        DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
    return 2;
}

// open log
FILE* file = fopen("log.txt", "w");
if (file == NULL)
{
    return 3;
}

// greet user with instructions
greet();

// initialize the board
init();

/* Sort the board using sort() from prvs*/
 for (int g = 0; g < d; g++)
 {
     sort(board[g], d);
 }
 
// accept moves until game is won
while (true)
{
    // clear the screen
    clear();

    // draw the current state of the board
    draw();

    // log the current state of the board (for testing)
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            fprintf(file, "%i", board[i][j]);
            if (j < d - 1)
            {
                fprintf(file, "|");
            }
        }
        fprintf(file, "\n");
    }
    fflush(file);

    // check for win
    if (won())
    {
        printf("ftw!\n");
        break;
    }

    // prompt for move
    printf("Tile to move: ");
    int tile = GetInt();
    
    // quit if user inputs 0 (for testing)
    if (tile == 0)
    {
        break;
    }


    /*Search for tile position*/
    for (int h = 0; h < d; h++)
    {
        keybinsearch(tile, board[h], d);
    }
    
    /*Get BLS postion*/
    for (int k = 0; k < d; k++)
    {
        keybinsearch( 0,board[k], d);
    }
    
    
    
    
    // log move (for testing)
    fprintf(file, "%i\n", tile);
    fflush(file);

    // move if possible, else report illegality
    if (!move(tile))
    {
        printf("\nIllegal move.\n");
        usleep(500000);
    }

    // sleep thread for animation's sake
    usleep(500000);
}

// close log
fclose(file);

// success
return 0;

}

Thanks a lot,


r/c_language • • Jul 28 '19

Linked list

0 Upvotes

What's the difference between return *node; and return node; in a linked list function. : Node declared as Struct node{ Int key; Struct node *next; }

Also what's the difference between the above structure definition and the below one:

Struct node{ Int key; Struct node* next; }


r/c_language • • Feb 26 '23

How to remove the zero

Thumbnail
gallery
0 Upvotes

r/c_language • • Nov 23 '19

If you think goto is a bad idea, what would you say about longjmp?

Thumbnail
engineering.redislabs.com
0 Upvotes

r/c_language • • Feb 03 '14

Think of CppCat as a compiler extension which allows you to find a bit more errors in your code

Thumbnail
cppcat.com
0 Upvotes

r/c_language • • Nov 29 '15

This snippet of working C code is supposed to check if an array index contains a value. How does it work?

0 Upvotes
for (i = 0; i < nrOfDice; i++){
    dieValues[dice[i] - 1] = dieValues[dice[i] - 1] + 1;
}

And why does it work?

its part of a function called

void printScores(const int dice[], int nrOfDice, int nrOfDieValues){

r/c_language • • May 10 '11

C to Go

Thumbnail www-cs-students.stanford.edu
0 Upvotes

r/c_language • • Feb 04 '23

help pls c Lang beginner

Thumbnail
gallery
0 Upvotes

r/c_language • • Oct 16 '24

The sheer chaotic gay energy of the c std lib contributors is an art that needs to be studied

Thumbnail
gallery
0 Upvotes

These are both from the same article, what two features did the author add that deserved such incredible documentation, bit utilities and an auto type. This will forever be one of the reasons I love this language (Source article btw: https://thephd.dev/c23-is-coming-here-is-what-is-on-the-menu)


r/c_language • • May 29 '20

Whic is best IDE for c language?

3 Upvotes

Which*


r/c_language • • Jun 28 '23

How to learn c language from scratch

0 Upvotes

The objective is to make a student who know ntg about computers


r/c_language • • Mar 25 '16

Obfuscated Coding - C Program To Print India's Map

Thumbnail
codingalpha.com
3 Upvotes

r/c_language • • Aug 13 '14

Kit, a project manager for C

Thumbnail
github.com
2 Upvotes

r/c_language • • May 04 '16

Help to stop buffer overflow

0 Upvotes

how can i prevent buffer overflow in this code?

include <stdio.h>

int main() { int i = 0; char str[8];

do{
 str[7] = '\0';
 printf("Enter 7 characters:\n");
 scanf("%s",&str);
 printf("\nYou entered: %s\n", str);

}while(str[7]!='\0');

if(i == 12336)
printf("i is %d. You Win\n", i);

else printf("i is %d. You Lose\n", i);

}


r/c_language • • Dec 20 '14

how to make a subwin in ncurses ?? i box( ) the subwin but i can't see it .. why ?

0 Upvotes

r/c_language • • May 02 '25

I am lost in learning c please help.....

2 Upvotes

The problem is that i know a bit basic c, i learned it on different years of my school and collage years/sems,

2 times it was c and one time it was cpp, they only teach us basic stuff,

like what are variables, functions, loops, structures, pointers, etc etc, basic of basic,

so now i'm mid-sem of my electronics degree, i wanted to take c seariosly, so that i have a confidence that i can build what i want when i needed to,

so what i wanna learn is max c99 since i heard that's the max that is used in embedded world,

so after reading the wiki, i started reading the " c programming a modern approach"

the problem is every chapter has more things for me to learn, but the problem is i know basics, so it's boring to read, i mean some times things dont even go inside my mind, i read like >100 pages of it,, out of 830 pages,

then i tried k&r but i heard there are some errors on it so i quit,

then i tried the handbook for stanford cs107 course, it was too advance so i had to quit it too,

I know what i have to learn next, like , i should learn memmory allocation and stuff, (malloc etc....)
i learned about a bit of structures on c++ so i have to relearn it on c,

i have to dive deep into pointers and stuff,

and other std library functions and stuff,

and a bit more on data structures,

and debugging tools etc etc

i mean those won't even be enough i also wanna learn best practices and tips and tricks on c,

like i mean i didn't even know i couled create an array with pointers,

it was also my first time knowing argc and argv on main function, i leart that while reading cs107,

so how do i fill my gaps .......,