C Test Summary 1

Source: Internet
Author: User
#include "stdio.h"int main(){ char *str = "AAA"; printf("%s",str); str[0]='B'; printf("%s",str);return;}

What is the problem with this code?

STR points to a String constant. Str [0] = 'B' assigns a value to the String constant. Of course, an error occurs.

Question 2

int (*a[10])(int)

A function pointer array. Each Pointer Points to an int func (INT) type function.

Question 3

What is the difference between the struct of C and C ++?

Of course, because the struct of C cannot have member functions, C ++ can

Question 4

void main(){      char str[10];      printf("%d ",strlen(str));}

Unknown output result: the strlen and sizeof have a big difference. strlen must judge the string '\ 0', different

Question 5

#include "stdio.h"#include "string.h"typedef struct A{char a:4;char b:4;unsigned short i:8;unsigned long m;};typedef struct B{char a;char b;unsigned short i;unsigned long m;};typedef struct C{char a;char b;unsigned short i;char c;unsigned long m;};int main(){  printf("%d\n",sizeof(struct A));  printf("%d\n",sizeof(struct B));   printf("%d\n",sizeof(struct C));return;}

What is the output result?

8 because the maximum byte length of structure a is 4 and 4 bytes are aligned

8

12-byte alignment

Question 6

void wap(int *p1,int *p2){int *p;*p = *p1;*p1 = *p2;*p2 = *p;}

Error P: How can I assign a value when the field pointer has no space?

Question 7

A very non-mainstream question

To assign a value to the absolute address 0x100000, we can use

(Unsigned int *) 0x100000 = 1234

So what should I do if I want to let the program jump to the absolute address 0x100000 for execution?

Convert 0x100000 to function pointer (void (*) () 0x100000

Then call it * (void (*) () 0x100000

Question 8

Basic Questions

# Include <file. h> and # include "file. H"

What is the difference?

First, search for file. h from the standard library directory.

The second one looks for file. h In the user's working directory.

Question 9 what is the cause of stack overflow?

No resources recycled

Question 10

How to reference a defined global variable?

1 Reference header files

If the variable is written incorrectly, an error is reported during compilation.

2 Use extern

Write error. No error is reported during compilation, but an error is reported during connection.

Question 11

Write swap (x, y) in macro definition and exchange two numbers.

# Define swap (x, y) (x) = (x) + (y); (y) = (x)-(y); (x) = (X) -(y); or # define mswap (x, y) (x) = (x) + (y); (y) = (x)-(y ); (x) = (x)-(y)

Question 12

int main(){ int a,b;   a =3;   b= 5;   mswap(a,b);   printf("a %d b %d\n",a,b);return 1;}

What is the function of returning 1? The c Standard considers that the 0 table is returned successfully. The other 1 is an error.

Question 13

Preprocessing command # define declares a constant

# Define second (60*60*24*365) UL

Question 14

Why does C ++ call the function compiled by the C compiler extern "C"

Resolve function name matching

Question 15

unsigned char *a;unsigned long *b;a = (unsigned char *)0x801000;b = (unsigned long *)0x810000;

A + 5 =?

B + 5?

Good question: A + 5 is actually the address plus 5 characters in length, B + 5 plus 5 long integer lengths

0x801005

Zero X 810020

Question 16

int main(){ int a,b; int m[5]={1,2,3,4,5}; int *ptr = (int *)(&m+1); printf("m is %x \n",m); printf("m[0] is %x \n",&m[0]); printf("ptr is %x \n",&m); printf("ptr is ind %d %x \n",*(ptr-1),ptr);   a =3;   b= 5;   mswap(a,b);   printf("a %d b %d\n",a,b);return 1;}

Note (int *) (& M + 1 );

Actually moving an array Length

Question 17

# Define Sq (A) (a) * ())

Int A = 5;

Int B;

B = SQ (A ++ );

Pre-compilation will replace, maybe 25 May 36

========================================================== ==========================================

Insert a concept

A function prototype is a pre-declaration of a function before a function is used. The following describes how to use such a function. The function prototype is generally composed of the function name, parameter table, and return value type. The function prototype is not required. If your complete definition of this function appears before using this function, you do not need a function prototype. When a function such as char get (int I) is used, it is called like this: c = get (a); where A is a pre-defined int, and then when the function is executed, the value of a is the value of I in the get function. The return value in the get function is assigned to C in the main function.

1. in an expression, values of the char and short types are automatically converted to int or unsignedint, no matter whether they are signed or unsigned. (If the short size is the same as that of the int type, the range of unsigned short is greater than Int. In this case, unsignedshort is converted to unsigned INT ). Because they are converted to a type with a larger range, they are called "Upgrade )".

2. data types are classified in the order from high to low, namely Long Double, double, float, unsignedlong long, long, unsigned long, long, unsigned int, and Int. Here is a small exception. If long and INT are the same size, the unsigned int level should be above long. Char and short do not appear in this level list because they should have been upgraded to int or unsigned Int.

3. In any operation involving two data types, the lower-level types between them will be converted to higher-level types.

4. In the value assignment statement, before assigning the value on the right to the variable on the left, convert the Data Type of the value on the right to the type of the variable on the left. That is to say, what data type is the variable on the left, and what data type is the value on the right. This process may lead to a type upgrade for the value on the right, or a type downgrade (demotion ). The so-called "downgrade" refers to the conversion of a type with a higher level to a type with a lower level.

5. When passed as a parameter to a function, char and short will be converted to int, and float will be converted to double. The function prototype can be used to avoid this automatic upgrade.

========================================================== ==========================================

Question 18

Unsigned int A = 6;

Int B =-20;

(A + B> 6 )? Puts ("> 6"): puts ("<= 6 ");

The answer is:

If int is converted to unsigned, the signed bit will become useless. Therefore, the result is greater than 6.

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.