Exercise three--c Language pen questions

Source: Internet
Author: User

1. What is the output of the following program fragment?
#include <stdio.h>
#include <string.h>

int main (void)
{
Char str[]= "h\t\" \\/\012\00034 ";
printf ("%d", strlen (str));
}
Analysis: The main research is the use of escape characters and strlen library functions. \ t and \ "and \ \ are denoted by the \ symbol followed by an escape character, the three escape characters are the alarm," and \ Three symbols, and the use of the escape character \ddd or \xhh in a C program makes it easy and flexible to represent any character. The \DDD is followed by a three-bit octal number, and the value of the three-bit octal number is the corresponding octal ASCII value. The \x is followed by a two-digit hexadecimal number, which is the 16-in ASCII value of the corresponding character in the two-bit hexadecimal number. In the program of the problem, the \000 of STR is processed as the end of the string. So the valid string that STR represents consists of: H, \ t, ", \,/, the paper-taking control, altogether six valid characters.
Answer: 6

2. What is the output of the following code?
#include <stdio.h>
#include <string.h>

int main ()
{
int m=4,n=5;
float a= (float) m/n;
float b=m/n;

printf ("%.2f,%.2f", A, b);
return 0;
}
Analysis: The float cast is used as: (float) + variable or (float) + (expression), so the value of a is 4.0/5 and B has a value of 4/5.
Answer: 0.80, 0.00

3. What is the output of the following code?
#include <stdio.h>

int main ()
{
int x=3, y= (5, 4);

printf ("%d", x*=y+1);
return 0;
}
Analysis: The rule of both good operators: Take the last value. So the value of Y is 4. Rules for the reflexive operator: A variable is a reflexive operation of the value of the entire expression after it.
Answer: 15

4. What is the output of the following code?
#include <stdio.h>

int main ()
{
printf ("%d", (1<<2+3));
return 0;
}
Analysis: Investigate shift operations and basic priorities. The operator precedence of << is lower than the + operator, so the result is 1<<5.
Answer: 32

5. Use typedef to define an array type A with 10 integer pointers.
Analysis: The use of TypeDef is very flexible, very powerful, relevant knowledge points are difficult to understand, but some basic use of the method is relatively easy to grasp. See the relevant C language books in detail.
Answer: typedef int *a[];

6. What is the output of the following code?
#include <stdio.h>

