Why does this compile? [C++]

compilation process in c with diagram
compilation and linking process in c
compiler in c language
compilation process in c++
how to compile c program in linux
how to compile c program in windows
explain the process of compiling and running c program
compile c program gcc

I tried to look for an answer online but couldn't quite find it.

Today I saw these lines of code:

    int main(){
    int n = 7;
    while(n /= 10);
    }

It doesn't make much sense, but the question was only 'will it compile?'. To which I answered no, but I was wrong.

My question is, why? Why does

    n /= 10

behave like a bool (or an int) here?

An assignment (including a compound assignment like /=) is an expression, which yields the value that was assigned1.

So, you can do something like: x = y = z = 0;, which assigns 0 to z, takes the result of that (also 0) and assigns it to y, and takes the result of that (still 0) and assigns it to x.

From there, it's making use of the implicit conversion from int to bool, in which 0 converts to false, and any non-zero value converts to true.


1. Note: that's what happens for built-in types. By convention, when/if you overload operator= for a class, you have it return *this;, so it works the same way, as a user would/will expect--but that part's not mandatory--you can overload your operator= to return a different value or an entirely different type--but this is almost always a bad idea and should usually be avoided.

Compiling a C program:- Behind the Scenes, How do we compile and run a C program? Below are the steps we use on an Ubuntu machine with gcc compiler. compilation We first create a C program using​  C is a high-level language and it needs a compiler to convert it into an executable code so that the program can be run on our machine. How do we compile and run a C program? Below are the steps we use on an Ubuntu machine with gcc compiler. We first create a C program using an editor and save the file as filename.c $ vi filename.c

What you have here for your while loop is as follows:

while ( expression );

If the expression is true or non 0 the loop will continue; otherwise if it evaluates to false or 0 it will terminate. So looking back at your original:

int n = 7;
while ( n /= 10 );

This then becomes:

while ( n = 7 / 10 ); 

Here the full expression is n = 7 / 10; This should result in 0 due to truncation of integer arithmetic. The value by implicit conversion from int to bool becomes false. As the yielded result is 0.

There is nothing here preventing this from compiling. As this is no different than having:

while ( false );

However with assignment and arithmetic operations; this may not always be the case, but in your case it is. Consider this next example: This will still compile but the loop will not terminate:

int n = 5;
while( n + n );

This will then become:

while( 5 + 5 );
...
while( 10 );
...
while( true );

Which will still compile but the loop will continue infinitely.

Programming - Compiling C Programs, Compiling a C Program. Compiling is the transformation The compiler does not ensure that your program is logically correct. The compiler we use is the GNU  The compiler does not ensure that your program is logically correct. The compiler we use is the GNU (Gnu is not Unix) Open Source compiler. G++ is the name of the compiler. (Note: G++ also compiles C++ code, but since C is directly compatible with C++, so we can use it.). To compile a program, you use the following command:

Just like += and -= work, *= and /= work.

In fact, there are also &= and |=.

These all evaluate to the new value that has been assigned.

And, as you know, you don't have to put a boolean in a while/for/if condition; you only need to put something there that can be converted to a boolean.

For example, if (42), or for (char* ptr = begin; ptr; ++ptr), or while (n /= 10).

Compiling a C program, This chapter describes how to compile C programs using gcc . Programs can be compiled from a single file or from multiple files, using system libraries and  The Compiler Does a Lot More Than You May Think. In this article, you have learned about some of the basic language constructs of C++ and how the compiler works with them, starting from the processing stage to the linking stage.

C++ will convert the n /= 10 to bool. Integers = 0 converted to bool evaluates to false. Integers != 0 converted to bool evaluates to true. That while will be evaluated as while(false).

Why does this C program compile with no errors?, Should there be a linker error? The short answer to "Shouldn't there at least be linking errors?" is "There is no guarantee that there'll be a  A compiler is a program that translates human-readable source code into computer-executable machine code. To do this successfully, the human-readable code must comply with the syntax rules of whichever programming language it is written in. The compiler is only a program and cannot fix your code for you.

Walkthrough: Compile a C program on the command line, Visual C++ includes a C compiler that you can use to create everything from basic console programs to full Windows Desktop applications,  Walkthrough: Compile a C program on the command line. 04/25/2019; 10 minutes to read +2; In this article. Visual C++ includes a C compiler that you can use to create everything from basic console programs to full Windows Desktop applications, mobile apps, and more.

Why does my GCC compiler not compile C code?, Traditionally, gcc always names the executable "a.out" unless told otherwise. You should use: gcc -o gcc_test.exe gcc_test.c. You can probably  When you write a program in C/C++, the traditional thing people know is, a compiler magically turns your C/C++ code into native code for that machine. But something doesn't add up here. If I compile my C/C++ program targeting the x86 architecture, it would seem that the same program should run on any computer with the same architecture.

Compiling and Linking, Compilation refers to the processing of source code files (.c, .cc, or .cpp) and the creation of an 'object' file. This step doesn't create anything the user can  Compiling C program from IDE is fairly simple. In this post I will explain how to compile and run C program using command line and GCC compiler in windows. Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.

Comments
  • Any non-zero value is treated as true in C++.
  • The operator /= is correct and is equivalent to : ( n = n / 10) Just like the += operator which is equivalent to : (n = n / 10)
  • You're going to be horrified in the future by how much compiles when you don't want it to.
  • Isn't n = 7/10 going to be 0, since the values are integers? Aside from that the answer is pretty clear.
  • @auouo 7/10 depends on the compiler and how it does integer arithmetic. If it truncates it then yes it would be 0 and false but if it doesn't and see's that it's a non 0 value it will treat it as true. However I think you are correct about it being 0.
  • @auouo I've edited my answer to reflect that as I forgot about the truncation....
  • No, it won't run forever because n will change each loop iteration.
  • Integers != 0 are false.
  • @stark: No, integers == 0 are false, integers != 0 are true.
  • in fact it wont run even once
  • Editet, Integers !=0 are true.