C Written Examination records

Source: Internet
Author: User
Tags pack

Write down questions that you don't understand in your exams:

1, c reentrant function

Reentrant functions are primarily used in multitasking environments, where a reentrant function is simply a function that can be interrupted , that is, it can be interrupted at any point in the execution of the function, and transferred to the OS to execute another piece of code, and return control without error Instead of reentrant functions, because of the use of some system resources, such as global variable area, interrupt vector table, etc., if it is interrupted, there may be problems, such functions can not run in a multitasking environment.

2, the difference between strlen and sizeof:

Although it is common sense, but I often forget which is which, strlen will not count the "" "occupied by a byte, sizeof will

3, the difference between the array and the linked list

Arrays: Data order storage, fixed size

Linked list: Data can be stored randomly, the size can be changed dynamically

4, Tpyedef and define

typedef (int*) pint;
and the following line:
#define PINT2 int*

Effect is the same. actually different.

See the difference in practice:

Pint a,b effect with int *a; int *b; indicates that two integer pointer variables are defined.

The effect of pINT2 a,b is the same as int *a, B; The expression defines an integer pointer variable A and integral variable B.


5,

Main ()
{
int a[5] = {1,2,3,4,5};
int *ptr = (int*) (&a+1);
printf ("%d%d", * (a+1), * (ptr-1));
}
The output of this program is: 2 5


&a+1 does not represent A's address (set to 0x0010) plus 1 and becomes 0x0011. Since a is an array of 5 int types, the "+1" in "&a+1" is represented as a space (or offset) equivalent to "1" of the size, at which point &a+1 represents a[5]. PTR is a[5 because &a+1 represents a[5].

and PTR as int type of pointer, so "ptr-1" will subtract "1" int type pointer space, at this time is a[5-1]=a[4].

6, comma-expression

The value of the comma expression is the value of the last element

Main ()
{
int A, b,c, D;
A=3;
b=5;
C=a,b;
D= (A,B);
printf ("c=%d", c);
printf ("d=%d", D);
}
The output of this program is: c=3d=5 Note the parentheses are all good expressions

7. Finding the wrong

void GetMemory (char *p)
{
p = (char *) malloc (100);
}
void Test (void)
{
char *str = NULL;
GetMemory (str);
strcpy (str, "Hello World");
printf (str);
}

The parameter of the incoming getmemory (char *p) function is a string pointer, and modifying the parameter inside the function does not really alter the value of the passed-in parameter, and finishes char *str = NULL;
GetMemory (str);
After the STR is still null

(You can change the pointer to a double pointer and then alter the contents of the cursor char **p= (char*) malloc (100)) and remember to release the pointer.

Ii

Char *getmemory (void)
{
Char p[] = "Hello World";
return p;
}

void Test (void)
{
char *str = NULL;
str = GetMemory ();
printf (str);
}


