My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
Paid Surveys
|
Debug C code
Posted Date: 16 May 2008 Resource Type: Articles/Knowledge Sharing Category: Placement Papers
|
Posted By: Shanthi M Member Level: Diamond Rating: Points: 1
|
|
|
|
C code to Debug 1. int i=10; printf("i=%d",i); Can you think of any other way to print the value of i ?
Answer : int i=10; printf("i=%d",*(&i));
Explanation: *(&i)means the value contained in the address location denoted by i.
2. #include "stdio.h" main() { printf("%d %d",sizeof(NULL),sizeof("")); }
Answer: 2 1
Explanation: Because 'NULL' is defined to be 0 in stdio.h,the size of 0 which is an integer is 2 bytes. Even though the string has no characters, it has a null character '\0'. The size of this character is 1 byte.Hence the output is 2 1.
3. main() { int a=10; int *p; p=&a; printf("a=%d",a); printf("*p=%d",*p); printf("&a=%d",&a); printf("p=%u",p); printf("value at p=%d",*(&a)); } Note: The address of a is 1000. Answer: a=10 *p=10 &a=1000 p=1000 value at p=10
4.float a=4; int i=2; printf("%f %d",i/a,i/a); printf("%d %f",i/a,i/a);
Output:
0.5 0 0 0
Explanation: Conversion of lower byte to higher byte is not possible in printf statement. That is, %d is not converted to %f.
5. int x=10,y=5,p,q; p=x>9; q=x<3&&y!=3; printf("%d %d",p,q);
Output: 1 1
6. int x=10,y=-20; x=!x; y=!y; printf("%d %d",x,y);
Output: 0 0
Explanation: Any value other than 0 following this logical NOT (!) is assumed to be 1.
7. int c=5,d=5,e=-10,a; a=c>1?[d>1||e>1 ? 100:200]:300; What is the value of a?
Output: 100
|
Responses
|
| Author: Vidya 23 May 2008 | Member Level: Diamond Points : 2 | useful information
|
|
|