#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Program tracking..


    I am unable 2 understand the following programs and their output.. can u pls help..

    1.
    main()
    {
    float me=1.1;
    double you=1.1;
    if (me==you)
    printf("I hate C");
    else
    printf("I love C");
    }

    2.
    main()
    {
    char word[25];
    printf("word\n");
    scanf("%[A...I]c",word);
    printf("input");
    scanf("%[^'0']c",word);
    printf("over");
    getch();
    }

    3.
    main()
    {
    char *p="hai friends",*p1;
    p1=p;
    while(*p!='\0')
    ++*p++;
    printf("%s%s",p,p1);
    }

    4.
    main()
    {
    int a[]={1,4,3,5,7};
    int i=2;
    printf("%d",a[i][a]);
    getch();
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    These programs are useful for obfuscation contests. Don't write like this. Take #4.

    int a[]={1,4,3,5,7};
    int i=2;
    printf("%d",a[i][a]);

    Per my understanding, c evaluates pointer[integer] as
    *(pointer+integer)
    Addition commutes, (a+b) == (b+a)
    *(integer+pointer)
    hence you can write
    integer[pointer]
    But please don't, your fellow programmers aren't likely to think that way.

    a[i][a]

    a[i] is an integer, giving
    integer[a]
    a is a pointer
    a[i][a] looks valid to me.




    #1, I'd have to build and run, otherwise I'd have to spend maybe hours studying IEEE floating point. The key is to investigate number representations and what happens as the compiler casts through successive types. Again, that's part of the c standard.



    #3, study precedence and left-to-right versus right-to-left order of operation. Don't write code like that!



    #2, study the man pages for printf.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo