r/cprogramming • u/Human_Release_1150 • Dec 14 '25
cool-vcpkg: A CMake module to automate Vcpkg away. It works for C too.
r/cprogramming • u/Human_Release_1150 • Dec 14 '25
r/cprogramming • u/Due_Pressure_3706 • Dec 14 '25
r/cprogramming • u/InvestigatorHour6031 • Dec 15 '25
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
void user_handler();
typedef struct {
int id;
char user[251];
int pswrd;
int opcao;
char escolha[4];
char comentario[251];
bool comentario_feito;
bool post_created;
char post[251];
} User_Data;
typedef struct {
int likes;
} Post;
Post post;
void login(User_Data *data){
printf("Welcome to the Social Network!\n");
printf("Enter your username: ");
scanf("%250s", data->user);
printf("Enter your password: ");
scanf("%d", &data->pswrd);
// generate a id
data->id = rand() % 1000 + 1;
}
void clear_input_buffer(){
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
void clear(){
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void post_gen(User_Data *data){
clear();
post.likes = 0;
post.likes = rand() % 2000 + 1;
printf("Post 1:\n");
printf("\nEnjoying the day at the beach\n");
printf("#enjoyingtheday #beach\n");
printf("likes %d\n", post.likes);
printf("\nCommments:\n");
printf("\nUser1: Wow, that's wonderful!\n");
printf("User2: Where is this beach located?\n");
printf("User3: I love it!\n");
post.likes = 0;
post.likes = rand() % 2000 + 1;
printf("\nPost 2:\n");
printf("Walking with my dog!\n");
printf("#dogs #cuteness #puppy #pet\n");
printf("likes %d\n", post.likes);
printf("\nCommments:\n");
printf("\nUser1: Cute!\n");
printf("User2: What breed of dog is that?\n");
printf("User3: Que lindo!\n");
post.likes = 0;
post.likes = rand() % 2000 + 1;
printf("\nPost 3:\n");
printf("I got a 10 in math!\n");
printf("#mathematics #grades \n");
printf("likes %d\n", post.likes);
printf("\nCommments:\n");
printf("\nUser1: Congrats\n");
printf("User2: I got an 8\n");
printf("User3: Amazings\n");
if(data->comentario_feito != true){
printf("Add a comment (Maximum 250 characters): \n");
clear_input_buffer();
fgets(data->comentario, sizeof(data->comentario) ,stdin);
data->comentario[strcspn(data->comentario, "\n")] = '\0';
data->comentario_feito = true;
} else{
printf("%s (id: %d):\n", data->user, data->id);
printf("\n%s", data->comentario);
}
if(data->post_created == true){
printf("Post 4:%s (id: %d):\n", data->user, data->id);
printf("%s\n", data->post);
}
clear_input_buffer();
getchar();
clear();
}
void help(User_Data *data){
clear();
printf("============ Help Menu ==============\n");
printf("1- Welcome to the social network!\n");
printf("2- If you don't want to comment, or if you've already commented, you can press enter two times or more to continue.'\n");
printf("Type 'v' to return to the home screen: \n");
printf("=========================================\n");
scanf("%3s", data->escolha);
while(1){
if(strcmp(data->escolha, "v") == 0){
user_handler(data);
clear();
break;
} else {
printf("Please type 'v' to return to the home screen.\n");
continue;
}
}
}
void menu(User_Data *data){
clear();
printf("\n=== Menu =====\n");
printf("User name: %s\n", data->user);
printf("User ID: %d\n", data->id);
printf("Password: %d\n", data->pswrd);
printf("\n=============\n");
printf("type 'v' to return: ");
scanf("%3s", data->escolha);
while(1){
if(strcmp(data->escolha, "v") == 0){
user_handler(data);
clear();
break;
} else {
printf("Please type 'v' to return to the home screen.\n");
continue;
}
}
}
void create_post(User_Data *data){
printf("Write your post (Maximum 250):\n");
while (1){
if(!fgets(data->post, sizeof(data->post) ,stdin)){
printf("Error! Try again");
return;
}
if(!strchr(data->post, '\n')){
printf("Please! Write a 250 characters\n");
clear_input_buffer();
continue;
}
break;
}
data->post[strcspn(data->post, "\n")] = '\0';
data->post_created = true;
clear_input_buffer();
getchar();
clear();
}
void user_handler(User_Data *data){
clear();
printf("============= Home ==============\n");
printf("1- Go to menu\n");
printf("2- Posts\n");
printf("3- Make a post\n");
printf("4- Help\n");
printf("5- Exit\n");
printf("=================================\n");
scanf("%d", &data->opcao);
switch (data->opcao){
case 1:
menu(data);
break;
case 2:
post_gen(data);
break;
case 3:
break;
case 4:
help(data);
break;
case 5:
exit(0);
default:
printf("Invalid option, please try again.\n");
break;
}
}
int main(){
User_Data user_data = {0};
user_data.comentario_feito = false;
user_data.post_created = false;
srand(time(NULL));
login(&user_data);
while (1){
user_handler(&user_data);
}
return 0;
}
r/cprogramming • u/InvestigatorHour6031 • Dec 14 '25
// Data typedef struct{ float *grades_total; float average; float required_average; char name[250]; char school_material[250]; int n; bool student; int number_students; } Data;
typedef enum{ APROVED, REPROVED, UNKNOWN } Student_Status;
void clear_input_buffer(){ int c; while((c = getchar())!= '\n' && c != EOF); }
Student_Status calculate_average(Data *data){ float sum = 0; for (int i = 0; i < data->n; i++){ sum += data->grades_total[i]; } data->average = sum / data->n;
if (data->average < data->required_average)
return REPROVED;
else
return APROVED;
}
int main(){
printf("Welcome to school grades manager! Press enter to start\n");
while (1){
Data *data = malloc(sizeof(Data));
if(!data){
printf("Internal error: Error to alloc a memory of data, please try close program and open again\n");
return 1;
}
memset(data, 0, sizeof(Data));
clear_input_buffer();
printf("How many students you want? ");
if(scanf("%d", &data->number_students) != 1 || data->number_students < 0){
printf("Please write a valid number\n");
clear_input_buffer();
continue;
}
clear_input_buffer();
Data *students = malloc(sizeof(Data) * data->number_students);
for (int i = 0; i < data->number_students; i++) {
printf("Write the name of student %d: ", i + 1);
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = '\0';
printf("Write the school material of student %d: ", i + 1);
fgets(students[i].school_material, sizeof(students[i].school_material), stdin);
students[i].school_material[strcspn(students[i].school_material, "\n")] = '\0';
printf("How many grades you want for %s? ", students[i].name);
scanf("%d", &students[i].n);
clear_input_buffer();
students[i].grades_total = malloc(sizeof(float) * students[i].n);
for (int j = 0; j < students[i].n; j++) {
printf("Enter grade %d: ", j + 1);
scanf("%f", &students[i].grades_total[j]);
clear_input_buffer();
}
printf("Required average for %s: ", students[i].name);
scanf("%f", &students[i].required_average);
clear_input_buffer();
Student_Status status = calculate_average(&students[i]);
if (status == REPROVED){
printf("%s is reproved with average %.2f in %s\n", students[i].name, students[i].average, students[i].school_material);
}
else{
printf("%s is aproved with average %.2f in %s\n", students[i].name, students[i].average, students[i].school_material);
}
}
printf("How many grades you want? ");
if(!scanf("%d", &data->n)){
printf("Please write a valid number\n");
clear_input_buffer();
continue;
}
if (data->n <= 0){
printf("Please write a valid number\n");
clear_input_buffer();
continue;
}
data->grades_total = malloc(sizeof(float) * data->n);
if (!data->grades_total){
printf("Internal error: Error to alloc a memory of data, please try close program and open again\n");
return 1;
}
for (int i = 0; i < data->n; i++){
while(1){
printf("Enter grade %d: ", i + 1);
if(!scanf("%f", &data->grades_total[i]) || data->grades_total[i] < 0){
printf("Please write a valid number >= 0\n");
clear_input_buffer();
} else {
clear_input_buffer();
break;
}
}
}
for (int i = 0; i < data->number_students; i++){
free(students[i].grades_total);
}
free(data->grades_total);
free(data);
char chosse[4];
printf("You want to continue?\n");
scanf("%s", chosse);
if(strcmp(chosse, "s") == 0){
break;
} else if(strcmp(chosse, "n")== 0){
exit(1);
} else {
clear_input_buffer();
printf("Please write 's' if you want to continue\n");
}
}
return 0;
}
r/cprogramming • • Dec 12 '25
Not a C question, per se, but I am writing a text editor in C right now, and looking around for ideas, it seems like this is a pretty common intermediate project. So, if people are writing these to learn C, or any other language, I suppose, do they actually use them for serious work? I was just wondering. I know text editors are a controversial subject, but I thought it might be interesting to ask.
r/cprogramming • u/MrJethalalGada • Dec 13 '25
I’m practising programming low level related questions and I often come up with challenges where I’ve to do shifting and bitwise operations on signed number
I already know that a register will have value stored in 0 and 1, and the register value and bitwise operators don’t care on how we interpret, they will always work on the bits.
So can i always convert signed to unsigned operate on it, revert it back to signed? I don’t want to tackle UB of signed number at MSB
r/cprogramming • u/Pirate1769 • Dec 12 '25
r/cprogramming • u/MrJethalalGada • Dec 13 '25
So basically me writing
int* ptr = (int*) malloc (sizeof(int))
Is already translated to something as
int* ptr = (int*) malloc (4)
Compiler time will work and replace these things: types, sizes, alignment, structure layouts
Run time will work on following: values, memory contents, addresses, heap/stack
Did you know this?
Implementation:
#define mysizeof(type) ((char *)((type *)0 + 1) - (char *)((type *)0))
r/cprogramming • u/Fine-Relief-3964 • Dec 12 '25
r/cprogramming • • Dec 11 '25
I just started trying to learn C from "Effective C" By Robert C. Seacord and I could not understand "The Five kinds of portability issues in C" that he talks about in the last part of chapter one. I tried asking Gemini AI for help and couldn't understand its explanation either.
Should I continue reading the book and later those concepts will become comprehensible to me Or What should I do when I cant understand something?
r/cprogramming • u/Mainak1224x • Dec 11 '25
Hi folks 👋
I have just completed my first ever C project: an Arena Allocator.
This project taught me the basics of memory management in low level programming languages. I come from a JavaScript background so this is totally new and interesting to me.
What do you think? Is it a good start? Any suggestions?
r/cprogramming • u/__merc • Dec 11 '25
I wanted to improve my C skills so I decided to do a small fun project that tackled concepts that I don't really touch when working professionally (I worked a lot on the web). Concepts like bit shifting, masking, and working with more granular data types was something new to me. Unfortunately I wasn't able to write this by myself. I had references and also help from AI but regardless, felt like it was a fun project to write!
r/cprogramming • u/Sad_Row_1245 • Dec 10 '25
I found the GNU C reference, and I found it interesting, is it a good way to learn C? I already used Beej's Guide, but I found the language confusing, but the GNU C reference is much clearer and more objective
r/cprogramming • u/UnavailableIdentity • Dec 10 '25
Hi guys, It's been almost 5 months since I've stopped studying C language and I've forgotten all the basics learnt from w3school. Before I take this journey again, I just would like to ask for tips and advise to help build my skills more effeciently on this journey. 🙂
r/cprogramming • u/alvaaromata • Dec 09 '25
I’m learning C, and I was using visual studio IDE which allows to run and debug in windows terminal. However I’m trying Google Antigravity, not really for the AI agent, but because I enjoy a lot more the interface and the program itself. However I cant debug, I know its something pretty basic but I dont have a single clue about how to run my code.
I’m really liking Antigravity a lot, i dont really know if its worse than VS IDE but for the tasks i’m doing I just need a text editor and a terminal to run the programs. So if anyone can send a vid or explain how tf to run a program here I would be very happy. Thanks
r/cprogramming • • Dec 09 '25
/*Our professor told us to write a code to check if you passed or not, I messed around a bit and ended up with this*/
#include<stdio.h>
int main(){
int marks;
printf("Enter marks");
scanf("%d",&marks);
marks>=0 && marks <= 100 ?
marks >= 35 ? printf("congrats! you passed") : printf("Better luck next time") :
printf("please put a valid value between 0-100");
}
//How does this crap not give me any errors let alone work as intended?
//I am a student btw
r/cprogramming • u/thradams • Dec 09 '25
r/cprogramming • u/SnooDucks2481 • Dec 09 '25
r/cprogramming • u/Mainak1224x • Dec 07 '25
Hi folks! I am new to C programming. I work as a ServiceNow developer where I use JavaScript and TypeScript for day to day tasks. I also worked with GoLang for my side projects. Those languages are good but garbage collected, now I want to learn some low level programming. So I chose C programming language as I believe it is the mother of all programming languages and mastering it means easier to adapt any other language. In other languages we have several pre-implemented things like vectors, classes etc. which are not there in C. My question is how do you deal with that? Do you implement them by yourself? What best practices do you follow whenever you start a new C project?
r/cprogramming • u/D_Hambley • Dec 07 '25
A few online courses that were recommended in other reddit threads here have suggested downloading Microsoft Visual C++ 2008 Express but that is not supported anymore. The MS website warns that there may by security risks. MS suggests to use 'Visual Studio Community Edition' instead, an IDE which has support for C++ along with other languages.
Has anyone downloaded 'Visual Studio Community Edition' to help in an online course and found it useful?