26
5
submitted 6 months ago by [email protected] to c/[email protected]
27
4
submitted 6 months ago by [email protected] to c/[email protected]
28
14
submitted 7 months ago by [email protected] to c/[email protected]

Hey, so I've been searching the web for a while for some popular applications that use C. All that I can find so far is, Git, Vim, Linux, and Unix. I also know the Windows kernel uses it a little bit, but that's it. Does anyone know some popular apps that use C? Doesn't have to be programming related, just an actual app that's written in C? Sorry if this question sounds redundant or anything.

29
12
submitted 8 months ago* (last edited 8 months ago) by [email protected] to c/[email protected]

I'm trying to create a dynamic array which can be modified using the functions Array_Push(array, val) & Array_Del(array, index). Now the current way I have this I need a variable to keep track of the size of it. My implementation of this concept is to store the data/size in a struct like so:
struct Array {
  void **data;
  int size;
}
However in order to read the actual array you have to type array.data[i] which I think is a little bit redundant. My solution to this was attempting to store the size of the array in a different index. I didn't want to store it inside [0] as that would create a lot of confusion, so I wanted to try storing it inside of [-1]. An obvious problem with this is that [-1] is outside the array. What I did instead was create an array via void **array = malloc(sizeof(void*) * 2) (the * 2 is so when you push with realloc() it doesn't free empty memory,) then setting the size via array[0] = (void *)0. After that I increment the pointer to it via array += 1. However when I try to free it free(array - 1), I end up freeing non malloc()ed. I think this is just an issue with my understanding of pointers, so I wanted to ask where my logic is going wrong, along with if anybody actually knows how to do what I'm trying to do (in the title).

30
14
submitted 8 months ago* (last edited 8 months ago) by [email protected] to c/[email protected]
31
3
submitted 9 months ago by [email protected] to c/[email protected]
32
6
submitted 10 months ago* (last edited 10 months ago) by [email protected] to c/[email protected]

Hi experienced people!

I am working on an interpreter of sorts. I would like its scripts to be invokable from the command line - so It would honor the "#!" as a first line, basically by making any line starting with a "#" a comment.

The issue is that I want to be able to read the source code more than once. The first pass will deduce the number of lines, the number of variables, the number of line labels, The beginning of the second pass will allocate arrays (malloc) to hold the program and its data, then re-read the source to store it internally and fill symbol tables and mark variables. once the source is read the 2nd time the program will begin to execute.

If an interpreted program is mentioned on the command line it would only get one pass at the source, right? That source would come in on standard input, and once read is no longer available.

Is there a way for my interpreter to get the file name instead of the body of the file?

While writing the question I came up with an idea, but I hope there is a better one. I could as a first pass store each line of the program in a known temporary file, then for the second pass I could read that file. I don't like this but if there is no better way...

33
5
submitted 10 months ago by [email protected] to c/[email protected]

I'll explain myself. I'm doing a small project using a few dependencies, so I have to add them when I call gcc. Everything else is really easy. I just have c files and header files. I find it really cumbersome to have to tell make what headers go with what c files when they have the same name. I don't see why we don't have a build system where you simply have to give a project folder with the name of source file with the main() function, give the name of the output executable, the external dependecies to be called with gcc, and that's it. Everything else can be automatically detected and linked apropriately, even with multiple folders inside the project folder. Does something like that exist that's simple to use, or is this doable in make?

34
1
MISRA C - Wikipedia (en.wikipedia.org)
submitted 10 months ago by [email protected] to c/[email protected]
35
11
submitted 10 months ago by [email protected] to c/[email protected]

Not new, but a rabbit hole nonetheless.

36
3
submitted 10 months ago by [email protected] to c/[email protected]

Hello,

I've installed the library called Raylib (https://www.raylib.com/). I followed the instructions to install the dynamic library. However, when compiling using gcc, including the raylib.h header dynamically in the source code produces an undefined reference error for every function I use from that library. That means that the library functions weren't attached to the executable at runtime, so gcc didn't find the right .so file.

To actually produce an executable, I have to use a shell script, that for some reason needs raylib's source code path. But that goes against the point of installing a dynamic library, since the latter is used so that source code can be compiled without library functions, instead putting them somewhere else to be shared with other executables.

