Skip to main content

Posts

Showing posts with the label C

Are these lines of code in C programming the same

Are these 2 lines of code the same ?? line 1: void (**foo)(int) line 2 void *(*foo)(int) Kindly help me understand on what is happening. by Pisers in StackOverflow on July 02, 2022 . Answer They are not the same. void (**foo)(int); foo is a pointer to a pointer to a function that takes an int parameter and returns void . void *(*foo)(int): foo is a pointer to a function that takes an int parameter and returns a pointer to void . Postfix operators like () and [] have higher precedence than unary * , so T *a[N]; // a is an array of pointer to T T (*a)[N]; // a is a pointer to an array of T T *f(); // f is a function returning pointer to T T (*f)(); // f is a pointer to a function returning T by John Bode on July 02, 2022 .

How to read the content from the second line onwards on a .txt file on C

I need help to read the numbers of a .txt file and put them in an array. But only from the second line onwards. I'm stuck and don't know where to go from the code that i built. Example of the .txt file: 10 20 45000000 48000000 56000000 #define MAX 50 int main (void){ FILE *file; int primNum; int secNum; int listOfNumers[50]; int numberOfLines = MAX; int i = 0; file = fopen("file.txt", "rt"); if (file == NULL) { printf("Error\n"); return 1; } fscanf(file, "%d %d\n", &primNum, &secNum); printf("\n1st Number: %d",primNum); printf("\n2nd Number: %d",secNum); printf("List of Numbers"); for(i=0;i<numberOfLines;i++){ //Count the number from the second line onwards } fclose(file); return 0; } by LearningLinux in StackOverflow on July 03, 2022 . Answer ...

why does the do while skip scanf when none digit character is inputted

In c I have this snippet int n=0; do { printf("random text\n"); scanf("%d", &n); } while ( n < 1 || n > 10); when the user inputs anything other than a number the loop spams the printf without waiting for user input why does this happen and how do I fix this by packet_sniffer in StackOverflow on June 05, 2022 . Answer You need to skip the current content of the buffer if it does not contain a valid number. For example int n=0; do { printf("random text\n"); if ( scanf("%d", &n) == 0 ) { scanf( "%*[^\n]" ); } } while ( n < 1 || n > 10); Pay attention to that this prompt printf("random text\n"); is confusing when you expect from the user to enter a number. In general you should also add a check for EOF. For example int n=0; do { n = 0; printf("random text\n"); int...

Object not rendering after adding transformations

I'm adding transformations to my C OpenGL program. I'm using CGLM as my maths library. The program has no warnings or errors. Still however, when I compile and run the program, I simply get a window coloured my clear colour. The following is my program's main loop // Initialize variables for framerate counting double lastTime = glfwGetTime(); int frameCount = 0; // Program loop while (!glfwWindowShouldClose(window)) { // Calculate framerate double thisTime = glfwGetTime(); frameCount++; // If a second has passed. if (thisTime - lastTime >= 1.0) { printf("%i FPS\n", frameCount); frameCount = 0; lastTime = thisTime; } processInput(window); // Clear the window glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Bind textures on texture units glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glActiveTexture(GL_TEXTURE1); glBindTex...

Unexpected output - Choosing the lowest number from an array

I want to find the smallest number from the numbers in an array entered by the user. The output I am getting for the program is a vague number -858993460. This is generally because of a type mismatch but they seem alright here. Not able to think of a solution. Thanks for the help! ``` // #include<stdio.h> int main() { int a; int i; int num[25]; printf("Enter the 25 numbers:"); for (i = 0; i <= 24; i++) { scanf("%d", &num[i]); } for (i = 0; i <= 24; i++) { a = num[i]; if (a < num[i+1]) { a = a; } else a = num[i+1]; } printf("Lowest number is %d\n", a); return 0; } by JP0605 in StackOverflow on 2022-06-05 . Answer Hope this will work for you. Jus...