Wednesday, December 31, 2008

C Program Page 1

What will print out?
main( )
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%s\n”,p2);
}
Answer: empty string.

What will be printed as the result of the operation below:
main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%d\n”,x,y);
}
Answer : 57 94

What will be printed as the result of the operation below:
main()
{
int x=5;
printf(“%d,%d,%d\n”,x,x< <2,x>>2);
}
Answer: 5,20,1

What will be printed as the result of the operation below:
#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %d\n”,x,y);
swap2(x,y);
printf(“%d %d\n”,x,y);
}
int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;
}
Answer: 10, 510, 5

What will be printed as the result of the operation below:
main()
{
char *ptr = ” Cisco Systems”;
*ptr++;
printf(“%s\n”,ptr);
ptr++;
printf(“%s\n”,ptr);
}
Answer:Cisco Systems
isco systems

What will be printed as the result of the operation below:
main()
{
char s1[]=“Cisco”;
char s2[]= “systems”;
printf(“%s”,s1);
}
Answer: Cisco

What will be printed as the result of the operation below:
main()
{
char *p1;
char *p2;
p1=(char *)malloc(25);
p2=(char *)malloc(25);
strcpy(p1,”Cisco”);
strcpy(p2,“systems”);
strcat(p1,p2);
printf(“%s”,p1);
}
Answer: Ciscosystems

The following variable is available in file1.c, who can access it?
static int average;
Answer: all the functions in the file1.c can access the variable.

What will be printed as the result of the operation below:
main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %d\n”,x,y);
}
Answer: 11, 16

What will be printed as the result of the operation below:
main()
{
int a=0;
if(a==0) printf(“Cisco Systems\n”);
printf(“Cisco Systems\n”);
}
Answer: Two lines with “Cisco Systems” will be printed.

Predict the output or error(s) for the following:
1:-
void main()
{
int const * p=5;
printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation: p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

2:-
main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer: mmmm aaaa nnnn
Explanation: s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].

3:-
main()
{
float me = 1.1;
double you = 1.1;
if(me==you)printf("I love U");
else
printf("I hate U");
}
Answer: I hate U
Explanation:For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.Rule of Thumb: Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) .

4:-
main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
Answer:5 4 3 2 1
Explanation:When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

5:-
main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++)
{
printf(" %d ",*c);
++q;
}
for(j=0;j<5;j++)
{
printf(" %d ",*p);
++p;
}
}
Answer: 2 2 2 2 2 2 3 4 6 5
Explanation: Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.

6:-
main()
{
extern int i;
i=20;printf("%d",i);
}
Answer:
Linker Error : Undefined symbol '_i'
Explanation : extern storage class in the following declaration, extern int i;specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .

7:-
main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
Answer:0 0 1 3 1
Explanation :Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR () operator. So the expression ‘i++ && j++ && k++’ is executed first. The result of this expression is 0 (-1 && -1 && 0 = 0). Now the expression is 0 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.

8:-
main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}
Answer:1 2
Explanation:The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

9:-
main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
Answer :three
Explanation :The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

10:-
main()
{
printf("%x",-1<<4);
}
Answer: fff0
Explanation : -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value.

11:-
main()
{
char string[]="Hello World";
display(string);
}
display(char *string)
{
printf("%s",string);
}
Answer:
Compiler Error : Type mismatch in redeclaration of function display Explanation :In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.
12:-
main()
{
int c=- -2;
printf("c=%d",c);
}
Answer: c=2;
Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus.Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable.

13:-
#define int char
main()
{
int i=65;
printf("sizeof(i)=%d",sizeof(i));
}
Answer: sizeof(i)=1
Explanation: Since the #define replaces the string int by the macro char

14:-
main()
{
int i=10;
i=!i>14;
Printf ("i=%d",i);
}
Answer : i=0
Explanation: In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).

Tuesday, December 30, 2008

HTML Page 1

HTML
*With HTML you can create your own Web site.
*HTML is very easy to learn!
*You will enjoy it!

What is HTML?
*HTML is a language for describing web pages.
*HTML stands for Hyper Text Markup Language
*HTML is not a programming language, it is a markup language
*A markup language is a set of markup tags
*HTML uses markup tags to describe web pages

HTML Tags
*HTML markup tags are usually called HTML tags
*HTML tags are keywords surrounded by angle brackets like
*HTML tags normally come in pairs like and
*The first tag in a pair is the start tag, the second tag is the end tag
*Start and end tags are also called opening tags and closing tags.

HTML Documents - Web Pages
*HTML documents describe web pages
*HTML documents contain HTML tags and plain text
*HTML documents are also called web pages
*The purpose of a web browsers (like Internet Explorer) is to read HTML documents and *display them as web pages. The browser does not display the HTML tags, but uses the tags to *interpret the content of the page:

Try it yourself

What Do You Need?
*You don't need any tools to learn HTML at W3Schools.
*You don't need any HTML editor
*You don't need a web server
*You don't need a web site

Editing HTML
*In this tutorial we use a plain text editor (like Notepad) to edit HTML. We believe this is the best way to learn HTML.
*However, professional web developers often prefer HTML editors like FrontPage or Dreamweaver, instead of writing plain text.

Creating Your Own Test Web
*If you just want to learn HTML, skip the rest of this chapter.
*If you want to create a test web on your own computer, just copy the 3 files below to your desktop.
*(Right click on each link, and select "save target as" or "save link as")
mainpage.htm
page1.htm
page2.htm
*After you have copied the files, you can double-click on the file called "mainpage.htm" and see your first web site in action.

Use Your Test Web For Learning
*We suggest you experiment with everything you learn at W3Schools by editing your web files *with a text editor (like Notepad).
Note: If your first web site contains HTML markup tags you have not learned yet, don't panic. *You will learn much more HTML in the next chapters.

HTM or HTML Extension?
*When you save an HTML file, you can use either the .htm or the .html extension. We use .htm in our examples. It is a habit from the past, when the software only allowed three letters in file extensions.
*With new software it is perfectly safe to use .html.