Why does int's value change?

int? c#
int size
size of int in java
32-bit integer
16-bit integer limit
32-bit integer limit
int number

I have this C code:

int a = 5;
printf("a is of value %d before first if statement. \n", a);
if (a = 0) {
    printf("a=0 is true. \n");
    printf("a=0 is not true. \n");
printf("a is of value %d after first if statement. \n", a);
if (a == 0){
    printf("a==0 is true. \n");
    printf("a==0 is not true. \n");
return 0;


a is of value 5 before first if statement.
a=0 is not true. 
a is of value 0 after first if statement. 
a==0 is true. 
Program ended with exit code: 0

I do not understand why the int value is still recognized as 5 in the first statement, but changes to 0 before the 2nd if, or why it changes at all?

When you do if (a = 0) you are setting the variable a to 0. In C, this will also evaluate the expression to 0.

So actually that if-statement works in two steps. It's as if you did:

a = 0; //assign 0 to a

if (a) {  ... } //evaluate a to check for the condition

In which case, since a is 0, it evaluates to false. That's why you end up in the else of the first part, and in the second part (a == 0) evaluates to true!

What Does Int Mean in C, C++ and C#?, for the specific system the program is executed on. Form 1099-INT: The form issued by all payers of interest income to investors at year's end. Form 1099-INT breaks down all types of interest income and related expenses. Payers must issue Form 1099

in the first if you need to use "==".

if(a == 0) {

Is the size of C "int" 2 bytes or 4 bytes?, has a range of -128 to 127 - both representing 256 distinct numbers. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more

in if (a = 0) your assigning value 0 to a.

To do a comparison you should do in if (a == 0) (so with double ==)

IntegersFloats \ Examples \ Processing.org, This is because you are using the integer division version of operator/ , which takes 2 int s and returns an int . In order to use the double version  An int (short for "integer") is a whole number. A float (short for "floating-point number") is a number with a decimal point. int() returns an int created from its input argument. You can use it to convert a string like "15" into the int 15, or a float like 12.059 into the int 12.

if (a = 0) 

In the above if statement, first 0 is assigned to the variable a. Then the condition is evaluated.

Because a now holds the value 0, it is evaluated as false and the corresponding else block is executed.

In C language, true is represented by any numeric value not equal to 0 and false is represented by 0.

I do not understand why the int value is still recognized as 5 in the first statement, but changes to 0 before the 2nd if, or why it changes at all?

Because, after execution of the first if statement, value of a is 0. So, when second if statement is evaluated, a has the value 0, and thus the condition if (a == 0) evaluates to true.

A Tutorial on Data Representation, The INT function syntax has the following arguments: Number Required. The real number you want to round down to an integer. Example. Copy the example data​  There are essentially three ways to do what you are trying to do, each with benefits and drawbacks. Integer math: this results in truncating results during division as you found out. If you want the decimal portion, you need to treat that separately by dividing, getting the remainder, and treating the decimal portion as the remainder divided by

0 is false, ie. not true.

That is why the first if-statement behaves like it does.

And a=0 assigns 0 to a, which is the reason for its value afterwards.

Why does dividing two int not yield the right value when assigned to , In Java SE 8 and later, you can use the int data type to represent an unsigned 32-​bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use​  int as a signed decimal number. '%d' and '%i' are synonymous for output, but are different when used with scanf() for input (using %i will interpret a number as hexadecimal if it's preceded by 0x, and octal if it's preceded by 0.)

INT function, The numeric types are the integral types byte , short , int , long , and char , and Because the null type has no name, it is impossible to declare a variable of the  The Internal Revenue Service requires most payments of interest income to be reported on tax form 1099-INT by the person or entity that makes the payments. This is most commonly a bank, other financial institution or government agency.

Primitive Data Types (The Java™ Tutorials > Learning the Java , If the literal has no suffix, its type is the first of the following types in which its value can be represented: int , uint , long , ulong . If the literal is  According to Wolfram Alpha, $\int_{-\infty}^{\infty}\sin(x)dx$ does not converge.. This makes no sense to me, intuitively, which I'll justify with a plot: As we see, the positive and negative areas 'cancel out', so, for any $\alpha \in \mathbb{R}$, $\int_{-\alpha}^{\alpha}\sin(x)dx=0$ (I'm just thinking geometrically- in no way is this supposed to be a rigourous justification).

Chapter 4. Types, Values, and Variables, Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (​base To explicitly convert a value to integer, use either the (int) or (integer) casts. The int 20h is the “exit program” system call. One theory is that it is placed at offset 0000h so that if execution runs off the end of the code segment, the instruction pointer will wrap back around to zero, and then the program will terminate.

  • a = 0 is an assignment, not a comparison
  • You're saying you don't understand why a=0 changes the value of a to 0?
  • You typoed the first if. ANd it looks like it works because the result of an assignment is the valued assigned. Which here is 0. So it returns false. You should write comparissons with constants as 0==a instead so this becomes a compile time error.
  • Doesn’t look like a typo to me, since he specifically repeated the assignment operation in the following two printf calls. Seems to me he’s trying to understand how exactly an assignment inside an if statement works in C. See my answer below.
  • Please mark the best answer as accepted. Read more here