Char p[] = "Hello World";
return p; The p[] array in is a local automatic variable within a function, and after the function returns, the memory has been freed and there is no idea where the getmemory () is pointing. (p plus static, it's OK)


8, respectively gives the bool,int,float, the pointer variable and "0 value" comparison of the IF statement (assuming variable name is VAR)
Answer:
BOOL type variable: if (!var)
int type variable: if (var==0)
Float type variable:
const float Epsinon = 0.00001;
if ((x >=-Epsinon) && (x <= Epsinon)
Pointer variable: if (var==null)
Analysis:
Examination of the "internal strength" of the 0 value judgment, the 0 judgment of the bool variable can be written as if (var==0), and the int variable can also be written as if (!var), the judgment of the pointer variable can also be written as if (!var), although the procedure can run correctly, But the meaning of the procedure was not clearly expressed.
In general, if you want to let if judge a variable's "true", "false", you should use if (Var), if (!var) directly to indicate that it is "logical" judgment, if you use if to judge a numeric variable (short, int, long, etc.), should use if (var==0), It is a good programming habit to show that 0 is a "numerical" comparison, and that it is appropriate to use if (var==null) for the judgment pointer.
Floating-point variables are not accurate, so float variables cannot be used with "= =" or ". = "In contrast to numbers, it should be managed to be converted into" >= "or" <= "forms. If written as if (x = = 0.0), the sentence is wrong, 0 points.


9, void Func (char str[100])
{
sizeof (str) =?
}

Answer: 4, not 100 (if not as a formal parameter, then 100)
Func (char str[100]) function, the array name is used as a function parameter, in the function body, the array name loses its connotation, is only a pointer, loses its intension at the same time, it also loses its constant characteristic, may make the increment, the self reduction and so on the operation, may be modified. (the original array name is as a pointer constant and cannot be self reduced)


10,

least = MIN (*p++, b);
Answer: #define MIN (A,b) ((A) <= (B)? (A): (B))
MIN (*p++, b) can create side effects of macros
Analysis:
This question mainly examines the interviewer's use of the macro definition, a macro definition can implement functions similar to a function, but it is not a function at all, and the "arguments" in parentheses in a macro definition are not real arguments, and a one-to-one substitution is made for "parameters" when the macro is expanded.

Macro definition #define MIN (a,b) ((A) <= (B)? (A): (b) The result of the effect on min (*p++, b) is:
((*p++) <= (b)? (*p++): (b))
This expression will have side effects, and the pointer P will do more than once + + self-increase operation.


11, strcpy and memcpy are standard C library functions, they have the following characteristics.

①STRCPY provides a copy of the string. That is, strcpy is used only for string copying, and it also copies the end character of the string, not just the string content. The prototype of the strcpy function is: char* strcpy (char* dest, const char* SRC);

②MEMCPY provides a common memory copy. That is, memcpy has no limitations on what needs to be replicated, so it is more versatile.
void *memcpy (void *dest, const void *src, size_tcount);

12. char a,b,c,*d; a= ' \ '; b= ' \xbc '; d= ' \017 '; printf ("%c%c%c\n", a,b,c,*d) find out where the error occurred at compile time someone else's answer:

A must be wrong, a should be written: A= ' \ \ is the escape character
D is also wrong, the landlord said very to
D is an address, can not be so assigned
(I think D is right, notice is double quotes Ah, is to give that string of the first address constant to D.) )
B is a 16 binary number


13, Exchange two variables of the value, do not use the third variable. That is, a=3,b=5, after the exchange of a=5,b=3 (Maths problem ...). )

A = a + B;
b = a-b;
A = A-b;


14, C and C + + struct What is the difference?

(1) The default access type for properties in C + + classes is private, and struct in C + + has the default access type public
(2) C + + classes can have inheritance, virtual functions, polymorphism, and C + + struct not.
 
C language struct inside can not have function, can only have variable. C + + extends the struct function in C

14, the following description of the error is
A Binary files can be opened to read the end of the file, and the sequential file can not
B At the end of the program, you should close the open file with the function fclose ()
C When reading data from a binary file using function fread (), you can read data to all elements in the array with an array name
D You cannot define a file pointer to a binary file by using file

I chose D, I don't know, right?

15. Wild pointer

"Wild Pointer" is not a null pointer, it is a pointer to "junk" memory (not available memory). People generally do not use null pointers incorrectly, because it is easy to judge with an if statement. But "wild pointers" are dangerous, if you can't tell if a pointer is a normal pointer or a "wild pointer." Having a good programming habit is the only way to avoid "wild pointers."

There are three main causes of wild pointers: One, the pointer variable has not been initialized. Any pointer variable that has just been created does not automatically become a null pointer, and its default value is random, and it can be scrambled. Therefore, the pointer variable should be initialized at the time it is created, either by setting the pointer to null or by having it point to legitimate memory. Second, the pointer p is free or delete, no null, let a person mistakenly thought that p is a legitimate pointer. Despite the free and delete names (especially delete), they simply release the memory that the pointer refers to, but do not kill the pointer itself. The pointer now points to "junk" memory.       The freed pointer should immediately place the pointer to NULL to prevent the "wild pointer" from being generated. Third, the operation of the pointer beyond the scope of the variable. For example, do not return pointers or references to stack memory, because stack memory is released at the end of the function.
15,

