/r/C_Programming
The subreddit for the C programming language
Click the following link to filter out the chosen topic
comp.lang.c
Frequently Asked Questions/r/C_Programming
How is Let us C book for C programming?
Open source at https://github.com/arttnba3/Nornir-Rootkit, which currently contains some mainstream and legacy LKM rootkit techniques, and I hope too add something more soon...
Hello, i'm currently on vacation from work and college, and i've decided to start learning C for fun. i'd like to know the best way to begin. i'm studying Information Systems in college, and i've worked as a web developer using JS and PHP. i've also completed some college projects in Python, working with APIs. What would be the best starting point? Is it a difficult language to learn? Thanks.
I want to try the sokol library with clion on linux. I have sokol and sokol-samples running but I dont know how to build my own files.
Should I clone sokol into every project or only into one global directory?
How can I compile sokol projects without fips (sokols buildsystem) easily?
Has anyone used sokol before and if so, how is your setup?
edit:
clion uses cmake so I would prefer to build with that
So i am learning command lines arguments and just came cross char *argv[]. What does this actually do, I understand that this makes every element in the array a pointer to char, but i can't get around as to how all of this is happening. How does it treat every other element as another string? How come because essentialy as of my understanding rn, a simple char would treat as a single contiguous block of memory, how come turning this pointer to another pointer of char point to individual elements of string?
Hi, C community! I started learning C few days ago, and finished a project for me.
I love C/C++ but I felt the lack of rich build / package system like Cargo or npm is quite frustrating for a beginner. I tried CMake, it's good, but still a bit difficult.
I wanted to automate repeated tasks, I learned about CMake kits and found a Neovim plugin that supports CMake kits. But somehow didn't work on my machine, so I thought I gotta make my own tool.
Playing bunch of strings and pointers was quite a thrill. I would appreciate it if you could review the code!
I'm really bad at English.
Hi everyone! I’m a biologist turned data engineer who self-learned everything related to data engineering, including Python and SQL. Now, I’m aiming to transition into a junior embedded developer role and want to learn C.
With my current background, what’s the best way to start learning C? Are there any approaches or resources that might suit me better, given my experience with Python and SQL? Or should I stick to the classical route of learning C from scratch?
I’d love to hear your advice on the best ways to get started 🙌🏽
Hello mates! I'm back after posting about my new project CJIT here, which some people appreciated. Since you shared some useful advice back then, I'm asking for more. :^)
I shared my short list in this issue all the stuff I plan to put as built-in extensions inside CJIT.
A few are already there: of course STB, Nuklear, termbox2 and miniaudio, also dmon.
This time I have a question that may have been asked already here recently, or elsewhere, so please also point me to more threads where people share some digging and ranking across the wealth of minimalistic libs and header-only C implementations around.
Cheers!
Hi,
The title kind of says it all - I want to get into ARM64 OS security research, specifically Darwin based OS' such as iOS, macOS or pretty much any operating system that runs on the XNU kernel.
Unfortunately I have learnt the hard way that I although I might know a thing or two about Darwin, I actually do not know anything about programming in C and have spent the past few months lying to myself about this and just relying on ChatGPT to do everything for me :/.
I have been learning some basics such as arrays, for loops, if statements, how to use malloc to an extent but what are some beginner projects I can work on based on things like memory management etc that will improve my foundation in C, allow me to gradually become a experienced programmer and eventually niche out into Darwin?
Thanks everyone for your help!
hi im new i would like to try to learn c++ so I can program myself and make my own dma program for rainbow six
Hi all! I'm building a game and I am curious how people build their projects in C. I am coming from a C++ background, but I am experimenting with just C as a personal preference. In C++, we usually use OOP techniques to build out the game. For C I am running into some road blocks where I am not sure if I am going to have problems down the road.
Let's say I have a Card struct, and want it to have different data depending on which type of Card it is. My first idea was to make some kind of void* variable that I could cast to different types of data. The next idea was to make all the different types of data as variables in the Card struct, and only assign the one that I want to use. This would waste space, but not that much as they would be pointers to the data, not the actual data structs.
The idea here is that I want a array of cards which have different data depending on the cards. Obviously in OOP, I could have a base class of Card with different types of cards inherited from it, and then cast between the types. I am looking for something similar I can do in the C environment.
Thanks in advance for any suggestions!
To learn more about c api design I’ve been looking through some c projects, currently looking through Sokol, and noticed that some structs have a void* user_data member.
What is this usually used for?
i want to represent arbitrary data of arbitrary types which all occupy the same size in memory. right now i typedef'd a void* to act like a block of raw memory which i can assign values i want to and retype it at will. but i want to know if i should move away from using a hacky sort of system like this into using a union. I want to make sure that the system is as fast as possible, so i have reservations about using a specialized structure that must be referenced.
or in other words:
struct or typecast?
Hi guys,
I was wondering how to begin to programming in C and do you have any links/youtubers/advices for a newbie pls 😄
Edit: Please don't downvote
We already know that C doesn't have a string datatype by default, and mostly people allocate it in char[]
or char*
. It also doesn't have standard libraries to work with dynamicly-sized strings, meaning that you have to handle that on your own.
However, I've already developed a library that enables support for dynamicly-sized strings.
So my criticism for C is: Why didn't developers of C add this library to the compiler itself by default (I don't mean specifically my implementation)? If I can do it, so could they.
(However, this doesn't change the fact that C is my favorite programming language)
Edit: Please don't downvote as I've got my answer: It's C, and in C, you write your own functions. It's not like Python that has a lot of in-built functions or anything.
A very much C++-like set of try-catch blocks in C, inspired from the post Exceptions in C with Longjmp and Setjmp by Francesco Nidito:
Try-Catch-In-C/main.c at master · htanwar922/Try-Catch-In-C
The implementation basically uses a simple tracking of exception contexts created using longjmp and setjmp. It's really interesting, do check it out.
typedef uint8_t bool;
#define true 1
#define false 0
#define not !
#define and &&
#define or ||
typedef struct exception
{
uint8_t type;
const char* message;
bool cleared;
jmp_buf ex_buf;
} exception;
enum exception_type
{
NoError = 0,
RuntimeError = 1,
LogicError = 2,
};
#define error_type(err) ( \
err == RuntimeError ? "RuntimeError" \
: err == LogicError ? "LogicError" \
: "UnknownError" \
)
#define MAX_EXCEPTIONS 0xff
struct exception * exception_context[MAX_EXCEPTIONS] = {NULL};
uint8_t exception_idx = MAX_EXCEPTIONS;
#define try \
{ \
exception_idx++; \
exception_context[exception_idx] = (struct exception*)malloc(sizeof(struct exception)); \
struct exception * pexcep = exception_context[exception_idx]; \
do { \
pexcep->type = NoError; \
pexcep->cleared = false; \
if( 0 == (pexcep->type = setjmp(pexcep->ex_buf) ) ) {
#define catch(err_type, e) \
} else if (err_type == pexcep->type) { \
pexcep->cleared = true; \
struct exception e = *pexcep;
#define catch_(e) \
} else { \
pexcep->cleared = true; \
struct exception e = *pexcep;
#define finalize \
} \
pexcep->cleared = true; \
} while(0); \
if (pexcep->type != NoError and not pexcep->cleared) { \
if (exception_idx == 0) { \
fprintf(stderr, "terminate called after throwing an instance of %s:\n" \
" what(): %s\n" \
, error_type(pexcep->type) \
, pexcep->message); \
exit(pexcep->type); \
} \
exception_context[exception_idx - 1]->type = pexcep->type; \
exception_context[exception_idx - 1]->message = pexcep->message; \
exception_context[exception_idx - 1]->cleared = pexcep->cleared; \
free(pexcep); \
exception_context[exception_idx] = NULL; \
exception_idx--; \
pexcep = exception_context[exception_idx]; \
longjmp(pexcep->ex_buf, pexcep->type); \
} \
else { \
free(pexcep); \
exception_context[exception_idx] = NULL; \
exception_idx--; \
} \
}
#define throw(err_type, msg) \
{ \
struct exception * pexcep = exception_context[exception_idx]; \
if (pexcep->type != NoError and pexcep->cleared) { \
if (exception_idx == 0) { \
fprintf(stderr, "terminate called after throwing an instance of %s:\n" \
" what(): %s\n" \
, error_type(err_type) \
, msg); \
exit(pexcep->type); \
} \
free(pexcep); \
exception_context[exception_idx] = NULL; \
exception_idx--; \
} \
pexcep = exception_context[exception_idx]; \
pexcep->message = msg; \
pexcep->type = err_type; \
longjmp(pexcep->ex_buf, pexcep->type); \
}
#define throw_ \
{ \
if (exception_idx >= MAX_EXCEPTIONS) { \
perror("terminate called without an active exception"); \
abort(); \
} \
struct exception * pexcep = exception_context[exception_idx]; \
if (pexcep->type == NoError) { \
perror("terminate called without an active exception"); \
abort(); \
} \
if (pexcep->cleared) { \
if (exception_idx == 0) { \
fprintf(stderr, "terminate called after throwing an instance of %s:\n" \
" what(): %s\n" \
, error_type(pexcep->type) \
, pexcep->message); \
exit(pexcep->type); \
} \
exception_context[exception_idx - 1]->type = pexcep->type; \
exception_context[exception_idx - 1]->message = pexcep->message; \
exception_context[exception_idx - 1]->cleared = false; \
free(pexcep); \
exception_context[exception_idx] = NULL; \
exception_idx--; \
} \
pexcep = exception_context[exception_idx]; \
longjmp(pexcep->ex_buf, pexcep->type); \
}
So I'm practicing linked lists, and I wrote a function to reverse a linked list, it receives a list with a dummy head, makes a new one (empty list with dummy head) and keeps adding the elements from the first list behind the dummy head in order resulting in a reversed list.
The end result looks correct, it can normally be printed out inside the reverse function. According to the debugger the return value of the function is also normal, but for some reason it doesn't actually assign the return value to the variable, instead the variable is just NULL.
I went to my print function, made a local variable tmp and assigned list->next to it, which resulted in a read access violation However, upon checking tmp in the locals tab, it seemed to have the entire reversed list in it (minus the dummy head which I skipped), while the original list just showed NULL with nothing else in it.
Does anyone have an idea what might be causing this? I can provide the code if necessary.
Edit: Issue solved, I forgot to declare the function in the header file.
Without cheating! You are only allowed to check the manual for reference.
like the pow function but it works slower and only works for integers and sometimes returns the minimum integer limit for some reason
int worsePow(int base, int power)
{
if(power==0) return 1;
int result = base;
int exponent = 1;
while(true)
{
if(exponent == power) return result;
if(exponent*2 > power)
{
return result * worsePow(base, power - exponent);
}
result *= result;
exponent *= 2; }
}
Hi, me again, i'm still in reading the C programming language second version, and rereading this part bother me
/*strcpy:copyttos;pointerversion2*/
voidstrcpy(char*s,char*t)
{
while((*s++=*t++)!='\0')
;
}
I understand that *s and *t are evaluated first, (values that t and s points to).
But after that, why s and t are increment and not there value? *s and *t ?
I just want to understand, even if some people are stating that this syntax is odd, or too complicated/cryptic.
void viewDoctor() { FILE *fp; int ID;
printf("DOCTOR ID: ");
scanf("%d",&ID);
struct Doctor doc;
fp = fopen(filename, "r");
if (fp != NULL) {
int found = 0;
while (fscanf(fp, "%d\n%99[^\n]\n%d\n%99[^\n]\n%70[^\n]\n%14[^\n]\n",
&doc.doctor_ID, doc.name,&doc.year_of_experience,
doc.specialization,doc.available_time,doc.emergency_no) != 6 ) {
if (doc.doctor_ID == ID) {
found = 1;
printf("\nDoctor ID: %d", doc.doctor_ID);
printf("\nName: %s", doc.name);
printf("\nExperience: %d years", doc.year_of_experience);
printf("\nSpecialization: %s", doc.specialization);
printf("\nAvailable Time: %s", doc.available_time);
printf("\nEmergency No: %s\n", doc.emergency_no);
break;
}
}
fclose(fp);
if (!found) {
printf("\nDoctor with ID %d not found.\n", ID);
}
} else {
printf("Error opening file!\n");
}
} I just want to compare Doctor ID to print further details but it’s not working
i've been learning c recently, and i've learned about pointers and how they work, and i can't fully understand why a certain piece of code i've written works. from my understanding, an array of pointers has to have its memory allocated before values can be stored in it (like a char *ptr pointer). so i'm a bit confused as to why the following code works and stores the values assigned:
#include <stdio.h>
#include <stdlib.h>
// function declaration
int string_add();
// main function
int main(void) {
// defining strings
char **strings; // initialize strings
*strings = "This is the first string";
*(strings+1) = "This is the second string";
*(strings+2) = "This is the third string";
*(strings+3) = "This is the fourth string";
*(strings+4) = "This is the fifth string";
*(strings+5) = "This is the sixth string";
*(strings+6) = "This is the seventh string";
*(strings+7) = "This is the eigth string";
*(strings+8) = "This is the ninth string";
*(strings+9) = "This is the tenth string";
*(strings+10) = "This is the eleventh string";
int n = 10;
*(strings+11) = "This is the twelvth string";
for (int i=0; i<=11; i++) {
printf("%d\n%s | %x\n", i, *(strings+i), &(*(strings+i)));
}
return 0;
}
Hello! I wanted to make a continuiation of my last post to show my code and ask your opinion on how good it is, by the way, i'm a beginner in c programming and this program was a project at my university, here's the code :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
int N=100,T[N],B[N],O[N],E[N],A[N],D[N],i,X,min,max,S,o,c,r,t;
bool exist;
printf("Enter the size of the array : ");
scanf("%d",&N);
printf("Enter %d elements of the array :\n",N);
for(i=0;i<N;i++) {
scanf("%d",&T[i]);}
while(true){
printf("\n\n"
"**************************************MENU**************************************\n"
"* 1. Find min and max of the array *\n"
"* 2. Find position of a value in the array *\n"
"* 3. Reverse the array *\n"
"* 4. Split array into even and odd arrays *\n"
"* 5. Sort the array *\n"
"* 6. Exit *\n"
"********************************************************************************\n"
"\nEnter your choice : ");
scanf("%d",&X);
switch(X)
{
case 1:
min=0;
max=0;
for(i=1;i<N;i++){
if(T[i]>T[max]) max=i;
else if(T[i]<T[min]) min=i;
}
printf("The maximum of this array is %d\n",T[max]);
printf("The minimum of this array is %d\n",T[min]);
break;
case 2:
printf("Enter the value for the number you want to find : ");
scanf("%d",&S);
i=0; exist=false;
while(i<N && !exist){
if (T[i]==S) exist=true;
i++;
}
if(exist) printf("This value exists in the position %d in this array",i);
else printf("This value does not exist in the array");
break;
case 3:
o=0;
for(i=N-1;i>=0;i--) {
B[o]=T[i];
o++; }
printf("The reverse of this array is : ");
for(o=0;o<N;o++) {
printf("%d ",B[o]);}
break;
case 4:
for(i=0;i<N;i++) {
E[i]=T[i];
O[i]=T[i];}
printf("The odd array consists of : ");
for(i=0;i<N;i++) {
if(O[i] % 2 == 0) O[i]=0;
else printf("%d ",O[i]);}
printf("\nWhile the even array consists of : ");
for(i=0;i<N;i++) {
if(E[i]!=O[i]) printf("%d ",E[i]);}
break;
case 5:
printf("Do you want to sort the array :\n 1-Ascending\n 2-Descending\n " "Enter a choice : ");
scanf("%d",&c);
if(c==1){
for(i=0;i<N;i++) A[i]=T[i];
for(r=0;r<N;r++){
for(i=0;i<N;i++) {
if(A[i]>A[i+1]){
t=A[i];
A[i]=A[i+1];
A[i+1]=t;
}
}
}
printf("The array sorted in ascending order is :");
for(i=0;i<N;i++) printf("%d ",A[i]);
}
else if(c==2){
for(i=0;i<N;i++) D[i]=T[i];
for(r=0;r<N;r++){
for(i=0;i<N;i++) {
if(D[i]<D[i+1]){
t=D[i];
D[i]=D[i+1];
D[i+1]=t;
}
}
}
printf("The array sorted in descending order is :");
for(i=0;i<N;i++) printf("%d ",D[i]);
}
else {printf("ERROR");
break;}
break;
case 6:
exit(0);
default:
printf("ERROR");
break;
}
}
}