How to input negative numbers in the C language?

In C language, negative numbers can be represented by adding a minus “-” sign in front of the number. For example, to input -1, you can use the following code:

int num = -1;
printf("%d\n", num);

The output result is -1.

Alternatively, if you want to obtain a negative number from user input, you can use the scanf function and add a negative sign before the inputted number. For example:

int num;
scanf("%d", &num);
num = -num;
printf("%d\n", num);

In this way, if the user inputs 1, the program will output -1.

bannerAds