C语言基础题--含答案

1. .struct tagAAA                                                                                  

{                                                                                           
    unsigned char ucId:1;
    unsigned char ucPara0:2;
    unsigned char ucState:6;
    unsigned char ucTail:4;
    unsigned char ucAvail;
    unsigned char ucTail2:4;
    unsigned long ulData;
}AAA_S; 
问:AAA_S在字节对齐分别为1、4的情况下,占用的空间大小是多少?
答案:9 12


2.typedef struct tagTest
{
      UCHAR ucFlag;
      ULONG ulLen;     
}TEST_S;

TEST_S test[10];

四字节对齐方式时: sizeof(TEST_S) = ______, sizeof(test)________.
答案:8   80



char acHello[] = "hello\0world";
char acNew[15] = {0};
strcpy(acNew,acHello);
strlen(acNew) = _____
sizeof(acHello) = ______
答案:5   12


4.#pragma pack(4)/*编译选项,表示4字节对齐*/
int main(int argc, char* argv[])
{
    struct tagTest1
    {
        short a;
        char d; 
        long b;    
        long c;    
    };
    struct tagTest2
    {
        long b;    
        short c;
        char d;
        long a;    
    };
    struct tagTest3
    {
        short c;
        long b;
        char d;     
        long a;    
    };
    struct tagTest1 stT1;
    struct tagTest2 stT2;
    struct tagTest3 stT3;
    
    printf("%d %d %d", sizeof(stT1), sizeof(stT2), sizeof(stT3));
    return 0;
}
#pragma pack()(编译选项结束)
请问输出结果是:_________
答案:12   12   16


5. enum ENUM_A
{
       X1,
       Y1,
       Z1 = 5,
       A1,
       B1 
};
enum ENUM_A enumA = Y1;
enum ENUM_A enumB = B1;
请问 enumA = ____; enumB = ______;
答案:1   7


6.以下程序的输出结果是________.
#include     <stdio.h>
int fun(int   x,int y)

static int   m = 0;8
static int   i = 2;3
i += m + 1;12
m = i + x + y;
return m;
}
void main()
{
int   j = 4;
int   m = 1;
int   k;
k = fun(j, m);
printf("%d,", k);
k=fun(j, m);
printf("%d\n", k);
return;
}
答案:8   17


7.以下程序的输出结果为________
#define CIR(r) r*r /*请注意这种定义的缺陷,不允许这么定义*/
void main()
{
    int a = 1;
    int b = 2;
    int t;
    t = CIR(a + b);
    printf("%d\n", t);
    return;
}
答案:5


8.在VRP中,实现了strncpy类似的函数,定义如下:
#define CHAR char
#define ULONG unsigned long
#define VOID void

#define MACRO_COPYWORLDLENGTH 4

CHAR *VOS_strncpy(CHAR *pcDest, const CHAR *szSrc, ULONG ulLength)
{
    CHAR *pcPoint = pcDest;
    
    if(( NULL == szSrc ) || ( NULL == pcDest ) ))
    {
        return NULL;
    }
    
    while(ulLength && (*pcPoint = *szSrc))/*这里采用了在判断语句中赋值的方式(*pcPoint = *szSrc),建议尽量不使用*/
    {
        pcPoint++;
        szSrc++;
        ulLength--;
    }
    if(!ulLength)
    {
        *pcPoint = '\0';
    }
    return pcDest;
}

VOID main(VOID)
{
    CHAR szStrBuf[ ] = "1234567890";
    CHAR szStrBuf1[ ] = "1234567890";
    CHAR *szHelloWorld = "Hello World!";
    strncpy(szStrBuf, szHelloWorld, MACRO_COPYWORLDLENGTH);
    VOS_strncpy(szStrBuf1, szHelloWorld, MACRO_COPYWORLDLENGTH);
    printf("%s %s", szStrBuf, szStrBuf1);
}
程序的输出结果为________
答案:Hell567890 Hell

9.
char acHello[] = "hello\0world";
char acNew[15] = {0};
memcpy(acNew,acHello,12);
strlen(acNew) = _____
sizeof(acHello) = _____
答案:5 12


10. typedef struct Head 
   { 
       UCHAR aucSrc[6]; 
       ULONG ulType; 
   } HEAD_S;

在强制一字节对齐情况下,请指出sizeof(HEAD_S) = ________;
在强制二字节对齐情况下,请指出sizeof(HEAD_S) = ________;
在强制四字节对齐情况下,请指出sizeof(HEAD_S) = ________;
答案:10 10 12


11.union tagAAAA
{
    struct
    {
        char ucFirst;
        short usSecond;
        char ucThird;
    }half;
    long lI;
}number;

struct tagBBBBB
{
    char ucFirst;
    short usSecond;
    char ucThird;
    short usForth;
}half;
                                                                                                
struct tagCCCC
{
    struct
    {
        char ucFirst;
        short usSecond;
        char ucThird;
    }half;
    long lI;
};
                                                                                                    
在字节对齐为1下,sizeof(union tagAAAA)、sizeof(struct tagBBBBB)、sizeof(struct tagCCCC)是____ ____ _____
在字节对齐为4下,sizeof(union tagAAAA)、sizeof(struct tagBBBBB)、sizeof(struct tagCCCC)是____ ____ _____
答案:4   6   8
           8   8   12


