[Solved] Declaring a function in a header file which references structs defined in different header files

You will need to include file1.h and file2.h in file3.h instead of file3.c. As func3 uses struct1 and struct2, you can’t declare it without having the declarations of the structs on hand. Don’t be tempted to redeclare the structs in file3.h. solved Declaring a function in a header file which references structs defined in different … Read more

[Solved] buffer overflow Identification in the code

1. while ((buffer[i++] = getchar()) != ‘\n’) You have to be sure that the number of characters being entered is less than 4096. Else you have a buffer overflow. While reading until the end of the line it would be better to use fgets() which is much safer. 2. strcpy(newbuffer,buffer); What if your array buffer … Read more

[Solved] What does this following C code implement [closed]

The code is probably intended to create an identity matrix with 1’s on the leading diagonal and 0’s elsewhere. The Fortran equivalent of that code is used as the first example of bad code in the classic Elements of Programming Style by Kernighan and Plauger. Working from memory, the Fortran code was roughly: DO 10 … Read more

[Solved] Slot Machine in C (gotoxy)

You have infinite loops in all functions. If you enter one function you never return. Consider putting this while in main function. #include <stdio.h> #include <time.h> … int main(){ srand( time(0) ); fnSlotMachine(); while(1) { fnSlot1(); fnSlot2(); fnSlot3(); } } … void fnSlot1(){ Sleep(50); fnGotoXY(5, 9); intSlot1 = rand() % 9; printf(” | %i %i … Read more

[Solved] Use memory on stack

why not the program crash, or print some random garbage? The program will not crash as you are not accessing any illegal memory. The stack memory is a part of your program and as long as you are accessing the memory in a valid range the program will not crash. Yes, you can modify the … Read more

[Solved] This C program wont compile/run? [closed]

Try this code #include <stdio.h> #include <windows.h> #include <math.h> #define PI 3.141597 #define WAVELENGTH 70 #define PERIOD .1 int main() { float i,s,x; i = 0; for (i=0;i<=PI;i+=PERIOD) { s = sin(i); for (x=0;x<s*WAVELENGTH;x++) { putchar(‘*’); } putchar(‘\n’); } return 0; } 1 solved This C program wont compile/run? [closed]

[Solved] Character Array Declaration and initialization [duplicate]

Per the online C2011 standard, it is not valid; you may not have an empty initializer list (see 6.7.9, Syntax). That doesn’t mean a specific implementation can’t offer an empty initializer list as an extension, but the utility would be unclear. Beyond that, the compiler has no way of knowing how much storage to set … Read more

[Solved] C prog. Pointers and strings [closed]

In your first example, you MUST pass a char pointer for the “%s” modifier so it is actually the way it has to be, you would of course know that if you read the appropriate documentation, like e.g. The C Standard. The second one, is wrong. Because it would invoke undefined behavior. To print the … Read more

[Solved] Exit when press 0 in C [duplicate]

Exapmple One of the ways. #include <stdio.h> #include <stdlib.h> void end_proc(void){ printf(“Quitting the program…\n\n”); exit(0); } void input_error_proc(void){ int ch; printf(“ERROR\n”); while((ch = getchar()) != ‘\n’ && ch != EOF);//clear input } int main(void){ int program; while(1){ printf(“\n” “Choose one of the following programs: \n\n” ” (1) Fibonacci Sequence Calculator \n” ” (2) Decimal and … Read more