The difference between ifdef and ifndef: ifdef: If the identifier is defined, execute the program segment 1, or else execute the other program segment ifndef: In contrast to ifdef, if the identifier is not defined, execute program segment 1, otherwise the other program segment 2
Avoid header file duplication (ifndef and ifdef don't make mistakes)

Each time you write a header file, add:
#ifndef _xxx_h_
#define _XXX_H_
#endif

16, the Static keyword variable, the global object is not initialized, 0

17,

What is byte alignment and why is it aligned?
The memory space in modern computers is divided by byte, theoretically, it seems that access to any type of variable can begin at any address, but the reality is that access to a particular type of variable is often at a specific memory address, which requires that various types of data be arranged in space according to certain rules, Instead of sequencing one after another, that's the alignment.
The function and reason of the alignment: each hardware platform has a great difference in the processing of storage space. Some platforms have access to certain types of data only from certain addresses. For example, some of the architecture of the CPU to access a variable does not have to be aligned, when the error occurs, then programming in this architecture must ensure byte alignment. Other platforms may not, but the most common is the loss of access efficiency if the data is not aligned according to its platform requirements. For example, some platforms start every time from the even address, if an int (assuming 32-bit system) if stored in the beginning of the even address, then a read cycle can read out the 32bit, and if the location of the beginning of the odd address, it will take 2 reading cycles, The 32bit data can be obtained by piecing together the high and low byte of the results of two readings. Obviously, the reading efficiency is much lower.
Two. The effect of byte alignment on the program:

Let's take a look at a few examples (32bit,x86 environment, GCC compiler):
The structure body is defined as follows:
struct A
{
int A;
Char b;
Short C;
};
struct B
{
Char b;
int A;
Short C;
};
The length of the various data types on the 32-bit machine is now known as follows:
Char:1 (Signed and unsigned)
Short:2 (Signed and unsigned)
Int:4 (Signed and unsigned)
Long:4 (Signed and unsigned)
Float:4 Double:8
What about the size of the top two structures?
The result:
sizeof (Strcut A) value is 8
The value of sizeof (struct B) is 12.

The structure body A contains a 4-byte length int, a 1-byte length char and a 2-byte length of the short data one, and B is the same; the a,b size should be 7 bytes.
The result of this is because the compiler wants to align data members in space. This is the result of aligning the compiler's default settings, so we can change the compiler's default alignment settings, of course. For example:


#pragma pack (2)//* Specify 2-byte alignment */
struct C
{
Char b;
int A;
Short C;
};
#pragma pack ()/* To cancel the specified alignment and restore the default alignment */
The value of sizeof (struct C) is 8.


The modified alignment value is 1:
#pragma pack (1)//* Specify 1-byte alignment */
struct D
{
Char b;
int A;
Short C;
};
#pragma pack ()/* To cancel the specified alignment and restore the default alignment */
The sizeof (struct D) value is 7.


To modify the code:

struct A {
int A;
Char b;
Short C;
};

struct B {
Char b;
int A;
Short C;
};

The output is 4, indicating that the preceding int affects alignment.


18, byte alignment (in the absence of #pragma pack macros, be sure to read the last line)

Alignment rules each compiler on a particular platform has its own default alignment factor (also called the Zimo number). Programmers can change this factor by precompiling the command #pragma pack (n), where n is the "n=1,2,4,8,16 factor" you want to specify. Rule ⒈ data member Alignment rules: struct (struct) (or union) data members, the first data member is placed at offset 0, after each data member's alignment according to the #pragma pack specified value and the data member itself long degree, than the smaller one. ⒉ The overall alignment rule of a struct (or union): After the data member has been aligned, the structure (or union) itself is aligned, and alignment is performed in the smaller of the value and structure (or union) of the maximum data member length specified by the #pragma pack. ⒊ binding 1, 2 can be inferred: when the n value of the #pragma pack equals or exceeds the length of all data members, the size of this n value will have no effect.

Finishing work : The total size of the structure, that is, the result of the sizeof,. Must be an integer multiple of the largest member within it. The insufficiency must be filled.


19. About Temporary variables

(1) Do not take a temporary variable to address the operation, because you do not know whether the compiler maps it to the Register

(2) Do not return the address of a temporary variable, or a temporary pointer variable, because it is the end of the function call will be recycled, do not know where to point

(3) Do not request too large a temporary array, because it is placed on the stack, the stack may not be large enough

20, implement strlen without local variables and global variables

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.