This question already has answers here:
Strange label usage for an IF condition in a DO loop [duplicate]
(2 answers)
Closed 8 years ago.
I am converting an old code in Fortran which I think is a 77. there a couple of usage of IF in the following way that I can't realize what it does:
IF(x1-x2) 12, 13, 14
12 WRITE(*,*) x1
.
.
.
13 y=...
.
.
14 DO x=x1,x2
IF(x-x2) 33, 34, 40
.
.
.
The code complies and runs fine and it produces the results. Has anyone encountered such a usage of IF?
That's an arithmetic IF. It branches to the first, second, or third label depending on whether the expression is negative, zero, or positive.
In very old versions of Fortran (or FORTRAN), it was the only form of IF statement that was available.
It's probably a little odd to see it used in a version as recent as Fortran 77, which had a more modern logical IF. The feature was declared obsolescent in Fortran 90, but I wouldn't be surprised if more modern Fortran compilers still support it.
Related
This question already has answers here:
In C++ books, array bound must be constant expression, but why the following code works?
(2 answers)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 11 months ago.
first timer here!
This might seem like a really dumb question but I have tried a lot of different threads on this website already but without any luck... hope that someone might have an idea, thanks in advance!
I get an error on input n for the array below (image attached too):
int n, q;
cin >> n >> q;
long long BITree[n+1];
return 0;
Error shows: "expression must have a constant value C/C++(28)"
This error message has several posts on this website already however I am positive that this should work as I do not get this error when trying the same code on my laptop (also able to compile and run on the laptop).
Things that I've tried so far:
Checked all extension settings (as well as versions),
Checked all Visual Studio Code Settings (as well as version),
Checked that I am using the same compiler version
Currently using: g++ (Rev5, Built by MSYS2 project) 11.2.0
This has bugged me for quite some time and I have taken the long road of always using the other threads to solve it but I really want to find out why this is happening. Does anyone have any ideas or could link me to a more appropriate thread about fixing this?
Thanks again!
This question already has answers here:
can i use c++ compiler to compile c source code? [duplicate]
(3 answers)
What is the difference between g++ and gcc?
(10 answers)
Closed 2 years ago.
It's what I believe to be a very simple question.
Context: I'm following a tutorial that allows me to run C++ code in Visual Studio Code, but I'm trying to run C code, not C++ code. The program I'm trying to run is a simple Hello World program (shown below), but this question applies to all C code.
#include <stdio.h>
int main() {
printf("Hello World!")
}
C and C++ are different languages. And even though they share a similar syntax, the semantic meaning of certain constructs are different.
C++ incorporates a large part of C, but it also diverges. You cannot just assume that C code compiled as C++ will give the same result.
You can write code that is both valid C and valid C++ yet mean different things in the two languages.
While C++ can be seen for the most part a superset of C, there are some constructions that are invalid C++ and others that have different behavior.
Instead of dealing with that, tell your compiler to target C instead of C++. All the popular C++ compilers also support C (at least one version).
This question already has answers here:
Does a compiler always produce an assembly code?
(4 answers)
Closed 3 years ago.
What is the reason that languages like C, C++ and similar compile their code down to assembler code, instead of just producing the binary directly? Is it just too hard to infer the "correct" programming from the abstracted language? It seems to me that converting to something that will again be converted is not an optimal way of doing things, but there are probably good reasons for this that I am unaware of. Is this connected to every CPU architecture having different implementations?
Assembler code and the binary are logically equivalent, the assembler code is just represented using so-called mnemonics which are a more human-readable form of the machine instructions.
All compilers do directly produce a binary.
This question already has answers here:
dollar sign in variable name?
(4 answers)
Closed 5 years ago.
I am a newbie to c++. I found the following statement in a C++ book: "In any C++ program, a variable name starts with a letter and contains only letters, digits, and underscores.The following are not variable name:"
2x // a name must start with a letter
time$to$market // $ is not a letter, digit, or underscore
Start menu // space is not a letter, digit, or underscore
The question is why time$to$market is not variable name?
I tried to compile it and the compiler did not complain, the compiler i use is MinGW 32bit for c++ in QT. However it should complain!
There are different variations of compilers. One may compile faster, one may differ with some default settings for some reasons. I don't know what compiler you are using. But if you make a bit reaserch on your compiler you will see that $ is allowed. Try to compile with different compiler to catch up exactly with your book. Check this topic to see what does pure c++ mean. Because books usually teach pure language.
What is pure C++
I tried to use it and the compiler did not give error.
Just don't do it, even if your current compiler supports it (as Quentin points out in a comment). It's not in the standard, so your code will not be portable to other environments, or even to the same compiler using a different standard (try the GCC options -ansi, -std=c++11 etc.). The best that can happen is that you get an error on compilation, the worst is undefined behaviour you'll be hunting after for many hours.
Its totally depends on the complier you are using, as I am using in TC 4.5 it show this error:
Compiling DEMO.C:
Illegal character '$' (0x24)
Some compiler may support it.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Code convert from C++ to C
Two months ago, my instructor have asked one question, I have searched to looking for answer but I could not find it.
question :
From c++ code, how can one generate c code just using console ( with g++ ) .
How can I do this ?
g++ compiles C++ directly to machine code, it does not first compile to C then compile that.
There may be some compilers that compile to C code first. I do not know of any if you really need the code. It is not the most efficient way to do it though.
I think that is what your instructor was trying to ask you, i.e. if there is a compiler switch to generate C code.
Is there any particular reason why you need to generate C code. Creating a C interface can be useful and there are ways to do this.
I'm far from a GNU expert, but I belive the compiler option would be -std=C89, to enforce compilation to follow the C90 version of the C standard (equivalent to old "ANSI C" which was released in 89, hence the C89), or -std=C99 for the C99 version of the standard.