/r/cprogramming
A Subreddit dedicated to the C programming language. Everything related to C is welcome here.
C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions. It has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computer architectures that range from supercomputers to PLCs and embedded systems. Wikipedia
Imperative (procedural), structured
Dennis Ritchie
Dennis Ritchie & Bell Labs (creators);
ANSI X3J11 (ANSI C);
ISO/IEC JTC1/SC22/WG14 (ISO C)
1972 (48 years ago)
C18 / June 2018 (2 years ago)
Static, weak, manifest, nominal
Cross-platform
.c for sources
.h for headers
C++ is not C (but C can be C++)
For C++ go to :
Other Resources
/r/cprogramming
I have an embedded systems related interview coming up, these topics were mentioned on glassdoor - would appreciate any question reccomendations you guys have, I want to practice as much as I can
when I run this program i always get a warning in strtol.
#include <string.h>
#define buffer 50
#define limit_size (1024 * 1024 * 10) / sizeof(int);
#define input_size 20
void clear() {
int c;
while((c = getchar() != '\n') && (c != EOF));
}
int main(){
long long int height, weight; //truyen tham so neu char sang int
char num_height[input_size], num_weight[input_size]; //buoc dau bao mat
char printf_buffer[buffer]; //buffer de in
//input
fgets(num_height, sizeof(num_height), stdin);
fgets(num_weight, sizeof(num_weight), stdin);
//neu user co nhap \n
if((num_height[0] == '\n') || (num_weight[0] == '\n')){
snprintf(printf_buffer, sizeof(printf_buffer), "User pressed Enter, that not allowed!\n");
fprintf(stderr, "%s", printf_buffer);
clear(); //xoa bo dem
return 1;
}
//xoa bot ky tu \n neu co
num_height[strcspn(num_height, "\n")] = '\0';
num_weight[strcspn(num_weight, "\n")] = '\0';
//ham de chuyen char sang int
char *end;
height = strtol(num_height, &end, 10);
if(*end != '\n' && *end != '\0'){
snprintf(printf_buffer, sizeof(printf_buffer), "Height cant converte char to int!\n");
fprintf(stderr, "%s", printf_buffer);
clear();
return 1;
}
*end = '\0'; //lam rong
weight= strtol(num_weight, &end, 10);
if((*end != '\n' && *end != '\0')){
snprintf(printf_buffer, sizeof(printf_buffer), "weight cant converte char to int!\n");
fprintf(stderr, "%s", printf_buffer);
clear();
return 1;
}
return 0;
}```
I understand there are many libraries that can help me. but just for a moment lets say I wanted to create a basic GUI library for myself. How would I go to add GPU support for maybe games? and if I want to just create an app with some button. yada yada yada
I want to understand how libraries like SDL, opengl render windows, and then render the graphics that I make with them? Do they use x11 on linux(where it is available) or win32 lib on Windows?
Would be nice if someone can recommend me some books or some thing to read so that I can understand.
Thank you!
Hi all,
I'm currently learning C as my first programming language and feel like I have a good grasp of the fundamentals, including memory allocation and pointers. However, I'm not sure what to focus on next.
I've tried some easy LeetCode problems, and have been able to do a couple, but I've been advised to hold off on LeetCode for now until I develop skills more or just not to do it all together?
This is my first language so i am trying to learn anything and everything in order to get a head-start on my C programming semester next year.
Does anyone have suggestions for resources, courses, projects, or exercises that would be good for building my skills at this stage? Any advice is appreciated!
I wrote a CLI memory unit convertor for educational purposes in just 52 lines of code!
I know that errors aren't very verbose, I basically return help info everytime validation fails. The program's pretty straightforward, I think.
Critics are welcome!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define HELP \
"memconvert - convert any memory units\n" \
"\n" \
"Usage:\n" \
" # Convert 1024 megabytes to gigabytes\n" \
" $ memconvert 1024 MB GB\n" \
typedef struct {
char *name;
double value;
} memunit;
void memconvert(int quantity, memunit *origin, memunit *convert);
void die(void);
int main(int argc, char **argv)
{
if (argc < 4) die();
int quantity = strtol(argv[1], NULL, 10);
if (quantity < 0) die();
memunit unit_table[] = {
{"B", 1},
{"KB", 1024},
{"MB", pow(1024, 2)},
{"GB", pow(1024, 3)},
{"TB", pow(1024, 4)},
NULL
};
memunit *origin = unit_table;
memunit *convert = unit_table;
for (;origin->name; origin++)
if (!strcmp(origin->name, argv[2]))
for (;convert->name; convert++)
if (!strcmp(convert->name, argv[3]))
memconvert(quantity, origin, convert);
die();
}
void die(void)
{
puts(HELP);
exit(1);
}
void memconvert(int quantity, memunit *origin, memunit *convert)
{
printf("%d %s is %.0f %s\n", quantity, origin->name, (origin->value * quantity) / convert->value, convert->name);
exit(0);
}
This is the rough code I've made so far (exluding the platform wrappers):
size_t idm_getpagesize( void )
{
size_t pagesize = _idm_getpagesize();
#ifdef PAGE_SIZE
return pagesize ? pagesize : PAGE_SIZE;
#else
return pagesize ? pagesize : (1 << 4);
#endif
}
size_t idm_sizeof_heap_allocation_prefix( void ) { return sizeof(void*); }
size_t idm_round_to_alignment_boundary( size_t min )
{
size_t mask = ~(SIZE_MAX << __builtin_ctzll( (idmllu)sizeof(void*) ));
return (min & mask) ? (min | mask) + 1 : min;
}
void* idm_create_heap( size_t minsize, size_t max_allocations )
{
if ( !minsize )
{
errno = EINVAL;
return NULL;
}
if ( !max_allocations )
max_allocations = UCHAR_MAX;
unsigned long long pagesize = idm_getpagesize();
size_t hsize = sizeof(IDM_HEAP) + minsize + sizeof(void*) * max_allocations;
size_t lsize = sizeof(IDM_ADDR) * (max_allocations+2), gave = 0;
if ( minsize )
return NULL;
IDM_HEAP *heap = _idm_viewmap
( NULL, hsize + lsize, &gave, IDM_O_PROT_DUPLEX, _INVALID_IDMSYS_MAP, 0 );
if ( !heap )
{
errno = ENOMEM;
return NULL;
}
IDM_ADDR *list = (void*)(heap + 1);
uchar *data = ((uchar*)(heap + 1)) + lsize;
/* Where each allocation is noted */
heap->listvm.addr = list;
heap->listvm.edge = data;
heap->list_unused = list;
heap->list_active = ((IDM_ADDR*)data) - 1;
/* Where each allocation lives */
heap->datavm.addr = data;
heap->datavm.edge = ((uchar*)heap) + hgave;
heap->data_edge = heap->datavm.edge;
return heap;
}
What tips do peops have to give about implementing heaps in general? Keep in mind the above is a work in progress, not ready for testing yet. The API is not set in stone yet so I can still make changes if I need to.
I created a small tool in C that reads ID3v2 track number and prepends it to the name of the file. I actually use this tool because my car stereo sorts the music alphabetically and I would rather listen to it in the album tracks order. Anyway, I decided to add it to a public github repo and would like your opinions on the actual coding methods I used and how to improve it. Also I will be showing this repo to future employers in job interviews so any advice on the README file will be appreciated as well.
The repo link: https://github.com/Adam12a12/id3tag
Hello, I'm hoping you guys can help me I've been trying to write a code in C where when a sensor is high the pic sends out a high and a pwm. I'm trying to achieve 1.5v in pwm but no matter what.
*/ // PIC16F818 Configuration Bit Settings
// 'C' source line config statements
// CONFIG #pragma config FOSC = INTOSCIO // Internal oscillator, I/O function on RA6 and RA7
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin function (MCLR disabled, RA5 is digital I/O)
#pragma config BOREN = OFF // Brown-out Reset Disable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage Programming Disable (Low-voltage programming disabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB2 // CCP1 Pin Selection bit (CCP1 function on RB2)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h> #include <stdbool.h>
#define _XTAL_FREQ 2000000 // Define the oscillator frequency (2 MHz in this case)
bool RB0LastState = false;
void main(void) { // Configure the oscillator to 2 MHz OSCCON = 0b01100000;
// Pin configurations TRISBbits.TRISB0 = 1; // Set RB0 as input (Motion sensor) TRISAbits.TRISA1 = 0; // Set RA1 as output (LED or motor driver) TRISBbits.TRISB2 = 0; // Set RB2 (CCP1) as output for PWM signal
// Initialize outputs PORTA = 0x00; PORTB = 0x00;
// Configure PWM PR2 = 0b11111001; // Set PWM period (255 for 8-bit resolution) CCP1CON = 0b00111100; // Configure CCP1 for PWM mode T2CON = 0b00000101; // Set TMR2 prescaler to 4:1 CCPR1L = 0b01111100; // Set duty cycle to 50% (example)
// Main loop while (1) { if (PORTBbits.RB0 == 1 && RB0LastState == false) { PORTAbits.RA1 = 1; // Turn on LED/motor driver CCP1CON = 0b00001100; // Enable PWM output RB0LastState = true; }
if (PORTBbits.RB0 == 0 && RB0LastState == true) { PORTAbits.RA1 = 0; // Turn off LED/motor driver CCP1CON = 0x00; // Disable PWM output RB0LastState = false; }
__delay_ms(100); // Debounce delay } }
i want to learn C programming for free but i cant find any good videos so are their any live free sessions which teach you C as a complete noob?
#include <stdlib.h> #include <time.h> #include "system.h"
int generatePlanets(int nbrStars);
int generatePlanets(int nbrStars) { int n = 0; struct system *Sp = malloc(n * sizeof(system)); if (Sp == NULL) { printf("Memory allocation failed \n "); return 1; }
for (int i = 0 ; i < nbrStars ; i++) { int planetsRan = rand() % 10; Sp[i].nbrPlanets = planetsRan; printf("They are %d planets in the system %d \n \n" , Sp[i].nb> }
return 0; }
int main() { srand(time (NULL)); int val = rand() % 100; printf("====Stardream 0.1 test==== \n \n"); printf("Generating number of stars in the galaxy : %d \n", val); generatePlanets(val); return 0; }
Seems to work... Did i create multiples instances of Sp ?
Hi i am a beginner in c, i am CS undergrad, I have some experience on programming. I want to understand more on c or how things work but when I google how c works just compiling , assembling and linking related beginner stuffs pop out but I want to understand more. Can you share your links, can you give advices
Thanks to the Mods for letting me post here - greatly appreciated.
As per the title, I have a C Developer job opening on my desk. Here's a summary:
* 100% Remote.
* Ideally the Developer will be based in Canada and on the Mountain Time Zone. That's the perfect situation. However, anywhere in the US or Canada will be fine so long as, a) you can work on a Mountain Time schedule; and, b) if in the US, we can make the numbers work due to USD>>>CDN exchange rates.
* This is an hourly-paid, 40 hours per week, consulting role
* The company is a small Canadian shop comprising ex-IBM'ers who broke away several years ago and formed their own company. They have built a strictly C-based product focused on cybersecurity and which is targeted towards mainframe, z/OS environments.
* Due to their product's success in the market they have more work they can handle and need an extra pair of experienced "C Hands" to come aboard.
* Hourly rate? The reality is that I have no idea what the market bears for C Developers [20 years ago I did]. I can tell you to the penny what a Java SWE in NYC costs. Or, a cloud native AWS/NodeJS SWE in Austin, TX.
A C Developer, however? I just don't know :-( I suspect this is one where the market will dictate.
* Use Reddit's Chat feature to get in touch. Providing my work email address and company info. here would doxx me [I'm active in a couple of subs] but rest assured I'm not a scammer/spammer or one who "does the needful" if you get my drift...
Thanks in advance.
Tell me some suggestions to change the program simple.
void func(int *a, int *b, char *s) {. ..} is this valid?
I was goofing around when I wondered what would happen if I tried to run a Python program compiled through pyinstaller with valgrind. I tried two simple console programs and one with GUI that used pygame. The first console program had the following code:
def xor(\*args):
return sum(args)%2
while True:
st = input()
for a in (0,1):
for b in (0,1):
for c in (0,1):
for d in (0,1):
print(a,b,c,d, " ", int(eval(st.lower())))`
which prints a truth value of an expression in terms of variables a,b,c,d relying on python's evaluation of the expression. The second program only contained "print(input())". The valgrind evaluation was identical for the two, so I'll give the one for the second program:
==8050== Memcheck, a memory error detector
==8050== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8050== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==8050== Command: ./p
==8050==
==8050== Warning: ignored attempt to set SIGKILL handler in sigaction();
==8050== the SIGKILL signal is uncatchable
==8050== Warning: ignored attempt to set SIGSTOP handler in sigaction();
==8050== the SIGSTOP signal is uncatchable
==8050== Warning: ignored attempt to set SIGRT32 handler in sigaction();
==8050== the SIGRT32 signal is used internally by Valgrind
hello
hello
==8050==
==8050== HEAP SUMMARY:
==8050== in use at exit: 0 bytes in 0 blocks
==8050== total heap usage: 202 allocs, 202 frees, 1,527,219 bytes allocated
==8050==
==8050== All heap blocks were freed -- no leaks are possible
==8050==
==8050== For lists of detected and suppressed errors, rerun with: -s
==8050== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
I tried running it with differently sized inputs (up to around 2000 characters) but it always reported 202 allocs, 202 frees. Does anyone know why the number never changes, and what those allocs could be doing in the first place? Also, what's with the sigaction warnings from valgrind (I've no idea what sigaction is)?
Edit: formatting
Hey guys, I need to program an AI for nine mans morris for a university project, so I‘m not allowed to use any external libraries etc. I‘m not asking for code, just some inspiration and ideas, or possible approaches. I don’t think machine learning is really on the table here, because of limited time and the above mentioned restrictions, so a algorithmic approach seems more appropriate (but I am happy for any reasonable suggestions).
Redditors, do your magic.
NOTE: THIS IS SIMPLY FOR IMPROVEMENTS This is not relevant for me to pass my class, but there is a contest for the best AI, so this should be within the rules I hope.
I have been researching about Rust and it just made me curious, Rust has:
And yet, Rust has:
Now if I'm not wrong, C has almost the same level of difficulty, but is faster and yet I don't see a large community of frameworks for web dev, app dev, game dev, blockchain etc.
Why is that? And before any Rustaceans, roast me, I'm new and just trying to reason guys.
To me it just seems, that any capabilities that Rust has as a programming language, C has them and the missing part is community.
Also, C++ has more support then C does, what is this? (And before anyone says anything, yes I'll post this question on subreddit for Rust as well, don't worry, just taking opinions from everywhere)
Lastly, do you think if C gets some cool frameworks it may fly high?
Hello, I am currently creating a Minecraft clone in C and OpenGL. I have implemented a chunk loading system however this would result in freezing as new chunks are loaded as the main thread is having to wait for the chunks terrain to be generated, therefore I am now trying to implement a second thread that will wait until a chunk is ready to be generated and generate its terrain.
My current idea is that the world stores a linked list for the chunks and a queue to store the chunks that need to be generated (each would store a pointer to the chunks memory that was allocated). Then when the load chunk function was ran it creates a new chunk that isn't visible so that you cant see the terrain before it's generated, it also adds this to the chunks to be generated queue.
Then the generator thread would wait until the chunks to generate queue has a chunk in, get that chunk (and remove from queue) and generate and update it along with making it visible.
I have tried implementing this my self with a separate thread however I'm not sure if the while (true) is really the best idea. I've also tried to use mutexes but am struggling to understand when to lock and unlock, if order matters and also in the generator thread I think it may be locking it forever but I'm not sure. I also tried to use a thread condition but still no luck.
If anyone could guide me on the best approach for my idea it would be very helpful. Thanks.
The GitHub repository if needed: https://github.com/Brumus14/minecraft-clone/tree/5d6614e62d80d54b2c535261b25c29c131590cec
#include "world.h"
#include <string.h>
#include <pthread.h>
#include "noise1234.h"
#include "../math/math_util.h"
pthread_mutex_t chunks_to_generate_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t chunks_to_generate_count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t chunks_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t chunks_count_mutex = PTHREAD_MUTEX_INITIALIZER;
int get_chunk_index(world *world, vector3i position) {
for (int i = 0; i < world->chunks_count; i++) {
chunk *chunk = linked_list_get(&world->chunks, i);
if (vector3i_equal(chunk->position, position)) {
return i;
}
}
return -1;
}
void generate_chunk_terrain(world *world, chunk *chunk, int stage) {
if (stage == 0) {
for (int z = 0; z < CHUNK_SIZE_Z; z++) {
for (int y = 0; y < CHUNK_SIZE_Y; y++) {
for (int x = 0; x < CHUNK_SIZE_X; x++) {
vector3i position;
vector3i_init(&position,
chunk->position.x * CHUNK_SIZE_X + x,
chunk->position.y * CHUNK_SIZE_Y + y,
chunk->position.z * CHUNK_SIZE_Z + z);
block_type type = BLOCK_TYPE_EMPTY;
if (position.y < (noise3(position.x * 0.03,
position.z * 0.03, world->seed)) *
16 -
8) {
type = BLOCK_TYPE_STONE;
} else if (position.y <
(noise3(position.x * 0.03, position.z * 0.03,
world->seed)) *
16) {
type = BLOCK_TYPE_DIRT;
}
chunk->blocks[z][y][x] = type;
}
}
}
}
// better ore generation
// use octaves and make noise generator abstraction
if (stage == 1) {
for (int z = 0; z < CHUNK_SIZE_Z; z++) {
for (int y = 0; y < CHUNK_SIZE_Y; y++) {
for (int x = 0; x < CHUNK_SIZE_X; x++) {
vector3i position = {chunk->position.x * CHUNK_SIZE_X + x,
chunk->position.y * CHUNK_SIZE_Y + y,
chunk->position.z * CHUNK_SIZE_Z + z};
if (chunk->blocks[z][y][x] == BLOCK_TYPE_DIRT &&
world_get_block(world,
(vector3d){position.x, position.y + 1,
position.z}) ==
BLOCK_TYPE_EMPTY) {
chunk->blocks[z][y][x] = BLOCK_TYPE_GRASS;
} else if (chunk->blocks[z][y][x] == BLOCK_TYPE_STONE) {
if (noise4(position.x * 0.2, position.y * 0.2,
position.z * 0.2, world->seed) > 0.6) {
chunk->blocks[z][y][x] = BLOCK_TYPE_COAL;
} else if (noise4(position.x * 0.2, position.y * 0.2,
position.z * 0.2,
world->seed * 1.419198) > 0.6) {
chunk->blocks[z][y][x] = BLOCK_TYPE_DIAMOND;
}
}
}
}
}
}
}
void *generation_thread_main(void *world_arg) {
world *world_pointer = (world *)world_arg;
// copy the data so dont have to keep chunks to generate locked for main
// thread
while (true) {
pthread_mutex_lock(&chunks_mutex);
pthread_mutex_lock(&chunks_count_mutex);
pthread_mutex_lock(&chunks_to_generate_mutex);
pthread_mutex_lock(&chunks_to_generate_count_mutex);
if (world_pointer->chunks_to_generate_count > 0) {
chunk *chunk = queue_dequeue(&world_pointer->chunks_to_generate);
world_pointer->chunks_to_generate_count--;
generate_chunk_terrain(world_pointer, chunk, 0);
generate_chunk_terrain(world_pointer, chunk, 1);
chunk_update(chunk);
chunk->visible = true;
printf("Chunk Generated - %d, %d, %d\n", chunk->position.x,
chunk->position.y, chunk->position.z);
}
pthread_mutex_unlock(&chunks_to_generate_count_mutex);
pthread_mutex_unlock(&chunks_to_generate_mutex);
pthread_mutex_unlock(&chunks_count_mutex);
pthread_mutex_unlock(&chunks_mutex);
}
return NULL;
}
void world_init(world *world) {
tilemap_init(&world->tilemap, "res/textures/atlas.png",
TEXTURE_FILTER_NEAREST, 16, 16, 1, 2);
world->chunks_count = 0;
linked_list_init(&world->chunks);
world->chunks_to_generate_count = 0;
queue_init(&world->chunks_to_generate);
world->seed = random_range(0, 100);
pthread_create(&world->generation_thread, NULL, generation_thread_main,
world);
pthread_detach(world->generation_thread);
}
void world_load_chunk(world *world, vector3i position) {
pthread_mutex_lock(&chunks_mutex);
pthread_mutex_lock(&chunks_count_mutex);
pthread_mutex_lock(&chunks_to_generate_mutex);
pthread_mutex_lock(&chunks_to_generate_count_mutex);
if (get_chunk_index(world, position) != -1) {
pthread_mutex_unlock(&chunks_to_generate_count_mutex);
pthread_mutex_unlock(&chunks_to_generate_mutex);
pthread_mutex_unlock(&chunks_count_mutex);
pthread_mutex_unlock(&chunks_mutex);
return;
}
chunk *new_chunk = malloc(sizeof(chunk));
chunk_init(new_chunk, position, &world->tilemap);
new_chunk->visible = false;
linked_list_insert_end(&world->chunks, new_chunk);
world->chunks_count++;
queue_enqueue(&world->chunks_to_generate, new_chunk);
world->chunks_to_generate_count++;
pthread_mutex_unlock(&chunks_to_generate_count_mutex);
pthread_mutex_unlock(&chunks_to_generate_mutex);
pthread_mutex_unlock(&chunks_count_mutex);
pthread_mutex_unlock(&chunks_mutex);
}
// remove chunks from chunks to generate if have moved out of render distance
// before generated
void world_unload_chunk(world *world, vector3i position) {
pthread_mutex_lock(&chunks_mutex);
pthread_mutex_lock(&chunks_count_mutex);
pthread_mutex_lock(&chunks_to_generate_mutex);
pthread_mutex_lock(&chunks_to_generate_count_mutex);
int chunk_index = get_chunk_index(world, position);
if (chunk_index == -1) {
pthread_mutex_unlock(&chunks_to_generate_count_mutex);
pthread_mutex_unlock(&chunks_to_generate_mutex);
pthread_mutex_unlock(&chunks_count_mutex);
pthread_mutex_unlock(&chunks_mutex);
return;
}
linked_list_remove(&world->chunks, chunk_index);
world->chunks_count--;
pthread_mutex_unlock(&chunks_to_generate_count_mutex);
pthread_mutex_unlock(&chunks_to_generate_mutex);
pthread_mutex_unlock(&chunks_count_mutex);
pthread_mutex_unlock(&chunks_mutex);
}
void world_draw(world *world) {
texture_bind(&world->tilemap.texture);
pthread_mutex_lock(&chunks_mutex);
pthread_mutex_lock(&chunks_count_mutex);
pthread_mutex_lock(&chunks_to_generate_mutex);
pthread_mutex_lock(&chunks_to_generate_count_mutex);
for (int i = 0; i < world->chunks_count; i++) {
chunk_draw(linked_list_get(&world->chunks, i));
}
pthread_mutex_unlock(&chunks_to_generate_count_mutex);
pthread_mutex_unlock(&chunks_to_generate_mutex);
pthread_mutex_unlock(&chunks_count_mutex);
pthread_mutex_unlock(&chunks_mutex);
}
// use mipmapping
block_type world_get_block(world *world, vector3d position) {
// rename to chunks loaded
vector3i chunk_position = {floor(position.x / CHUNK_SIZE_X),
floor(position.y / CHUNK_SIZE_Y),
floor(position.z / CHUNK_SIZE_Z)};
int chunk_index = get_chunk_index(world, chunk_position);
if (chunk_index == -1) {
return BLOCK_TYPE_EMPTY;
}
chunk *chunk = linked_list_get(&world->chunks, chunk_index);
vector3i block_chunk_position = {mod(floor(position.x), CHUNK_SIZE_X),
mod(floor(position.y), CHUNK_SIZE_Y),
mod(floor(position.z), CHUNK_SIZE_Z)};
return chunk->blocks[block_chunk_position.z][block_chunk_position.y]
[block_chunk_position.x];
}
void world_set_block(world *world, block_type type, vector3d position) {
vector3i chunk_position = {floor(position.x / CHUNK_SIZE_X),
floor(position.y / CHUNK_SIZE_Y),
floor(position.z / CHUNK_SIZE_Z)};
int chunk_index = get_chunk_index(world, chunk_position);
if (chunk_index == -1) {
return;
}
chunk *chunk = linked_list_get(&world->chunks, chunk_index);
vector3i block_chunk_position = {mod(floor(position.x), CHUNK_SIZE_X),
mod(floor(position.y), CHUNK_SIZE_Y),
mod(floor(position.z), CHUNK_SIZE_Z)};
chunk->blocks[block_chunk_position.z][block_chunk_position.y]
[block_chunk_position.x] = type;
chunk_update(chunk);
}
lets say the program is
void fun( float a=8,float b=3.7) { printf("%d,%f",a,b); }
how do i change the value of b lets say to 5.8 without interfering with a.
and also is there any error in my function as i tried in online compiler and i was getting this error
error: expected ';', ',' or ')' before '=' token
Here's a couple of snippets from a project I just started (made sure it works 1st)
GNUmakefile
GOALS:=$(or $(MAKWCMDGOALS),build)
export
$(GOALS):
$(CC) -E -x c -traditional-cpp -o tmp.mak -c ffxv-cheater.mak
make -f tmp.mak $(GOALS)
# $(RM) tmp.mak
ffxv-cheater.mak
#if 0
// This file is to be compiled into the actual makefile that will be run
#endif
NAME:=ffxv_s-cheater
#ifdef _WIN32
OUT:=$(NAME).exe
#else
OUT:=$(NAME).elf
#endif
build: $(OUT)
$(OUT): $(NAME).c
$(CC) -o $@ $<
Hi, i tried the following code, and it compiles and runs with gcc 11.4.0 and flags -Wall and -Werror :
#include <stdio.h>
enum fd_enum;
void print_enum(enum fd_enum);
enum fd_enum {
VAL_0 = 1UL << 40,
VAL_1
};
int main(void)
{
print_enum(VAL_1);
return 0;
}
void print_enum(enum fd_enum val)
{
If (val == VAL_0) {
printf("VAL_0 = %lu\n", val);
} else {
printf("VAL_1 = %lu\n", val);
}
}
When declaring print_enum()
, the enum size is not known yet. I manually forced the enum to be larger than an int
incase the compiler chosed this type by default if the enum is not defined yet. But apparently this doesn't even generate a warning and works fine, how ?
Text file treated as binary
I have a text file that is being treated as binary file when using grep without -a flag(because sometimes it contains null bytes in the file)...the text file is the output of c program...
Any way to check why this happening or how to debug this?
I'm using sokol for a simple project; it uses a custom compiler for certain .glsl files, called sokol-shdc. Apparently, it's suppose to have great IDE support, so then, how would I integrate it with meson?
I'm a relatively new programmer that is quite familar with C. Unfortunately, I havent gotten a clue when it comes to C's build systems, especially Meson. Worse yet, I'm trying to configure it with Sokol: a header only graphics library. Got any tips or tutorials?
I'm sure it has to do with operator precedence and order of evaluation, but I'm not understanding why.
Why is an ++ suffix in parentheses evaluated after the indirection outside of them?
And if the indirection is evaluated before the expression in the parentheses, why is the += operator evaluated first, even though it has a much lower precedence than the indirection operator?
I'm on Windows 64-bit and using gcc -std=c17 to compile.
Folks working in "operating systems" and "distributed systems" field, what kinds of projects are you guys working on in the company or personally? Can you share what kind of problems you guys are solving? Feel free to share details if possible even though it may be highly technical. TYIA.