Why divide a variable before type conversion?

I’m looking at the RNG from K&R2 2.7 Type Conversions: unsigned long int next = 1; /* rand: return pseudo-random integer on 0..32767 */ int rand(void) { next = next * 1103515245 + 12345; return (unsigned int)(next / 65536) % 32768; } /* srand: set seed for rand() */ void srand(unsigned int seed) { next… Read More Why divide a variable before type conversion?

How does enums types initializations in recursive functions work?

If I have an enum type declaration in a recursive function, will it be ‘created’ multiple times? Does it work like ‘#define’ in the sense that it just tells the pre-processor to ‘swap’ values (but with scope in mind)? What would happen here, per example: void recuFun() { enum someEnum {UP, DOWN, LEFT, RIGHT}; //… Read More How does enums types initializations in recursive functions work?

Struct initialization with char array outside a function

I have following typedef struct: typedef struct { E_Menus Menu; // E_Menus is a typedef enum char *MenuStr[200]; int CurrentItem; int ItemAmount; int ItemGap; int StartItemGap; } T_Menu; I want to initialize a struct of type T_Menu as a module variable (not local inside a function) as following: char mBootMenuStr[] = {"some text"}; T_Menu mBootMenu… Read More Struct initialization with char array outside a function

Can't understand why its not working when using two variables, the same works if i use just a single variable

#include <stdio.h> int main() { char *a,*b; scanf("%s %s",a,b); printf("%s %s",a,b); return 0; } It works like this, #include <stdio.h> int main(){ char *a; scanf("%s",a); printf("%s",a); return 0; } I think its something with memory becoz when i use malloc and assign some memory it works. >Solution : It is undefined behaviour (UB) in both… Read More Can't understand why its not working when using two variables, the same works if i use just a single variable

Getting calculation into struct field type of char

I have declared following struct typedef struct STRUCT{ char calculation[30]; }STRUCT; I have a function: int add_number(int num1, STRUCT *pointer) { int integer; int sum; printf("\nGive an integer to be added: "); scanf("%d", &integer); sum = num1 + integer; printf("%d+%d=%d\n", num1, integer, sum); scanf("%s", pointer->calculation); // here I would want to get the %d+%d=%d stored… Read More Getting calculation into struct field type of char