Beginners Tutorial : The C-language :: Discussions

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.

Replies

  • Ankita Katdare
    Ankita Katdare
    @simplycoder: Great initiative.
    We are proud to have CEans like you! πŸ‘
  • Reya
    Reya
    @simplycoder: Give us some debugging questions too.
  • TheV
    TheV
    Can you explain this code ... . . .. !
     #include
    void main()
     {
     int i=1,a;
     a = ++i + (++i)++ + ++i ;
     printf("%d",a);
     }
    
    How the middle 'i' is being calculated..
  • simplycoder
    simplycoder
    @TheV:
    Can you explain this code ... . . .. !
    Code:

    #include void main() { int i=1,a; a = ++i + (++i)++ + ++i ; printf("%d",a); }
    How the middle 'i' is being calculated..
    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
    TheV
    @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
    Reya
    TheV: Is it running without any error?
  • simplycoder
    simplycoder
    @TheV: Now rewrite yout code in Java if you can and post the answer.
    See the sequence point ambiguity for the same.
  • TheV
    TheV
    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
    simplycoder
    I am getting the post-increment errors as I pointed out.
    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 ?
    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
    TheV
    But I am not getting any error.....

    Originally posted by: simplycoder.

    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.

    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
    simplycoder
    @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
    Reya
    i got the output as 10.How come will the output will differ in Turboc3 and Devc?
  • H.Ali
    H.Ali
    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
    printf("ANSWER :: %lf\n",answer);
    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
    simplycoder
    @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
    H.Ali
    please explain the logic to print pascals triangle [using *]
  • simplycoder
    simplycoder
    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
    monujatt
    Code is not getting compiled in java.... giving error at (++i)++ πŸ˜€
  • Engg.Dee
    Engg.Dee
    simplycoder
    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.
    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
    simplycoder
    Engg.Dee
    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.
    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 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.
  • Engg.Dee
    Engg.Dee
    simplycoder
    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 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.
    okk.............!
  • Vishal Sharma
    Vishal Sharma
    Can you explain this code ... . . .. !
    Code:
    #include
    void main()
    {
    int i=1,a;
    a = ++i + (++i)++ + ++i ;
    printf("%d",a);
    }
    How the middle 'i' is being calculated..​
    the i modification statements are undefined. Different compilers may produce different outputs which cannot be judged. The pre-, -post increments are not supposed to be used in this way since the main reason for using the increments is controlling the loops. The above snippet causes ambiguity or confusion to the compiler as the same value is being incremented in several ways. hence, different compilers produce different outputs!
  • Vishal Sharma
    Vishal Sharma
    TheV
    Can you explain this code ... . . .. !
    #include
    void main()
    {
    int i=1,a;
    a = ++i + (++i)++ + ++i ;
    printf("%d",a);
    }
    
    How the middle 'i' is being calculated..
    the i modification statements are undefined. Different compilers may produce different outputs which cannot be judged. The pre-, -post increments are not supposed to be used in this way since the main reason for using the increments is controlling the loops. The above snippet causes ambiguity or confusion to the compiler as the same value is being incremented in several ways. hence, different compilers produce different outputs!
  • Vishal Sharma
    Vishal Sharma
    Okay guys Here's a simple question!!
    QUE:: Write a program to make a number HALF without using ' / '

    Post your code snippets! πŸ˜€
  • sulochana anand
    sulochana anand
    as my knowledge is concern if increment is applied three or more than three it increse twice in value.
  • sulochana anand
    sulochana anand
    am also agree that increment nd decrement op are used in loop.so no need more discussion over it.diffrent compiler may give diffrent result.its also true.
  • sulochana anand
    sulochana anand
    Vishal0203
    Okay guys Here's a simple question!!
    QUE:: Write a program to make a number HALF without using ' / '

    Post your code snippets! πŸ˜€
    can v try it as following sir?hahaha
    int i=20;
    printf(i>>1);
  • Vishal Sharma
    Vishal Sharma
    can v try it as following sir?hahaha
    int i=20;
    printf(i>>1);
    sir?? 😨 I'm just a 2nd year student πŸ˜–
    by the way, your code just needs a quoted %d in printf() statement, everything is fine after that. haha 😁

    Any other ways to do it?? 😏
  • sulochana anand
    sulochana anand
    Vishal0203
    sir?? 😨 I'm just a 2nd year student πŸ˜–
    by the way, your code just needs a quoted %d in printf() statement, everything is fine after that. haha 😁

    Any other ways to do it?? 😏
    yes u r write
  • 123_neha
    123_neha
    H.Ali
    please explain the logic to print pascals triangle [using *]
    try this code for first inverted triangle
    for(i=5;i>=1;i--)
    {
    for(j=1;j<=i;j++)
    {
    cout<<" "<<"*"<<" ";
    }
    cout< }

    you can do the second one by simply reversing the above loops
  • rahul69
    rahul69
    sulochana anand
    can v try it as following sir?hahaha
    int i=20;
    printf(i>>1);
    This logic does not work on the floating point numbers..(i think..) . A much simpler and effective way is this:
    no=no*0.5;
    😎
  • sulochana anand
    sulochana anand
    th
    rahul69
    This logic does not work on the floating point numbers..(i think..) . A much simpler and effective way is this:
    no=no*0.5;
    😎
    thanx i learned new thing.my math is not so good.
  • Jeffrey Arulraj
    Jeffrey Arulraj
    ohm's law
    how do I bypass a passworded computer running on windows 7 operating sysyem
    Do you mean to say that you forgot your password for Win 7

    Just remove the Cmos battery in the desktop that will reset all your passwords
  • Brainy
    Brainy
    hello engineers, how are you all? Nice to meet you all here again.
  • Chinu1
    Chinu1
    Thanks.. Please tell me about library function. And what the major difference between C and C++.
  • sulochana anand
    sulochana anand
    sulochana anand
    th

    thanx i learned new thing.my math is not so good.
    i want to know how any number multiplied by 0.5 becomes half.
  • Jeffrey Arulraj
    Jeffrey Arulraj
    sulochana anand
    i want to know how any number multiplied by 0.5 becomes half.
    Hey there .5 = 5/10 = 1/2

    Hope you got it

    But this is not a place to start fresh ideas buddy try to shift your new questions to threads
  • sulochana anand
    sulochana anand
    Conqueror
    Hey there .5 = 5/10 = 1/2

    Hope you got it

    But this is not a place to start fresh ideas buddy try to shift your new questions to threads
    thanx i got it.mens if v want to make half 13.then 13*0.5=6.5.0.5=5/10=1/2*13=6.5.right!
  • Jeffrey Arulraj
    Jeffrey Arulraj
    sulochana anand
    thanx i got it.mens if v want to make half 13.then 13*0.5=6.5.0.5=5/10=1/2*13=6.5.right!
    I am confused in your statement but the last part of your answer works the way you say
  • suchismith roy
    suchismith roy
    what will be the output and why
    main()
    {
    if(2,1,0)
    printf("yes");
    else
    printf("no");



    the output comes as no..as i have compiled it ...plz expalin me how
  • Vishal Sharma
    Vishal Sharma
    suchismith roy
    what will be the output and why
    main()
    {
    if(2,1,0)
    printf("yes");
    else
    printf("no");



    the output comes as no..as i have compiled it ...plz expalin me how
    in C, 0 stands for false.. and any other number means true..
    so whenever the condition is true, if block statement will be executed, else the else part..
    you've give 3 numbers 2,1,0
    since 0 stands for false, and o/p is no;
    if you just write 2,1,4,5,6,7....(non zero, non-negetive)
    then o/p will be yes
  • SamiZzY
    SamiZzY
    So this ques came in my paper, can anyone provide a ans with an explanation.
    #include
    { int x=10,y,z;
    z=y=x;
    y-=--x;
    z-=x--;
    x-=--x - x--;
    printf("x=%d y=%d z=%d",x,y,z);
    }
  • Pushpendra2911
    Pushpendra2911
    SamiZzY
    So this ques came in my paper, can anyone provide a ans with an explanation.
    #include
    { int x=10,y,z;
    z=y=x;
    y-=--x;
    z-=x--;
    x-=--x - x--;
    printf("x=%d y=%d z=%d",x,y,z);
    }
    on paper i solved
    x = -8
    y = 1
    z = 0

    let me know the answer
  • SamiZzY
    SamiZzY
    That's exactly the ans I came up with! Bt ppl were saying something like -6 1 0 . I dnt knw the real ans though.
  • rahul69
    rahul69
    SamiZzY
    So this ques came in my paper, can anyone provide a ans with an explanation.
    #include
    { int x=10,y,z;
    z=y=x;
    y-=--x;
    z-=x--;
    x-=--x - x--;
    printf("x=%d y=%d z=%d",x,y,z);
    }
    Ans should be x=6,y=1,z=1
    Initially x=y=z=10
    then applying post(use then change) and pre(change then use) decrement, this answer is obtained.πŸ˜’
  • SamiZzY
    SamiZzY
    Aaaaaahhhhh damn! Bt thankyou.
  • SamiZzY
    SamiZzY
    Actually I was hoping if someone could explain it step by step . I knw abt the post and pre increment operators but again and again I arrive at the wrong ans.
  • Vishal Sharma
    Vishal Sharma
    gjhgj
  • Vishal Sharma
    Vishal Sharma
    I think calculating the value of z and y is not a big deal, still I'll explain..
    after 1st line x=y=z => x = 10, y = 10, z = 10
    second step is y -= --x viz. equal to y = y - --x i.e. y = 10 - 9 (according to what rahul69 said, pre-decrement is done first and then the value is assigned)
    so, y = 1 and present values of x = 9 , y = 1 , z = 10

    in third step, z = z - x-- i.e. z = 10 - 9 (since post decrement, x value is assigned first and then x is decremented)
    so, z = 1 present values are x = 8 , y = 1 , z = 1

    forth step, x = x - (--x - x--) i.e. x = x - (7 - 7) (hope you understood why i've written 7 - 7)
    i've not written the value of x in above statement because after the calculation of
    (--x - x--) {bracketed part is calculated first because of the precedence rule} the value of x changes to 6
    so, x = 6

    therefore, your answer is x = 6, y = 1, z = 1
  • Vishal Sharma
    Vishal Sharma
    sorry about "gjhgj" πŸ˜› I was unable to post πŸ˜’
  • Vishal Sharma
    Vishal Sharma
    SamiZzY
    Actually I was hoping if someone could explain it step by step . I knw abt the post and pre increment operators but again and again I arrive at the wrong ans.
    you can try tracing and adding a watch to the following code to understand the things

    #include
    int main()
    {
    int x=10,y,z;
    z=y=x;
    y-=--x;
    z-=x--;
    int a =--x;            //calculation of x broken into 3 parts
    int b =x--;
    int c = x - (a - b);
    printf("x=%d y=%d z=%d",x,y,z);
    return 0;
    }
  • SamiZzY
    SamiZzY
    Had to read 4th step again n again bt thnx man priceless explanation.
  • Vishal Sharma
    Vishal Sharma
    SamiZzY
    Had to read 4th step again n again bt thnx man priceless explanation.
    I wanted you to do a little work over the calculation of (--x - x--) to understand how it works! Hope you understood it now
  • SamiZzY
    SamiZzY
    I think I do now thnx 😁
  • Jeffrey Arulraj
    Jeffrey Arulraj
    alicia32
    Hello and thanks for the information. I need to know from where i can download tutorials on PDF format or in video format. I am learning C and looking for some additional help. I have find this forum very helpful that is why i am asking for help.
    Try codecademy.com

    there the C language is taught to you in your own pace That is really helpful

You are reading an archived discussion.

Related Posts

i'm a final year student of university of ilorin. i want project topic on web design
[video=youtube;lESZqCflB0o]https://www.youtube.com/watch?v=lESZqCflB0o&feature=relmfu[/video]
Gate Scorer above 99 percentile: Gate Scorer above 99 percentile have to apply for all IIT’s and IISc with their interested specializations. They also have to apply for NITIE. If...
We've been denying Username Change requests. Those who want their username changed, please get in touch with me VIA PM as soon as possible with following information - 1. Username...
Centre for training an employment (CTE)- " A nodal agency for recruitment of various vacancies in different PSUs and semi-government organisation" has announced vacancies for Engineer’s in various streams (Electronics...