Location>code7788 >text

C data types, variable inputs and outputs, and conversion.

Popularity:286 ℃/2024-09-26 17:51:26
  The scanf standard function gets a number from the keyboard and records it into memory. In order to use this standard function you need to include this header file

In the scanf function call statement, you should use the address of the storage area to indicate the storage area; in double quotes, you should use a placeholder to indicate the type of storage area.

Try not to write anything that is not a placeholder in the scanf function call statement. If the format of the user's input is different from the format required by the program

If the user input format and the program requires a different format, the program can not get the number, you can get more than one number in a scanf function call statement.

Exercise:

Write a program to get a floating point number from the keyboard, use this floating point number as a radius to calculate the circumference of a circle and display the result on the screen



include<>

int main(){
float num=0;
scanf("%g",&num);
printf("Input value is: %g\n",num);
return 0;
}



  A byte can be divided into eight segments, each of which can be used to record a 0 or a 1.

To record a number into a byte must first be split into eight 0 or 1, with a group of 0 or 1 to represent the number of methods called binary,

Any one of the numbers can be marked both in decimal and binary mode, the computer can only record the number of binary representation of the number

binary number in each position has a number, the right position of the number of 0, to the left in ascending order

Binary numbers in each digit of 1 alone represents a number, the number is 2 to the next digit

If two adjacent digits in a binary number have the same content, the content of the left digit is twice the content of the right digit.

Binary representation of non-negative numbers in accordance with the above rules, the binary number plus one when the number for the beginning of the 0 position of a number of consecutive 1 to 0

Turning the rightmost 0 into 1, any number divided by 2 and retaining the integer portion of the result is equivalent to removing the content of the rightmost binary digit.

When converting a non-negative number from binary to decimal, you only need to convert each digit individually and then sum the results.

`0000 0011 = 2 to the 1st power + 2 to the 0th power = 2 + 1 = 3'.

To convert a non-negative number from decimal to binary, keep dividing the original number by 2 and retaining the integer part

Get a set of numbers, divide each number by 2 to get the content of a digit, write the content of all digits in reverse order from back to front to get the conversion result.

Negative numbers between binary and decimal can not be directly converted, you must use the opposite number, the conversion process is divided into three steps, the first calculation of the opposite number.

Then the opposite number is converted and finally the opposite number is calculated again.

The content of each digit of the binary number into the opposite number and then add one to get the opposite number of the binary. For example:

`-14

14

0000 1110

1111 0001

1111 0010 (binary of -14)
`

The leftmost digit in a signed type of binary number is called the sign bit, and his contents can be used to determine whether the number is negative or non-negative.

A sign bit with a 0 indicates a non-negative number, and a sign bit with a 1 indicates that the number is negative. For example:

`1100 1011

0011 0100+1=0011 0101=-53`.

Divide the digits of a binary number into groups of three from right to left, replacing each group with a number between 0 and 7.

The result is called the octal representation of the number. For example:

`0100 1010 01 101 010 1 5 2 = 152 (octal).'

It is possible to use the octal representation of numbers directly in a program; such numbers must begin with 0

The octal representation of a number can be displayed on the screen by using %o as a placeholder in the printf function call statement.

Divide the digits of a binary number into groups of four digits from right to left, replacing each group with a character (replace the digits from 10 to 15 with letters from a to f).

The result of this substitution is called the hexadecimal representation of the number

`1100 1011 cb (hexadecimal)
`

It is possible to represent numbers in a program using hexadecimal representation, which must begin with 0x

The hexadecimal representation of a number can be displayed on the screen by using %x or %X as a placeholder in the printf function call statement.

All letters in the number are lowercase when %x is used as a placeholder, and all letters in the number are uppercase when %X is used as a placeholder.

Operators represent rules for calculating numbers, and can be categorized according to the number of digits they need to match.

Monocular operators, binocular operators and trinocular operators.

The four operations of addition, subtraction, multiplication and division are represented in C by +, -, * and /.

If the two numbers involved in the division calculation are integers; then the result of the calculation retains only the integer part of the result, the C language uses % to represent the remainder operation.

     The assignment operator is represented by =. This operator records a number into a memory area.

     The assignment statement can be used as a number, the number is the number in the left memory after the copy is complete

     It is possible to use more than one assignment operator in a single statement, in which case the assignment operator to the **right** is counted first

     Most binaries can be combined with assignment operators to form compound assignment operators, e.g. +=, /=, etc.

     Compound assignment operators also require that the left-hand side represent the memory area and the right-hand side represent the number.

     The compound assignment operator records the result of the binomial operator into the left memory.

     The compound assignment operator has the same low priority as the assignment operator.

The increment operator (++) and the decrement operator (--) are both unary operators, **both of which must be used in conjunction with a storage area**.

    They can add or subtract the contents of a storage area by one and they both have two ways of using them.

    One is a pre operation (the operator is written in front of the storage area); the other is a post operation (the operator is written behind the storage area)

    Expressions written using these two operators can be used as numbers; the pre operation when used as a number is

    When the first operation is used as a number, it is the modified number, and when the second operation is used as a number, it is the modified number.

    Do not increment or decrement the same variable more than once in a single statement.

Logical operators are used to write logical expressions, the result of which can only be a Boolean value.

The ! is a unary logical operator that calculates the opposite of a Boolean value, and should be written before a Boolean value.

Bimanual logical operators include == (equal to),! = (not equal to), < (less than), > (greater than) > = (greater than or equal to) and <= (less than or equal to)

Expressions that contain at most one binomial logical operator are called simple logical expressions, the

The result of such a logical expression in math must be the same as the result in C.

    When writing a logical expression in C, if the logical expression contains more than one binomial logical operator, the result must be the same.

    must be split into several simple logical expressions and then merged.

    You can use && and or (||) to combine two logical expressions into one if one of the two logical expressions is true.

    If one of the two logical expressions results in true, the result is true when concatenated with or (||), and if one of the two expressions results in false, the result is false when concatenated with or (&&).

    Both with (&&) and or (||) have the short-circuit feature

   (If the result of the first logical expression determines the result of the combined logical expression, the latter logical expression can be ignored)

Exercise:
A man is considered overweight if his height minus weight is less than 105.
A woman is overweight if her height minus weight is less than 110.
gender (0 for women, 1 for men)
height height
weight
Write a logical expression to get the result true when overweight.

```c
(gender &&(height-weight<105))||
(!gender &&(height-weight<110) )

Exercise:

Write a program to get a number from the keyboard, determine whether the year is a leap year or not and display this result on the screen

A leap year is a leap year if the number of the year is divisible by 4 but not by 100.

A year is also a leap year if it is divisible by 400.

/*
 *
 * Write a program to get a year number from the keyboard, determine if the year
 * is a leap year or not and display the result on the screen.
 *If the year is divisible by 4 but not by 100, it is a leap year.
 * If the year number is divisible by 400, it is also a leap year.
 *
 * */
#include<>
int main(){
int year=0; int main(){
printf("Please enter a year:\n");
scanf("%d",&year);
int tmp=0;
tmp=(! (year%4) && year%100) || ! (year%400); int tmp=0; tmp=(!
printf("The result of the judgment is %d\n",tmp);
return 0; }
}