Dark Horse Programmer-the basic arithmetic of C language and the attention point of Process Control learning

Source: Internet
Author: User
Tags case statement

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Before learning the basic algorithm and process control, we first supplement the variable memory analysis and some usage notes of scanf function in learning the basic grammar of C language.

One, variable memory analysis: Different data types occupy the byte is not the same, the larger the data, the more bytes occupied, when the system allocates memory, memory addressing is from large to small, in the system to verify the output memory address of the C language statement is: printf ("Variable memory address is:%p", & Variable name). Where & is an address operator used to get the address of the variable,%p is used to output the address.

The scanf function: The scanf function is relative to the printf function, and the printf function outputs some data to the computer screen, while the SCANF function requires the user to enter data, and when the program executes to the SCANF function, it is temporarily stopped, requiring the user to enter information to press ENTER before continuing execution.

The scanf function accepts only the address of the variable, so scanf ("%d", & variable name) when using the scanf function. Note: The scanf function cannot have a newline character \ n. When you enter multiple values at once, the code is separated by some symbols, and the same symbol is entered as the user enters.

Basic arithmetic of C language

The basic operation of C language mainly includes arithmetic operation, assignment operation, self-increment and decrement operation, relational operation, logic operation and three mesh operation, and so on, let's exchange some points which I think are easy to confuse and pay attention to.

1, arithmetic operations: The main include is add, subtract, multiply, divide and take the rest of the operation, which need to pay attention to is to take the rest of the operation is also known as modulo operation , on both sides of the% is a whole number, and% of the results of the surplus is positive or negative only with% of the value of the left. (% left is positive, whereas negative on left side results in negative values)

2, assignment operation: This does not have much to say, relatively simple, attention and = = distinguish good.

3. Self-increment self-reduction operation: realize the same and different a=a+1,a+=1,a++,++a. Note that the ++a is used after a self-increment, while the a++ is first used and then self-increasing. sizeof is the number of bytes that calculate the amount of memory it occupies

4, relational operation: The true and false problem of C language, in C language any non-0 value is true (regardless of positive negative number), only 0 is false. Relational operations include comparison size, equal to or not equal, and so on.

5. Logical operation: Logic and &&, logical or | |, logical NON! Condition, where the logic is not the condition is returned 0, if the condition is not true returns 1.

6, trinocular operation: Three mesh operator: condition? Value 1: Value 2 When the condition is established returns the value 1, the return value 2 is not established

Second, the selection structure in the Process control

The C language selection structure mainly includes the IF statement and the switch statement, in the learning if statement there is a scope problem is to be noted, the following code to describe:

1#include <stdio.h>2  3 intMain ()4 {5      intA =Ten;6      if(a>1)//if the execution of the IF statement is not enlarged parentheses, the condition after the execution of the next statement, but regardless of whether the condition is true, the system will print B, the statement of the output B is not in the conditional statement7printf"a\n");//This writing is not very readable, but there is no problem when the program runs compile, it is a part of the IF structure8printf"b\n");9      return 0;Ten}

Another example is a bit similar to the previous example, but the scope of the problem is covered here:

1#include <stdio.h>2 intMain ()3 {4      if(Ten>6)5         intA =5;//this will cause an error because the scope of a is not clear, and if you need to define a, you need to add braces. 6printf"%d\n", a);//unable to output a7      return 0;8}

To summarize where the IF statement is noted, if you want to define a new variable in the statement following the IF statement, you must use braces, which is also to ensure the readability and specification of the code.

The same problem can occur in a switch statement, where a scope problem is involved in a case statement, with braces, for example:

1#include <stdio.h>2 intMain ()3 {4      CharC='+';5      intA =Ten;6      intb = -;7      Switch(c) {8            Case '+':9              intSum=a+b;//This cannot be defined here, because the scope of sum is ambiguous, the system cannot distinguish between a case or an entire program, so if you want to define it, you need to add braces. Tenprintf"and is%d\n", sum); One               Break; A            Case '-': -              intMinus=a-b;//here is the same, will be error.  -printf"the difference is%d\n .", minus); the               Break; -      } -      return 0; -}

To summarize, if the switch statement is to define a new variable after the case, it must be wrapped with curly braces {}, and its defined variables are defined only in the code block in which they are located, and then end when executed.

Dark Horse Programmer-the basic arithmetic of C language and the attention point of Process Control learning

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.