Thread: C program not executing Serially

  1. #1
    Registered User
    Join Date
    Feb 2012

    C program not executing Serially

    According to me C program should execute sequentially. I am referring to one of the standard C example projects developed by the micro controller chip manufacturers, i see in some of the functions where it sometimes executes the previous lines of code.
    void func1(void)
     Line1: Statement 1
     Line2: Statement 2
     Line3: Statement 3
    It executes Statement2 and normally should execute statement3 but it executes statement1 and then comes back to statement3.

    How is it possible? Please advise.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Perhaps you should post the actual example. In your overly simplified example, it looks either impossible, or perhaps you're missing an obvious goto or something.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You sometimes see weird stuff happening if you're trying to single step the code in a debugger.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Why is it so ? And how shall i proceed ? Can i just neglect and proceed ? Please advise.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Quote Originally Posted by Satya View Post
    Why is it so ?
    It isn't so. (EDIT: Unless you're referring to Salem's comment about single-stepping; if you have your optimization on high enough, and you have a fancy enough compiler, it might be able to determine that (a) the two lines are "independent" enough that they can be done in either order and (b) it is faster to do line two before line one.)

    Quote Originally Posted by Satya View Post
    And how shall i proceed ? Can i just neglect and proceed ? Please advise.
    What are you seeing that makes you think it is happening? Be as specific as possible. (Are you in the debugger? Are you watching LEDs being lit up by your chip?
    Last edited by tabstop; 11-11-2018 at 08:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. executing a program help
    By tbca in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 10:07 PM
  2. Executing one program from within another...
    By Raigne in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2007, 09:11 PM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. Executing a program
    By trovoada in forum C Programming
    Replies: 15
    Last Post: 07-04-2005, 06:01 AM
  5. executing program....
    By Turek in forum Windows Programming
    Replies: 6
    Last Post: 08-01-2002, 02:13 PM

Tags for this Thread