版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、.Basic c question.1.Question:where in memory the variables are stored?Local variables, global variables, static.AnswerLocal variables sit in Stack.Global and static goto Data segment.Dynamic memory comes from Heap.2.Questioncan you explian the meaning for the follwoing programchar *c1, *c2, *c3, *c4
2、, *c5 ;char analysis8 = 'a', 'n', 'a', 'l', 'y', 's', 'i' ,'s'int main()c5 = c4 = analysis;+c4;c3 = &analysis6;c2 = c5 + 2 ;c1 = &analysis7 - 3 ;printf ("%ct%ct%ct%ct%c", *c1,*c2,*c3,*c4,*c5);return 0;Answerc1, c2, c3,
3、 c4 and c5 are pointers (which can hold addresses). analysis is an array variable which is holding the string "analysis". >c5 = c4 = analysis;The starting address of the array is given to c5 and c4. Hence they point to first character 'a' in the array.>+c4;c4 is incremented b
4、y 1. Hence points to 'n' in the array.>c3 = &analysis6;c3 is given the address of 7th character (count from 0) of the array. Hence c3 points to character 'i'. >c2 = c5 + 2 ;.c5 points to first character. plus 2 means, c2 points to 3rd character 'a' (second 'a
5、9; in the string).>c1 = &analysis7 - 3 ;c1 points to 3rd character from the end (not counting the last character).c1 holds the address. *c1 means the data strored at c1. Since c1 points to 3rd character from the end (that is 5th character - count starts from 0), *c holds character 'y'
6、. Hence *c1 will print 'y' on the screen.Similarly for other pointer variables *c2, *c3, *c4 and *c53.Question:a=5 b=10 c=7(a>c)?a:(b>c)?b:c)Answer: 104Question : How do you declare an array of N pointers to functions returning pointers to functions returning pointers to characters?A.
7、char *(*(*aN)()();B. Build the declaration up incrementally, using typedefs:C. Use the cdecl program, which turns English into C and vice versa:D. All of the above.Answer : D5Question :void main()int count=10,*temp,sum=0;temp=&count;*temp=20;temp=∑*temp=count;printf("%d %d %d "
8、,count,*temp,sum);Answer : 20; 20; 20;.6Question :void main()int i=7;printf("%d",i+*i+);Answer : 49.Note: Don t change a variable twice in one expression.7Question : The number of syntax errors in the program?int f()void main()f(1);f(1,2);f(1,2,3);f(int i,int j,int k)printf("%d %d %d&
9、quot;,i,j,k);Answer : None.8Question : void main()float j;j=1000*1000;printf("%f",j);A. 1000000B. OverflowC. ErrorD. None of the aboveAnswer :D.9Question : Give the output of the programvoid main()unsigned i=1; /* unsigned char k= -1 => k=255; */signed j=-1; /* char k= -1 => k=65535
10、*/* unsigned or signed int k= -1 =>k=65535 */if(i<j)printf("less");elseif(i>j)printf("greater");elseif(i=j)printf("equal");Answer : less10Give the output of the programvoid main()char *s="12345sn"printf("%d",sizeof(s);Answer : 411Question :
11、Give the output of the programvoid main()int i;for(i=1;i<4,i+)switch(i)case 1: printf("%d",i);break;case 2:printf("%d",i);break;case 3:printf("%d",i);break;.switch(i) case 4:printf("%d",i);Answer : 1,2,3,412.Question : How to pass two arguments to a functio
12、n prompted to by function pointerA. g -> (1,2)B. *g(1,2)C. (*g)(1,2)D. g(1,2)Answer : C13Question: Can you have constant volatile variable?Answer:YES. We can have a const volatile variable.a volatile variable is a variable which can be changed by the extrenal events (like an interrput timers will
13、 increment the voltile varible. If you dont want you volatile varibale to be changed thendeclare them as“ const volatile”.14.Question:study the code:#include<stdio.h>void main()const int a=100;int *p;p=&a;(*p)+;printf("a=%dn(*p)=%dn",a,*p);What is printed?A)100,101B)100,100C)101,
14、101 D)None of the above.Answer CEmbedded C1. Using the #define statement, how would you declare a manifest constant that returns the number of seconds in a year? Disregard leap years in your answer.Answer#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)ULI'm looking for several things here:Basic kno
15、wledge of the #define syntax (for example, no semi-colon at the end, the need to parenthesize, and so on)An understanding that the pre-processor will evaluate constant expressions for you. Thus, it is clearer, and penalty-free, to spell out how you are calculating the number of seconds in a year, ra
16、ther than actually doing the calculation yourselfA realization that the expression will overflow an integer argument on a 16-bit machine-hence the need for the L, telling the compiler to treat the variable as a LongAs a bonus, if you modified the expression with a UL (indicating unsigned long), then
17、 you are off to a great start. And remember, first impressions count!2. Write the "standard" MIN macro-that is, a macro that takes two arguments and returns the smaller of the two arguments.Answer:#define MIN(A,B)(A)<= (B) ? (A) : (B)The purpose of this question is to test the following
18、:Basic knowledge of the #define directive as used in macros. This is important because until the inline operator becomes part of standard C, macros are the only portable way of generating inline code. Inline code is often necessary in embedded systems in order to achieve the required performance lev
19、elKnowledge of the ternary conditional operator. This operator exists in C because it allows the compiler to produce more optimal code than an if-then-else sequence. Given that performance is normally an issue in embedded systems, knowledge and use of this construct is important Understanding of the
20、 need to very carefully parenthesize arguments to macrosI also use this question to start a discussion on the side effects of macros, for example, what happens when you write code such as:.least = MIN(*p+, b);3. Infinite loops often arise in embedded systems. How does you code an infinite loop in C?
21、 There are several solutions to this question. My preferred solution is:Answer:while(1)?5. Using the variable a, give definitions for the following: a) An integerb) A pointer to an integerc) A pointer to a pointer to an integerd) An array of 10 integerse) An array of 10 pointers to integersf) A poin
22、ter to an array of 10 integersg) A pointer to a function that takes an integer as an argument and returns an integerh) An array of ten pointers to functions that take an integer argument and return an integerAnswers :a) int a; / An integerb) int *a; / A pointer to an integerc) int *a; / A pointer to
23、 a pointer to an integerd) int a10; / An array of 10 integerse) int *a10; / An array of 10 pointers to integersf) int (*a)10; / A pointer to an array of 10 integersg) int (*a)(int); / A pointer to a function a that takes an integer argument and returns an integerh) int (*a10)(int); / An array of 10
24、pointers to functions that take an integer argument and return an integerPeople often claim that a couple of these are the sorts of thing that one looks up in textbooks-and I agree. While writing this article, I consulted textbooks to ensure the syntax was correct. However,I expect to be asked this
25、question (or something close to it) when I'm being interviewed. Consequently, I make sure I know the answers, at least for the few hours of the interview. Candidates who don't know all the answers (or at least most of them) are simply unprepared for the interview. If they can't be prepar
26、ed for the interview, what will they be prepared for?.6. What are the uses of the keyword static?Answer:Static has three distinct uses in C:A variable declared static within the body of a function maintains its value between function invocationsA variable declared static within a module, (but outsid
27、e the body of a function) is accessible by all functions within that module. It is not accessible by functions within any other module. That is, it is a localized globalFunctions declared static within a module may only be called by other functions within that module. That is, the scope of the funct
28、ion is localized to the module within which it is declaredMost candidates get the first part correct. A reasonable number get the second part correct, while a pitiful number understand the third answer. This is a serious weakness in a candidate, since he obviously doesn't understand the importan
29、ce and benefits of localizing the scope of both data and code.7. What do the following declarations mean?const int a;int const a;const int *a;int * const a;int const * const a ;AnswerThe first two mean the same thing, namely a is a const (read-only) integer. The third means a is a pointer to a const
30、 integer (that is, the integer isn't modifiable, but the pointer is). The fourth declares a to be a const pointer to an integer (that is, the integer pointed to by a is modifiable, but the pointer is not). The final declaration declares a to be a const pointer to a const integer (that is, neithe
31、r the integer pointed to by a, nor the pointer itself may be modified).8. What does the keyword volatile mean? Give three different examples of its use.Answer:A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable.
32、In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register. Examples of volatile variables are:.Hardware registers in peripherals (for example, status registers) Non-automatic variables referenced within an interrupt service rout
33、ineVariables shared by multiple tasks in a multi-threaded application9.Can a pointer be volatile ? Explain.Yes, although this is not very common. An example is when an interrupt service routine modifies a pointer to a buffer10.What's wrong with the following function?:int square(volatile int *pt
34、r)return *ptr * *ptr;Answers:This one is wicked. The intent of the code is to return the square of the value pointed to by *ptr . However, since *ptr points to a volatile parameter, the compiler will generate code that looks something like this:int square(volatile int *ptr)int a,b;a = *ptr;b = *ptr;
35、return a * b;Because it's possible for the value of *ptr to change unexpectedly, it is possible for a and b to be different. Consequently, this code could return a number that is not a square! The correct way to code this is:long square(volatile int *ptr)int a;a = *ptr;return a * a;.11. Embedded
36、 systems always require the user to manipulate bits in registers or variables. Given an integer variable a, write two code fragments. The first should set bit 3 of a. The second should clear bit 3 of a. In both cases, the remaining bits should be unmodified.Answer:Use #defines and bit masks. This is
37、 a highly portable method and is the one that should be used.My optimal solution to this problem would be:#define BIT3(0x1<< 3)static int a;void set_bit3(void) a |= BIT3;void clear_bit3(void) a &= BIT3;12Embedded systems are often characterized by requiring the programmer to access a speci
38、fic memory location. On a certain project it is required to set an integer variable at the absolute address 0x67a9 to the value 0xaa55. The compiler is a pure ANSI compiler. Write code to accomplish this task.Answer:int *ptr;ptr = (int *)0x67a9;*ptr = 0xaa55;13. What does the following code output a
39、nd why? void foo(void)unsigned int a = 6;int b = -20;(a+b > 6) ? puts("> 6") :puts("<= 6");The answer is that this outputs "> 6." The reason for this is that expressions involving signed and unsigned types have all operands promoted to unsigned types. Thus ?
40、0 becomes a very large.positive integer and the expression evaluates to greater than 6. This is a very important point in embedded systems where unsigned data types should be used frequently.14.What does the following code fragment output and why?char *ptr;if (ptr = (char *)malloc(0) = NULL)puts(&qu
41、ot;Got a null pointer");elseputs("Got a valid pointer");Answer: Got a valid pointerThis is a fun question. I stumbled across this only recently when a colleague of mine inadvertently passed a value of 0 to malloc and got back a valid pointer! That is, the above code will output "
42、Got a valid pointer." I use this to start a discussion on whether the interviewee thinks this is the correct thing for the library routine to do. Getting the right answer here is not nearly as important as the way you approach the problem and the rationale for your decision.15.Typedef is freque
43、ntly used in C to declare synonyms for pre-existing data types. It is also possibleto use the preprocessor to do something similar. For instance, consider the following code fragment:#define dPSstruct s *typedefstruct s * tPS;The intent in both cases is to define dPS and tPS to be pointers to struct
44、ure s. Which method, if any, is preferred and why?The answer is the typedef is preferred. Consider the declarations:dPS p1,p2;tPS p3,p4;The first expands to:struct s * p1, p2;which defines p1 to be a pointer to the structure and p2 to be an actual structure, which is probably not what you wanted. Th
45、e second example correctly defines p3 and p4 to be pointers.Obscure syntax16. C allows some appalling constructs. Is this construct legal, and if so what does this code do?.int a = 5, b = 7, c;c = a+b;This code is treated as:c = a+ + b;Thus, after this code is executed, a = 6, b = 7, and c = 12.Real-time question1. What is real-time system?"A real-time system is one in which the correctnes
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- SpringBoot進階-深度研究
- 2025年山西信息職業(yè)技術學院高職單招高職單招英語2016-2024歷年頻考點試題含答案解析
- 2025年山東文化產業(yè)職業(yè)學院高職單招高職單招英語2016-2024歷年頻考點試題含答案解析
- 二年級數(shù)學計算題專項練習
- 2025年安徽醫(yī)學高等專科學校高職單招高職單招英語2016-2024歷年頻考點試題含答案解析
- 2025年安慶職業(yè)技術學院高職單招語文2018-2024歷年參考題庫頻考點含答案解析
- 2025年寧夏工商職業(yè)技術學院高職單招語文2018-2024歷年參考題庫頻考點含答案解析
- 2025年天津商務職業(yè)學院高職單招高職單招英語2016-2024歷年頻考點試題含答案解析
- 2025至2030年中國切斷坡口一體機數(shù)據(jù)監(jiān)測研究報告
- 2025年四川護理職業(yè)學院高職單招語文2018-2024歷年參考題庫頻考點含答案解析
- 2025年度公務車輛私人使用管理與責任協(xié)議書3篇
- 售后工程師述職報告
- 綠化養(yǎng)護難點要點分析及技術措施
- 2024年河北省高考歷史試卷(含答案解析)
- 車位款抵扣工程款合同
- 小學六年級數(shù)學奧數(shù)題100題附答案(完整版)
- 高中綜評項目活動設計范文
- 英漢互譯單詞練習打印紙
- 2023湖北武漢華中科技大學招聘實驗技術人員24人筆試參考題庫(共500題)答案詳解版
- 一氯二氟甲烷安全技術說明書MSDS
- 物流簽收回執(zhí)單
評論
0/150
提交評論