C. error-prone knowledge points

Source: Internet
Author: User
Tags case statement

Refer to kuai.com for a simple sorting.

1. The following program does not necessarily output hello-STD-out. Do you know why?

# Include <stdio. h>

# Include <unistd. h>

Intmain ()

{

While (1)

{

Fprintf (stdout, "Hello-STD-out ");

Fprintf (stderr, "Hello-STD-Err ");

Sleep (1 );

}

Return 0;

}

Reference answer: Whether stdout and stderr are the same device descriptor. Stdout is a block device, and stderr is not. Block devices are input only in the following situations. 1) return a carriage return, 2) the buffer is full, and 3) flush is called. But stderr does not.

2. Next, let's look at a cross-compilation task. Can the two files below be compiled? If yes, what is the result?

File1.c

Int arr [80];

File2.c

Externint * arr;

Intmain ()

{

Arr [1] = 100;

Printf ("% d \ n", arr [1]);

Return 0;

}

Reference answer: This program can be compiled, but an error occurs during running. Why? The reason is that using extern int * ARR in another file to declare an array externally does not get the expected value because their types do not match. Therefore, the pointer does not actually point to the array. Note: A pointer to an array is not equal to an array. Modify: extern int arr []. (Refer to section 6.5.4.2 of Iso c language)

3. What is the output of the following program? And explains why?(Note that this program does not output "B is 20 ″)

# Include <stdio. h>

Intmain ()

{

Int A = 1;

Switch ()

{

Int B = 20;

Case 1:

Printf ("B is % d \ n", B );

Break;

Default:

Printf ("B is % d \ n", B );

Break;

}

Return 0;

}

Reference answer: During compilation, a warning: unreachable code at beginning ofswitch statement may appear. We thought that after entering the switch, variable B will be initialized, but it is not because the switch-case statement will directly skip the initialization of variable B. Therefore, the program outputs a random memory value.

4. What is the output of the following program?

# Include <stdio. h>

Intmain ()

{

Int I;

I = 10;

Printf ("I: % d \ n", I );

Printf ("sizeof (I ++) is: % d \ n", sizeof (I ++ ));

Printf ("I: % d \ n", I );

Return 0;

}

Reference answer: If you think the output is respectively, 10, 4, 11, then you are wrong. The error is in the third place. The first one is 10, and the second is 4, there is no problem because an int on a 32-bit machine has four bytes. But why is the third output not 11? Actually 10? The reason is that sizeof is not a function, but an operator, which calculates the size of the I ++ type. This is a task that can be completed before the program runs (during compilation). Therefore, sizeof (I ++) is directly replaced by 4, and there will be no I ++ expression at runtime.

5.What is the output of the following program? (Definitely not 10)

# Include

# Defineprintint (expr) printf ("% s: % DN", # expr, (expr ))

 

Intmain ()

{

Int y= 100;

Int * P;

P = malloc (sizeof (INT ));

* P = 10;

Y = y/* P;/* dividing y by * p */;

Printint (y );

Return 0;

}

Reference answer: The output in this question is 100. Why? The problem lies in Y = y/* P;. we originally thought of Y/(* P). However, we did not add spaces and parentheses, result/* in Y/* P is interpreted as the beginning of the annotation. This is also the beginning of the nightmare.

6. What is the output of the following program?(Assume: Input Hello, world)

# Include <stdio. h>

Intmain ()

{

Char dummy [80];

Printf ("enter a string: \ n ");

Scanf ("% [^ r]", dummy );

Printf ("% s \ n", dummy );

Return 0;

}

Reference answer: The output in this example is "Hello, wo", and "% [^ r]" in scanf is a stem. It means that the character R is over.

 

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.