TA: C language Problem Collection--scanf () some common problems; switch statement; The order of execution for the For loop

Source: Internet
Author: User

The "format control" in the scanf function should be followed by the variable address, not the variable name.

The accuracy cannot be specified when entering data.
scanf ("%7.2f", &a). It's not legal. However, scanf ("%3d%3d", &a,&b) can be written like this.

If there are other characters in the format control string other than the format description, the characters with the same characters should be entered at the corresponding location when the data is entered.
scanf ("%d,%d,%d", &a,&b,&c);
Input: 3,4,5
scanf ("%d:%d:%d", &a,&b,&c);
Input: 3:4:5
scanf ("%d*%d*%d", &a,&b,&c);
Input: 3*4*5

scanf ("%2d%*3d%3d", &a,&b);
Input: (space with a # sign)
12#345#67
Assign 12 to A,%*3d to read 3-bit integers without assigning them to any variables. Then read into the 2-bit integer 67 to assign to B. This means that the 2nd data "345" is skipped.
* Indicates that this entry is not assigned to the corresponding variable after it is read.
If there is a "*" appended specifier after%, the number of columns that it specifies is skipped.
However, * If after%, for example:
scanf ("%d*%d*%d", &a,&b,&c);
Input: 3*4*5
This time the * number will not skip a specified number of columns of data, at the time of input, in the corresponding position to enter the * number.

What is the difference between%LF and%f.
L for input of Long Integer data (available%ld,%lo,%lx,%lu) and double type data (with%LF or%le)
Double A;
scanf ("%lf", &a);//cannot use scanf ("%f", &a);
printf ("%lf", a);//can also be used with printf ("%f", a);
float B;
scanf ("%f", &a);//cannot use scanf ("%lf", &a);
printf ("%f", a);//can also be used with printf ("%lf", a);

return 0; What does that mean?
The return value of the function can be returned as needed, not necessarily 0, and return 0 can be written when a value is required and there is no requirement for the return value;

int i=123;unsigned n=456;double a=12.34567;
printf ("%4d\t%7.4f\n\t%lu\n", i,a,n);
Output: (Space with a # sign)
#123 # # # #12.3457
####### #456
' \ t ' is a horizontal tab, accounting for 8 bits.

If there is a definition: float x=1.5;int a=1,b=3,c=2; the correct switch statement is
A switch (x)
{Case 1.0:printf ("*\n");
Case 2.0:printf ("**\n");
}
B switch ((int) x);
{Case 1:printf ("*\n");
Case 2:printf ("**\n");
}
C switch (a+b)
{Case 1:printf ("*\n");
Case 2+1:printf ("**\n");
}
D switch (a+b)
{Case 1:printf ("*\n");
Case c:printf ("**\n");
}
The expression after switch must be an integer or an enumeration type.
The expression after the case must be an integer constant expression.
The expression following the case must be a constant value.
A semicolon is not required after the switch expression.
Choose c for the answer.

The ASCII of the known letter A is 65, and the output after the program runs is:
void Main ()
{
Char A, B;
A= ' A ' + ' 5 ' + ' 3 '; b=a+ ' 6 '-' 2 ';
printf ("%d,%c\n", A, b);}
' A ', ' 5 ', ' 3 ', ' 6 ', ' 2 ' are character constants. Converted to ASCII code values for calculation.
A= ' A ' + ' 5 ' + ' 3 ' =65+53-51=67
b=a+ ' 6 '-' 2 ' =67+54-50=71; (ASCII 71 character is g)
Output:
67,g
On the basis of the original topic, if there is: Char c;c=a+ ' 1 ';
Then c=a+ ' 1 ' =67+49=116 (ASCII 116 characters are T)

int *p;
p=null;p=0;p= ' + '; The three statements have the same meaning and all represent P as 0. It is worth noting that the value of P is null and the non-p assignment is two different concepts. The former is a value (value 0), does not point to any variable, the latter does not assign a value to p but not equal to P no value, but its value is an unpredictable value, that is, p may point to a unit that has not been specified beforehand.

Order of execution for a For loop
The order of execution for the For loop is expressed in the following expressions:
for (EXP1;EXP2;EXP3)
{EXP4;}
The order of execution should be:
1) The first cycle, that is, the initialization cycle.
First executes the expression EXP1 (usually the initialization statement), then executes the EXP2 (which is usually the conditional judgment statement), determines whether the EXP1 conforms to the EXP2 condition, and if so, executes EXP4, otherwise, stops execution;
2) nth (n>=2) cycles.
First execute the EXP3, then determine whether the EXP3 meets the requirements of EXP2, and if so, continue execution at EXP4, otherwise, stop execution.
Summarize:
In general, the order of execution is consistent. First the condition is judged (EXP2), then the function body executes (EXP4), and the last for execution (EXP3). Reciprocating. The difference is that the condition is judged by the object. At the first judgment, the object is an initialization statement (EXP1), and the subsequent judgment object is the result of the execution (EXP3).

printf (); Statement compilation is right-to-left. The output is then left to right.

TA: C language Problem Collection--scanf () some common problems; switch statement; The order of execution for the For loop

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.