C pointer principle (69)-C Quick Start (2)

Source: Internet
Author: User
Tags integer numbers

However, if we enter numbers such as 900 and-10 in the above program that do not meet the requirements,

Hello, please enter a number: 900

The number you entered is: 900

Hello, please enter a number:-10

The number you entered is-10.

The system does not prompt an error. Therefore, you need to modify the program to limit the range of players entering numbers.

# Include

Int main (){

Int mynum;

Printf ("hello, please enter a number :");

Scanf ("% d", & mynum );

If (mynum> 500 | mynum <1 ){

Printf ("the number is limited to 1-. Please run this program again! \ N ");

}

Else {

Printf ("\ n the number you entered is: % d \ n", mynum );

}

}

Run the program:

Dp @ dp :~ /Cquick % gcc guessnum. c-o myguess

Dp @ dp :~ /Cquick %./myguess

Hello, please enter a number: 200

The number you entered is: 200

Dp @ dp :~ /Cquick %./myguess

Hello, please enter a number: 900

The number is limited to 1-. Please run this program again!

Dp @ dp :~ /Cquick %

The above program uses the C language if... else... statement block, which is a mechanism available in many languages (including some functional languages, such as the if... then... else ....), the statement block consists of two parts: the first part of the if statement segment, which indicates that if the condition following the if statement is met, the if statement segment is executed, and the second part is the else statement segment, if the condition after if does not meet the execution condition, what is the condition and does not meet the condition? C language can be understood as if the return value of the condition is not 0, the condition is met, and if it is 0, the condition is not met.

You can combine multiple conditions into a comprehensive condition as the condition for the statement block by using | (represents or) or & (represents and), such as the conditions in the above program: mynum> 500 | mynum <1

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

However, there is a keyword NULL in C. In C, the NULL and 0 values in the Condition Statement are the same, but NULL is often used for pointers and objects, 0 is commonly used in integer numbers such as int. This means that if the following similar statement blocks appear, it means that the pointer variable is contained (the pointer variable is usually used to point to the memory address of other variables, such as mypoint1 and mypoint2 in the following code) the following conditions are determined:

If (mypoint1! = NULL ){

............

}

If (mypoint2 = NULL ){

............

}


According to the running effect, the wrong number is not accepted, but to re-enter it, you must re-run the program.

Related Article

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.