Thread: I can't understand this program

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    6

    I can't understand this program

    Code:
    
    
    Code:
    #include <stdio.h>
    int x=5;
    void f (int x)
    {
    	printf("%d\n",x++);    
    }
    void g (void)
    {
    	static int x=0;
    	printf("%d\n",x++);  
    }
    int main(int argc, char** argv) {
    
    	printf("%d\n",++x);   
    	{
    		int x=4;
    		printf("%d\n",x++);
    	}
    
    	g();
    	f(x);
    	g();
    
    
    	return 0;
    
    
    
    }
    

    The output value is:

    6
    4
    0
    6
    1

    Can anyone give me some advice?
    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Again with the atrocious formatting. Click the "preview" button and make any necessary edits to your post before you submit.

    Hint: copy and paste as plain text to avoid any font face or size changes, or other format/display issues. That goes for code and non-code.

    EDIT: Oh, and my advice is "learn to read/trace code by hand". Not just for these homework assignments, it is an essential skill for all programmers at any level. I make a chart with a column for line number, a column for an explanation of what that line is doing, and a column for every variable. I then walk through the code line by line, filling in a new row for every line. Yes, it's a pain, but once you get the hang of it on paper, it becomes easy to do it all in your head.
    Last edited by anduril462; 12-11-2014 at 10:03 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Closed until the OP can figure out how to make decent posts.
    I fixed two already - I'm not going to waste any more time fixing others.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me understand what the next program does
    By alon4963 in forum C Programming
    Replies: 4
    Last Post: 10-26-2013, 11:26 PM
  2. Can you help me understand this program?
    By SCRIPT_KITTEH in forum C Programming
    Replies: 7
    Last Post: 07-23-2013, 04:03 AM
  3. help creating the program cannot understand at all
    By moe84 in forum C Programming
    Replies: 1
    Last Post: 12-01-2011, 10:47 PM
  4. Help me understand this program
    By DCICJay in forum C Programming
    Replies: 2
    Last Post: 06-06-2011, 08:52 PM
  5. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM