Total Pageviews

Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Tuesday, December 13, 2011

C loves me...C loves me not...

#include<stdio.h>
#include<string.h>

int main()
{
    int i, n;
    char *x="Alice";
    n = strlen(x);
    *x = x[n];
    for(i=0; i<=n; i++)
    {
        printf("%s ", x);
        x++;
    }
    return 0;
}

Hi C programmers, the output of the above program should have been: lice ice ce e
but when compiled it gives the output as follows : Alice lice ice ce e

Could someone please explain?

Monday, November 28, 2011

C loves me...C loves me not...

Hi C Programmers,caught up in another sticky situation. And you gotta help me out in this.Pertains to pointers and all stuff.Hope you come up with a soultion. Long live C.


This program gives an error of "invalid lvalue in increment" in the line with printf()
#include <stdio.h>
main()
{ int p =1;
printf("%d",++p++);

}

But if we modify the program to operate it on a pointer as follows:

#include <stdio.h>
main()
{ int *p,a =1 ;
 p = &a;
printf("%d",++*p++);
}

It runs successfully and prints '2' as output.
What is causing the difference here.Variable 'p' and '*p' are both integers being treated in the same way.The only aberration here  is that if i put *p in parenthesis in the second program like (*p),it gives the same error as in the first program!!Please quote a possible explanation to it

Thursday, November 17, 2011

C loves me...C loves me not...

Hi C programmers, i got stuck at this piece of code and concept of precedence, associativity and order of evaluation within printf() in C.Most of you are adept C programmers.So please, you gotta help me out in explaining the output of this code.This post may clarify some of your queries as well as unary operators are very important.

#include <stdio.h>
void main()
{
 int j = 1;

printf("%d%d%d%d" ,++j,j++,j,--j);

}
 Output is 2022
but as my friends and I thought, it should be 2020 if we consider the order of evaluation to be R to L.
Please reply to this post if you find a possible explanation to it.