Beginners Tutorial : The C-language :: Discussions

Discussion in 'Computer Science | IT | Networking' started by simplycoder, Aug 20, 2011.

    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    Hello everyone, I am simplycoder and welcome to C-programing discussions.
    In this thread, you can post your questions and I shall try to answer them to best of my knowledge.
    I prefer the questions which are parallel to the thread dedicated to tutorials,but I shall answer any question which I think I can.
    So just shoot the questions, some one would answer your questions sooner or later as this forum has a lot of good programers.
    • The MOD Squad

    AbraKaDabra Moderator

    Message Count:
    8,887
    Ratings Received:
    +1,177
    Engineering Discipline:
    Computer Science
    @simplycoder: Great initiative.
    We are proud to have CEans like you! :thumbsup:

    Reya Apprentice

    Message Count:
    2,011
    Ratings Received:
    +145
    Engineering Discipline:
    Computer Science
    @simplycoder: Give us some debugging questions too.

    TheV Certified CEan

    Message Count:
    80
    Ratings Received:
    +8
    Engineering Discipline:
    Computer Science
    Can you explain this code ... . . .. !
    Code:
     #include<stdio.h>
    void main()
     {
     int i=1,a;
     a = ++i + (++i)++ + ++i ;
     printf("%d",a);
     }
    
    How the middle 'i' is being calculated..

    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    @TheV:
    I dont think you compiler is going to like this, I have not written the code as yet, but then I do think that post increment is going to make some troubles with this program.

    As a thumb rule, modifying any statement more than once without a sequence point will lead to undefined results in C.
    in your case you have modified i multiple times without a sequence point, this will cause sequence point ambiguity.

    The best thing is, you can write your code in some other language except for C++ say in a Java, find out your result and let us know if they are same or not.

    TheV Certified CEan

    Message Count:
    80
    Ratings Received:
    +8
    Engineering Discipline:
    Computer Science
    @simplycoder... I have compiled this code in Turbo C++ and it's giving me 14 as output...but I can't make it out the actual calculating procedure..

    Reya Apprentice

    Message Count:
    2,011
    Ratings Received:
    +145
    Engineering Discipline:
    Computer Science
    TheV: Is it running without any error?

    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    @TheV: Now rewrite yout code in Java if you can and post the answer.
    See the sequence point ambiguity for the same.

    TheV Certified CEan

    Message Count:
    80
    Ratings Received:
    +8
    Engineering Discipline:
    Computer Science
    Friends have you compiled the code ,,,,,, I have compiled in both Turbo C++ and borland C++ . I am getting the same
    answer as 14 . .. Are you people got the same answer or having any error...??

    @simplycoder: I have compiled the same code after writing in java ... and I am getting 4 error.. But I am not concern about java compiler
    As java and C compilers are different so the result is different but I have written this code in C and I am getting answer as
    14. Are you compiled this code in C ? Are you getting any error ?

    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    I am getting the post-increment errors as I pointed out.
    I dont really get you on this note. AFAIK, this is sequence point error.

    You can try the expression : a = ++i + ++i + ++i; in java and c. This wont give you any error, but surely will give you different answers.

    TheV Certified CEan

    Message Count:
    80
    Ratings Received:
    +8
    Engineering Discipline:
    Computer Science
    But I am not getting any error.....


    That what I wanted to say you in my above lines....

    In java It is giving me ---- 9
    but in C ------------------12

    In C I understood the concept..It first increases the pre-increment of ' i ' and then placed to evaluate the expression..(Correct me if wrong)

    Can you explain me what is concept in java...? How 9 output is been shown in java....

    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    @TheV: surprisingly, for me in on my devC++, its 10 from C program.
    this is the sequence point ambiguity.
    java works in the same way in case of increment.
    but then for C its just sequence point ambiguity. check for google on the same for sequence point ambiguity.

    Reya Apprentice

    Message Count:
    2,011
    Ratings Received:
    +145
    Engineering Discipline:
    Computer Science
    i got the output as 10.How come will the output will differ in Turboc3 and Devc?

    H.Ali Apprentice

    Message Count:
    56
    Ratings Received:
    +0
    Engineering Discipline:
    Instrumentation & Control
    Hi simply coder ...... :)
    your Tutorials are really Helpful.. and thanking you for your Efforts....

    have a question regarding the sample program given in 16 th tutorial(Menu Driven program)...
    >> in your program the
    is placed out side the Switch , its ok when when the case 1 to 4 occurs :)
    but when the Default case is executed , it also prints the answer !
    i think it will be better if the printf is placed inside each cases ,(but then we have to type it 4 times :( )
    any other suggestion??



    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    @H.Ali: Yeah, you are correct. My Bad.
    I think we have to place printf you are correct. We should print it 4 times. That is very easy and nothing new is to be learnt.
    thanks for pointing it out.

    H.Ali Apprentice

    Message Count:
    56
    Ratings Received:
    +0
    Engineering Discipline:
    Instrumentation & Control
    please explain the logic to print pascals triangle [using *]

    Attached Files:

    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    To program any pattern, just make a table like this.
    [TABLE="class: grid"]
    [TR]
    [TD]Row.no[/TD]
    [TD]Stars[/TD]
    [TD]Blanks[/TD]
    [/TR]
    [TR]
    [TD]1[/TD]
    [TD]1[/TD]
    [TD]4[/TD]
    [/TR]
    [TR]
    [TD]2[/TD]
    [TD]2[/TD]
    [TD]3[/TD]
    [/TR]
    [TR]
    [TD]3[/TD]
    [TD]3[/TD]
    [TD]2[/TD]
    [/TR]
    [TR]
    [TD]4[/TD]
    [TD]4[/TD]
    [TD]1[/TD]
    [/TR]
    [TR]
    [TD]5[/TD]
    [TD]5[/TD]
    [TD]0[/TD]
    [/TR]
    [TR]
    [TD]n[/TD]
    [TD]n[/TD]
    [TD]n-stars[/TD]
    [/TR]
    [/TABLE]


    By using this logic, you can print the stars.
    Make sure you print blanks first and then stars.

    monujatt Certified CEan

    Message Count:
    41
    Ratings Received:
    +5
    Engineering Discipline:
    Computer Science
    Code is not getting compiled in java.... giving error at (++i)++ :)

    Engg.Dee Apprentice

    Message Count:
    46
    Ratings Received:
    +6
    Engineering Discipline:
    Industrial
    hi simply coder......i am from production engg. and i will have to face and campus drive in coming month. Tcs is coming for campus..they want us to have a fundamental knowledge of a language...can u help me to learn the basic of c.

    simplycoder Maniac

    Message Count:
    299
    Ratings Received:
    +98
    Engineering Discipline:
    Electronics & Communications
    You can refer the tutorial which I wrote on this forum,
    Pointers is an important topic left uncovered in tutorial.
    Hopefully soon I shall get some time to start writing from where I left.

    For book I suggest http://en.wikipedia.org/wiki/The_C_Programming_Language nice book, and a must read for insights of the language.

    If you have some doubt or stuck in analysis, post it down here.

Share This Page