Can someone explain to me how gcc finds the .so files and if anyone has used raylib dou you understand what the shell script does?

37
11
A Modern C Development Environment (interrupt.memfault.com)
submitted 11 months ago by [email protected] to c/[email protected]
38
5
submitted 11 months ago* (last edited 11 months ago) by [email protected] to c/[email protected]

Hi guys, I'm writing a program using the pbm "P1" format to create a random bitmap. The '1' char represents a black pixel and the '0' char represents a white pixel. All that has to be done to use this format is to basically "draw" the image in a file using this system. I just want to make a char matrix and randomly put black pixels until I hit a maximum percentage of black pixels which I don't want to exceed. Then I print that matrix to a file.

What I don't understand is that even if I decrease the value of PERCENTAGE, recompile, and execute the program, there is no noticeable difference, in fact I suspect it's the same image, although I can't be sure.

#include
#include

#define WIDTH 400 #define HEIGHT 250 #define TOTAL_PIXELS (WIDTH * HEIGHT) #define PERCENTAGE 0.01 #define BLACK_PIXEL '1' #define WHITE_PIXEL '0'

` int randomBrackets(int n){ return rand()/((RAND_MAX/n) + 1); }

int main(){
	
	char pbm_matrix[WIDTH][HEIGHT];
	for(int i = 0; i < HEIGHT; i++){
		for(int j = 0; j < WIDTH; j++){
			pbm_matrix[i][j] = WHITE_PIXEL;
		}
	}
	int total_black_pixels = 0;
	while((double)(total_black_pixels/TOTAL_PIXELS) < PERCENTAGE){
		int x = randomBrackets(WIDTH);
		int y = randomBrackets(HEIGHT);
		pbm_matrix[x][y] = BLACK_PIXEL;
		total_black_pixels++;
	}

	FILE* img_ref = fopen("bitmap1.pbm", "w");
	fprintf(img_ref, "P1 %d %d\n", WIDTH, HEIGHT); 
	for(int i = 0; i < HEIGHT; i++){
		for(int j = 0; j < WIDTH; j++){
			fputc(pbm_matrix[i][j], img_ref);
		}
		fputc('\n', img_ref);
	}
	fclose(img_ref);
	return 0;
}`

