how to stop scanf from adding a new line in c

To stop scanf from adding a new line in C, you can use the scanf function along with the %c format specifier to capture the newline character after reading the input. Here is an example:

#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf(" %c", &ch); // Notice the space before %c to consume the newline

    // Your code to use the character 'ch' goes here

    return 0;
}

Explanation: 1. #include <stdio.h>: Includes the standard input-output library in C. 2. char ch;: Declares a variable ch of type char to store the input character. 3. printf("Enter a character: ");: Displays a prompt message to the user. 4. scanf(" %c", &ch);: Reads a character from the standard input. The space before %c is used to consume any leading whitespace characters, including the newline character left in the input buffer from previous inputs. This prevents scanf from immediately consuming the newline character. 5. After this step, you can use the ch variable as needed in your code.

Remember, using %c to read a single character won't consume any leading whitespace except for the newline. If you need to clear the buffer after inputting something else, additional considerations may be necessary.