C language Programming (Onge)--three remaining points in the third week of courseware

Source: Internet
Author: User
Tags case statement

Just finished the second week left, and the following is the third week.

Week Three: Judging

1.if and else can be followed by a statement without {}. How will the program execute if it does not have {} after it, but follows two statements followed by an else statement?

Enter the following code in Dev C + + to compile

#include"stdio.h"intMain () {intA=0; scanf ("%d",&a); if(a>0) A+=1; printf ("a=%d", a); ElsePrint ("a=%d", a); return 0;}

The following questions were found:

Tip "9 2 [Error] ' else ' without a previous ' if '"

It means that else could not find the corresponding if, the second statement after the IF, printf makes the compiler think that if the end, it is a non-else if statement, so the subsequent direct input else will cause the compiler to error.

2./* */is a comment, the compiler regardless of its contents. But have you ever wondered what the compiler would replace it with? Is it a space? Or Leave no trace? Can I write code to test the compiler you use?

Enter the following code in Dev C + + to compile

#include"stdio.h"intMain () {int/*SDFD*/b//compiled byinch/*SDFF*/t A;//compile error, prompt [ERROR] unknown type name ' in ' and [ERROR] expected ' = ', ', ', '; ',//' asm ' or ' __attribute__ ' before ' a '    return 0;}

found that the first statement was compiled, and the second statement was compiled with an error, prompting "[ERROR] Unknown type name ' in ' and [ERROR] expected ' = ', ', ', '; ', ' asm ' or ' __attribute__ ' before ' A ', judged by these two results,/**/is replaced by the compiler when it is compiled with a space

3. Can the segmented function be used with switch-case? Is it worth it?

Considering that X=0 and x>0 are the same function f (x) =2*x, then you can divide the type into 0/12 cases to use the Switch-case statement, it is not worth the cost, and if the segment is increased, it is not necessarily possible to implement this method

The code is as follows:

#include"stdio.h"intMain () {inttype; Doublex,f; scanf ("%LF",&x); Type= (x<0); Switch(type) { Case 0: F=2*x;  Break; default: F=-1;  Break; } printf ("%f", F); return 0;}

C language Programming (Onge)--three remaining points in the third week of courseware

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.