This to open the image file netpbm is needed, and maybe libnetpbm (I don't know, everything is preinstalled on Linux Mint). I'm using the exercise in Rouben Rostamian's book as reference.

EDIT: I'm sorry for the very poor formatting at the top of the code, I can't seem to get the macros to look good

39
12
submitted 11 months ago by [email protected] to c/[email protected]

I just finished the C Piscine at a 42 school, so I have gotten a good grasp of the basics of C (about 300 hours worth). The school won't start until October, and I'd like to practice my C skills in the meantime so I can be better prepared when it does start.

Any suggestions for curriculum / projects that don't start at the very beginning? I already have a good grasp of pointers, control structures, structs, string manipulation, etc.

40
4
submitted 1 year ago by [email protected] to c/[email protected]
41
9
submitted 1 year ago by [email protected] to c/[email protected]

Unit tests are meant to verify the functionality of isolated units of code. When dealing with code whose output depends on the system or system configuration, what are approaches to write effective unit tests? I feel this problem plagues lower level systems languages more so I am asking it here.

I solve this by writing "unit tests" that I then manually compare to the output of my terminal's utilities. It is the quickest way to verify units work as expected but it is obviously not automated.

Making a container or a VM to run integration tests seems like the next easiest way, not sure if there are other cost effective ways.

Scenario

Say I have a function called

get_ip_by_ifname(const char *if_name, struct in_addr *ipaddr)

Inputs:

  • string of interface name
  • pointer to variable where the returned IP address will be

Returns:

  • -1 if interface does not exist,
  • 0 if interface exists but has no IPv4 IP
  • 1+ if interface exists and has at least 1 ip addr (some interfaces have multiple addresses, only 1st is written to ipaddr buffer)
Test Cases and their dependencies
  1. Interface doesn't exist
    • easy to test, use uncommon interface name
  2. Interface exists has no ipv4 ip address
    • requires the underlying system to have a unique interface name which I need to hard code and compare to in my unit test
  3. interface exists, has 1 ipv4 ip address
    • requires underlying system to have the uniquely named interface with exactly 1 uniquely defined ip address. Both of which I need to hard code into my test
  4. interface exists, has 1+ ipv4 ip addresses
    • similar to item 3.

The way I might test something like this works is write a test that logs each case's output to the terminal than run ip -c a in another terminal and compare the info in the 2 outputs. I verify it works as expected manually with very minimal setup (just assigned multiple IP addresses to one of my interfaces).

I would like to test this in an automated fashion. Is there any way that wont be a time sink?

42
7
submitted 1 year ago by [email protected] to c/[email protected]
43
10
submitted 1 year ago by [email protected] to c/[email protected]

Saw this on reddit, good quality tutorial.

44
11
submitted 1 year ago by [email protected] to c/[email protected]
45
13
Favorite projects? (programming.dev)
submitted 1 year ago by [email protected] to c/[email protected]

Hello fellow developers! I’m new to this community and I am interested in learning more about you all, I have a simple question what is your favorite personal project that you have developed in the C programming language? It doesn’t have to be something mind blowing or extremely complex just something that you’ve enjoyed developing.

46
4
submitted 1 year ago by [email protected] to c/[email protected]

https://gitlab.com/waspentalive/labelbasic

Label Basic is to be a simple basic interpreter that uses labels instead of line numbers. I have a design document in the project at GitLab. I have some C coding experience but I am really weak in pointers. All of my experience is small C programs for my own curiosity, I have published very little, see the other items in my GitLab.

47
3
submitted 1 year ago by [email protected] to c/[email protected]

Hi all, I implemented yet another on-line cryptographic hash functions calculator. It is written in C, compiled into WebAssembly and is running inside your web browser (nothing is sent to the server). The calculator: https://marekknapek.github.io/hash, web page source code: https://github.com/MarekKnapek/MarekKnapek.github.io, C source code: https://github.com/MarekKnapek/mk_clib.

48
6
submitted 1 year ago by [email protected] to c/[email protected]
      ' char* strInput =(char*) malloc(sizeof(char));
        int ch;
        int letNum = 0;
        while((ch = getchar()) != EOF){
                letNum++;
                strInput = (char*)realloc(strInput,letNum*sizeof(char));
                *(strInput + letNum - 1) = ch;
        }
        printf("\n");
        printf("%s\n",strInput);
        free(strInput);`

This is the contents of main in a program I wrote that takes an undefined number of chars and prints the final string. I don't understand why but it only works if I press ctrl+D twice, and only once if I press enter before.

does anyone get what's going on? And how would you have written the program?

49
4
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]

I was looking over the first kata i did on codewars, and I thought it would be fun to try and solve it in C. The object was to return a string based on a boolean input. it took a lot of trial and error, googling, chat gippity, but I eventually got it to work. I am still focused on learning python, but I've had it in my mind that I should branch out once I've reached a competence plateau in python. I'm nowhere near that plateau yet, but this seemed simple enough to warrant the necessary investment in time to accomplish it.

// C:
#include <stdbool.h>
// FIRST EVER C PROGRAM
const char *bool_to_word (bool value){
// you can return a static/global string or a string literal
  if (value == 1){
  return "Yes";
    }
  else{
    return "No";
  }
}

I realize this is pretty trivial, but still, it's a milestone for me and I wanted to do my part to get the ball rolling on this community.

50
2
Greetings - (lemmy.one)
submitted 1 year ago by [email protected] to c/[email protected]

Hi, I am WasPentalive and I use C to create small programs to explore randomness and other interesting subjects. For example, I had heard that the chances of winning the CA lottery was the same as flipping a coin Heads or Tails 25 times in a row. I wrote a C program to flip coins until that streak was accomplished.

view more: ‹ prev next ›

C Programming Language

931 readers
12 users here now

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

© Dennis Ritchie

🌐 https://en.cppreference.com/w/c

founded 1 year ago
MODERATORS