C language test questions (1)

Source: Internet
Author: User
1,
# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Char A [30];
Char * B = (char *) malloc (20 * sizeof (char ));
Printf ("% d/N", sizeof ());
Printf ("% d/N", sizeof (B ));
Printf ("% d/N", sizeof (A [3]);
Printf ("% d/N", sizeof (B + 3 ));
Printf ("% d/N", sizeof (* (B + 4 )));/
Return 0;
}

Answer: 30 4 1 4 1
2. Differences between variable definition and Declaration

In program design, we always use the definition of variables and the declaration of variables. Sometimes we are not very clear about this concept and know how to use it, but I don't know how it will take for a while. Next I will briefly introduce their differences as follows: (I hope my guidance will benefit you)

Variable declarations include:Two cases:

One typeYou need to create a bucket. For example:IntA bucket has been created during the declaration.

AnotherYes, you do not need to create a bucket. For example:Extern intVariable A is defined in other files.

FormerYes"Defining Declaration)"Or" Definition ", whileThe latterYes"Referncing Declaration )".

In a broad sense, declarations contain definitions, but not all declarations are definitions. For example:IntIt is both a declaration and a definition. However,Extern intIt is just a declaration, not a definition. In general, we often describe the definition of a bucket, rather than the definition of a bucket ". Obviously, we mean that the lifecycle here is narrow, that is, non-defined declarations, for example, in the main function

 int main() 
{ 
Extern int A; // This is a declaration, not a definition. Declare a as an external variable that has already been defined.
          //do something 
}
// Note: When declaring an external variable, you can remove the variable type, for example, extern A; dosomething ();
 
Int; // Is defined, defines an external variable whose A is an integer.

The "Definition" of an external variable is different from the "Declaration" of an external variable.The external variable can only be defined once. It is located outside all functions, and the external variable declaration in the same file can be multiple times, it can be defined within the function (which function should be used in that function), or outside the function (before the definition point of the external variable ). The system allocates storage space according to the definition of external variables (rather than the declaration of external variables. For external variables, initialization can only be performed in the "Definition", rather than in the "Declaration. The so-called "Declaration ",Its roleIs to declare this variable is an external variable that has been defined later. It is just a "Declaration" to "reference this variable in advance.ExternDeclaration only, without any definition.

UseStaticTo declare a variableFunctionThere are two:

(1) If a local variable is declared as static, the space allocated for the variable will always exist throughout the execution period of the program.

(2) If the external variable is declared as static, the function of this variable is limited to the module of this file (thanks

Legion8169

)

3. Differences between strcpy and memcpy:

What are the differences between strcpy (strncpy) and memcpy in the copy process?

Strcpy is a copy of the string. When the character '/0' is encountered during the copy, the copy is stopped (that is, the byte is 0x00, of course, the character '0' is not '/0 '). However, memcpy ignores the value of the copied data and copies the specified amount of data.

Take a look at the example below

Int main (){
Int I;
Char set1 [20], set2 [20];
Bzero (set1, 20 );
Bzero (set2, 20 );
Strcpy (set1, "Hello world! ");
Set1 [8] = 0;
Strcpy (set2, set1 );
Printf ("% s/n", set2 );
For (I = 0; I <20; I ++ ){
Printf ("% C", set2 [I]);
}
Printf ("/N ");
Memcpy (set2, set1, 20 );
Printf ("% s/n", set2 );
For (I = 0; I <20; I ++ ){
Printf ("% C", set2 [I]);
}
Printf ("/N ");

}

Put "Hello World!" in the array set1 first! ", Then set [8] to the string end sign '/0'

Let's take a look at the results after the strcpy and memcpy copies respectively.

The program running result is as follows:

Hello wo

Hello wo

Hello wo

Hello wold!

It can be seen that strcpy only copies the characters Hello wo.

Memcpy copies all characters in set1.

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.