int main ()
{
int a[3][3], (*B) [4];

b= (Int (*) [4]) A;
printf ("%d", (&a[1][1]-&b[1][1]));

return 0;
}
Analysis: B is a pointer to an array of 4 int elements, and A is forced to assign B. A[1][1] Take is which unit is clear, the main analysis b[1][1] take which unit, according to the syntax to understand, b[1] this kind of writing is equivalent to the B force upward to the pointer array (now B each cell is an array pointer, of course, this is a bit complicated now), so b[0] The value inside is actually the value of a,b[1] should refer to a unit, but this unit is an array of 4 int type, so b[1] The starting address is already b[0] value plus 4*sizeof (int) is also equal to a+4*sizeof (int), and then in the analysis b[1][1 ], this is relatively simple, is to offset a unit, so there is a total offset of 5 int units. However, A[1][1] has shifted the cells of 1*3+1=4 int, so the result of subtracting two addresses is 1 (pointer arithmetic).
Answer: 1.
By the way: because B[1][1] This forced ascension is a bit far-fetched, so the compiler can not check out the cross-border operation, that is, if we use b[5][6 in the program, the compiler will not error!

7. What is the output of the following code snippet?
#include <stdio.h>

int main ()
{
unsigned char a[16][16], *ptr;
int m;

ptr= (unsigned char *) A;
for (m=0; m < sizeof (a); m++, ptr++)
{
*ptr= m;
}
printf ("%x", a[1][10]);

return 0;
}
Analysis: Assigning values to each array unit by PTR pointers, A[1][10] is assigned a value of 15+11=26 to 16 for 26/16=1...10 that is 0x1a.
Answer: 1a

8. It is known that the output from the first of the following programs is 21, then the output in the next one is?
#include <stdio.h>

int main ()
{
unsigned int a[5][16], value = 0x87654321;
unsigned char *ptr;
int m;

ptr = (unsigned char *) &value;
printf ("%x\n", *ptr); Output is 21
ptr= (unsigned char *) (a);
for (m=0; m < sizeof (a); m++, ptr++)
{
*ptr= m;
}
printf ("%x\n", a[1][10]);

return 0;
}
Analysis: From the first printf output of 21 can be learned that the system is a small-end system, and then a[1][10] This unit is assigned four values for 16*4+10*4=104 start four integers, that is, 104 is assigned to A[1][10] the lowest 8bits,105 is assigned to a[1][ 10] The secondary low 8bits ....
Answer: 6b6a6968

9. What is the output of the following code?
#include <stdio.h>

int main ()
{
int a=1, b=10;

Do
{
A + +;
B-= A;
B--;
} while (b>0);
printf ("%d,%d", A, b);

return 0;
}
Answer: 4,-2

10.unsigned signed char can represent the maximum number defined by Uchar_max, then after the following program fragment is run, the result of the output is?
printf ("%d,%x", Uchar_max, Uchar_max)
Answer: 255, FF

11. What is the output of the following code?
#include <stdio.h>

int main ()
{
char *str = "Hello";
int * p= (int*) str;

printf ("%d,%d", sizeof (str[0]), sizeof (*P));

return 0;
}
Analysis: Str[0] represents a character cell, and *p represents an int cell.
Answer: 1, 4

12. Macro definition: The nth position of the integer variable A is cleared 0, the starting bit is the No. 0 position, and the other bits are unchanged.
Analysis: The basic macro definition of the study, remember that the macro definition is directly replaced, and if it is a macro with parameters, be sure to add parentheses to each parameter in the replacement section operator!
Answer: # define Clear_bit (A, N) (a) = (a) & (~ (1<< (n)))

13. What is the output of the following code?
#include <stdio.h>

int main ()
{
int m=0, n=0;

if (m++>2 | | m++<10)
n++;
Else
n--;
printf ("%d,%d", M, N);

return 0;
}
Analysis: The main study of the logical OR operator of the operation process, logic or | | Operation procedure for if the previous condition is true, then not the following conditions, if the previous condition is false then continue to judge (execute) The condition after judgment, so here m++>2 and m++<10 will be judged, that is, will be executed, so m++ executed two times.
Answer: 2, 1

14. When referencing library functions memcpy (), which header file do you want to include?
Answer: #include <string.h>

15. Run program written in C language
dir/b/tmp, DIR's C program
int main (int argc, char *argv[])
What is the value of ARGC?
Answer: 3, details refer to blog: http://blog.csdn.net/laoniu_c/article/details/39337981

16. Supplement the Atoi function body, the function is to convert a string to the full value.
#include <stdio.h>

/* Atoi:convert s to integer */
int atoi (char s[])
{
NT n = 0, i = 0;

while (' s[i '! =])
n = n*10 + s[i++]-' 0 ';

return n;
}

int main (void)
{
Char s[] = "12345";

printf ("%d\n", Atoi (s));
return 0;
}

17. Complete the following 17~20 as described below.
/ *
* DESCRIPTION
* The STRCHR () function locates the first occurrence of
* C (converted to a char) in the string pointed to S.
* The terminating null character is considered part of the string;
* Therefore if C is ' + ', the functions locate the terminating '.

* RETURN VALUES
* The Functions STRCHR () return a pointer to the located
* character, or NULL if the character does not appear in the string.
*/
#include <stdio.h>

char * (STRCHR) (const char *s, int c)
{
const char ch = (char) c;
const char *SC;

for (sc = 0;; ++s)
{
if (*s = = ch)
sc = s;
if (17)
Return ((char *) SC);
}
}

int main (void)
{
Char s[] = "Hello world.";
Char *p;

p = STRCHR (S, ' l ');
printf ("%p%p\n", p, &s[2]);

return 0;
}
Answer: ' ==*s ' | | 0!=sc
/ *
* DESCRIPTION
*the strstr () function locates the first occurrence of the
*null-terminated string S2 in the null-terminated string s.

* RETURN VALUES
*if S2 is a empty string, S1 is returned; If S2 occurs nowhere
*in S1, NULL is returned; Otherwise a pointer to the first
*character of the first occurrence of S2 is returned.
*/
#include <stdio.h>
#include <string.h>

char * (STRSTR) (const char *S1, const char *S2)
{
const char *SC1, *SC2;

if (18)
Return ((char *) s1);
for (; (S1 = STRCHR (S1, *s2))! = 0; 19)
{
for (SC1 = s1, sc2 = s2;;)
if (*++SC2 = = ' + ')
Return ((char *) s1);
else if (*++sc1! = *SC2)
20;
}
return (0);
}

int main (void)
{
Char s1[] = "Hello world.";
Char s2[] = "Llo";

printf ("%s\n", Strstr (S1, S2));

return 0;
}
Answer: 18:NULL==S1 | | ' ==s1[0 '
19:S1 + +
20:break

Exercise three--c Language pen questions

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.