12.struct tagABC 
{                                                                                     
    char cB;
    short sC;
    char cD;
    long lA;
}*pAbc;                                                                                    

pAbc = 0x100000;                                                                            
那么pAbc+0x100 = 0x_________; (ULONG)pAbc + 0x100 = 0x_________;(ULONG *)pAbc + 0x100 = 0x_________;(char *)pAbc + 0x100 = 0x_______;
答案:100C00 100100 100400 100100

13.unsigned long FUNC_C ( unsigned long ulAction )
{
    unsigned long ulResult = 0 ;
    
    switch ( ulAction )
    {
        case ACTION_A:
        {
            ulResult += 1 ;
            break ;
        }
        case ACTION_B:
        {
            ulResult += 1 ;
        }
        default:
        {
            ulResult += 1 ;
        }
    }

    printf( "ulResult = %u", ulResult ) ;

    return ulResult ;
}
当输入为ACTION_B时,输出结果为: ulResult = _________;
答案:2(因为此分支没有break分支)


14.下面的代码中,函数Test执行完毕后,打印的结果是 _____。
unsigned long g_ulGlobal = 0;
void GlobalInit(unsigned long ulArg)
{
    ulArg = 0x01;

    return;
}

void Test()
{
    GlobalInit(g_ulGlobal);
    printf("%lu", g_ulGlobal);
    return;
}
答案:0


15.以下程序的输出的结果是___________
int x = 3;
void incre();
void main()
{   int i;
    for (i = 1; i < x; i++) 
    {
     incre();
    }
    return;
}
void incre()
{   
    static   int x = 1;
    x *= (x + 1);
    printf("%d ",x);
    return;
}
答案:2   6


16.以下程序的输出的结果是___________
#pragma pack(4)/*四字节对齐*/
int main(int argc, char* argv[])
{
    unsigned char puc[4];
    struct tagPIM
    {                                                                                                   
        unsigned char ucPim1;
        unsigned char ucData0:1;
        unsigned char ucData1:2;
        unsigned char ucData2:3;
    }*pstPimData;

    pstPimData = (struct tagPIM *)puc;

    memset(puc, 0, 4);
    pstPimData->ucPim1 = 1;
    pstPimData->ucData0 = 2;
    pstPimData->ucData1 = 3;
    pstPimData->ucData2 = 4;

    printf("%02X %02X %02X %02X\n", puc[0], puc[1], puc[2], puc[3]);
    return 0;
}
#pragma pack()/*恢复缺省对齐方式*/
答案:01   26   00   00


17.
char *pcColor = "blue1" ;
char acColor[] = "blue1" ;
strlen(pcColor) = _____
strlen(acColor) = _____
sizeof(pcColor) = _____
sizeof(acColor) = _____
答案:5   5   4   6


18.
char str[] = "\\\0";
char *p = str;
int  n = 1000;
请计算
sizeof (str ) = ____________  
sizeof ( p ) = ______________   
sizeof ( n ) = ______________
答案:3   4   4


19.UCHAR *pucCharArray[10][10]; 
typedef union unRec
{
      ULONG ulIndex; 
      USHORT usLevel[6]; 
      UCHAR   ucPos; 
}REC_S;
REC_S   stMax,*pstMax;

四字节对齐方式时: sizeof(pucCharArray) = __指针的数组,每个指针的地址都是4字节____, sizeof(stMax)=_______, sizeof(pstMax)=__地址______,sizeof(*pstMax)=________. 
答案:400   12   4   12

20.typedef union unHead

    UCHAR aucSrc [6];
    struct tagContent
    {
        UCHAR ucFlag[3];
        ULONG ulNext;
    }Content;
}HEAD_S;
32CPU,VC编译环境下: 
在强制一字节对齐情况下,请指出sizeof(HEAD_S) = ________;
在强制二字节对齐情况下,请指出sizeof(HEAD_S) = ________;
在强制四字节对齐情况下,请指出sizeof(HEAD_S) = ________;
答案:7   8   8

21.
UCHAR *pszTest = "hello";                                                                       
UCHAR aucTest[] = "hello";
请问 sizeof(pszTest) = _____ , sizeof(*pszTest) = ______, sizeof(aucTest) = ______.     
答案:4   1    6     

22. struct BBB
    {
       long     lNum;
       char     *pcName;
       short    sDate;
       char     cHa[2];
       short    sBa[6];      
    }*p;
    p = 0x100000;
    p + 0x1 = 0x____
    (unsigned long)p + 0x1 = 0x______
    (unsigned long *)p + 0x1 = 0x______
    (char *)p + 0x1 = 0x______
答案:100018 100001 100004 100001

23.在4字节对齐的情况:
typedef struct tagRec
{
    long lA1;        
    char cA2; 
    char cA3;
    long lA4;
    long lA5;
} REC_S;

void main(int argc, char *argv[])
{
   REC_S stMax ;      
   printf("\r\n sizeof(stMax)= %d",sizeof(stMax));
   return;
}
输出结果为:
sizeof(stMax)=____
答案:16

24.void main ()
{
    unsigned long ulA = 0x11000000;
    printf("\r\n%x",*(unsigned char *)&ulA);
    return;
}
输出结果为:
答案:0

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值