Confusion in C Language Compilers

Source: Internet
Author: User

Yesterday, I participated in the so-called "Programming Competition" C language testing organized by the college. There are three questions, which are very painful. The first question: the number of output values between 1 and 1000 that can be divisible by 7 or 11, but cannot be divisible by 7 and 11 at the same time. Question 2: Enter three numbers to determine whether a triangle can be formed. If a triangle is formed, determine whether it is a right triangle, an isosceles triangle, or an equedan triangle. Question 3: The variant of Joseph's ring.

Most of the above are nonsense. The last sentence is the key. We can see that it is a Joseph ring. We usually use a linked list or a malloc statement to dynamically create an array. This time I want to write a simple code:

 

# Include <stdio. h> </P> <p> typedef struct node {<br/> int number; <br/> int tag; </P> <p>} people; </P> <p> void yuesefu (int n, int M, int K) {</P> <p> People people [N]; </P> <p> ...... </P> <p> ...... </P> <p> ...... </P> <p >}</P> <p> int main (INT argc, char * argv []) <br/>{</P> <p> int n, m, K; <br/> printf ("Enter students "); <br/> scanf ("% d", & N); <br/> printf ("Enter the number of the first person to leave "); <br/> scanf ("% d", & M); <br/> printf ("enter a number for the team "); <br/> scanf ("% d", & K); <br/> yuesefu (n, m, k); </P> <p> return 0; <br/>}</P> <p>

At that time, the data center used the VC ++ compiler. After compilation, the following error was displayed:

 

-------------------- Configuration: Test-Win32 debug --------------------
Compiling...
Test. cpp
D:/mobile app/test. cpp (11): Error c2057: Expected constant expression
D:/mobile app/test. cpp (11): Error c2466: cannot allocate an array of constant size 0
D:/mobile app/test. cpp (11): Error c2133: 'people': Unknown size
Error executing cl.exe.

Test. obj-3 error (s), 0 warning (s)

 

Okay, I remember. When I learned C language last year, I seem to have heard that dynamic arrays cannot be defined in C. However, I seem to have written this course in the data structure experiment class, and I still passed it. I used the dev C ++ compiler in the data structure experiment class. Okay, I went around and made it. I used a Fixed Array with a capacity of 100. However, it seems like this is not good. After all, I have done so many questions similar to Joseph's rings, so he shamelessly took out the USB flash drive and copied the experiment course program. Amitabha, sin! Sin !!

 

When I came back, I also lamented that I had to pay for my code every day, leave the materials and the network, and I couldn't write anything.

 

This weekend, I have nothing to worry about. I just want to see the problem of yesterday. So I put the above Code into Dev C ++. That's right !!!!!

 

I simplified the program to study the problem. The following test is performed:

 

 

In Dev:

 

# Include <stdio. h> <br/> void F (int n) {<br/> int I; <br/> int A [n]; <br/> for (I = 0; I <n; I ++) {<br/> A [I] = I; <br/> printf ("% d/T", a [I]); <br/>}</P> <p> int main (INT argc, char * argv []) <br/>{< br/> F (5); <br/> system ("pause"); <br/> return 0; <br/>}</P> <p>

 

Compilation is normal and no warning is provided. Output: 0 1 2 3 4

 

 

Put it in VC ++:

 

 

-------------------- Configuration: Test-Win32 debug --------------------
Compiling...
Test. cpp
D:/mobile app/test. cpp (5): Error c2057: Expected constant expression
D:/mobile app/test. cpp (5): Error c2466: cannot allocate an array of constant size 0
D:/mobile app/test. cpp (5): Error c2133: 'A': Unknown size
Error executing cl.exe.

Test. obj-3 error (s), 0 warning (s)

 

Again. Therefore, the conclusion is that Dev C ++ is very tolerant of arrays in C language.

 

 

This cannot end, so I pulled out the textbook last year.

The textbook writes as follows:

Dynamic Arrays cannot be defined in C. The array size (number of elements) is defined explicitly. The array defined below is incorrect:

Int N; </P> <p> scanf ("% d", & N); </P> <p> int C [N]; </P> <p>

At that time, the school data center used VC ++, so I was convinced of this. But now I find that this is correct in Dev C ++.

 

When I first learned the C language, I knew that the pure C compiler was gone. C ++ is backward compatible with C, so the C ++ compiler can fully meet the needs of C.

I suddenly thought of this section in the last C PPT Tutorial:

 

 

// C Language Program <br/> void main () <br/>{< br/> int A; <br/> A = 10; <br/> int B; <br/> B = a + 20; <br/>}< br/>

The PPT adds that the variable definition is placed after the execution part. Again, in C language programs, variables must be defined first and then used. The order cannot be reversed !!!

Therefore, it is a wrong program in C, and it is right to put the above program in C ++.

That is to say, when we used to learn C, because we used a C ++ compiler, the C we learned is a kind of mixed C ++. The teacher did not tell us.

I remember that in the first C language lesson, our head of the Department also solemnly told us that C ++ is not an extension of C. Do not mix the two ...... Sorry ......

 

C is powerful and flexible, but I feel a little confused now. For example, if the scanf () Buffer is damn bad, does sizeof () automatically add/N after calculating the character array? For example, Dev C ++, VC ++, and turboc add consecutive left, how many children's shoes of C language used to have a nightmare ......

 

 

Java is becoming more and more popular. At least Java exception handling is very useful, and the error reporting capability of the Java compiler is also strong. But Java's garbage collection makes Java doomed to be unable to do large games (think about it, when you are playing a very high online game, the garbage collector is working, and the program is resting for a few minutes ...... What will it look like ............)

 

Even so, I am sure of C's position. C is very powerful! I posted this blog because I had never written anything since I was so good. Just say a few words ......

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.