Find the answer to your Linux question:
Results 1 to 3 of 3
I am just now diving into learning c and i am following a tutorial on a website and it says "So in C you must declare all of your variables ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie champ_weller's Avatar
    Join Date
    Sep 2011
    Location
    ohio
    Posts
    106

    simple c question


    I am just now diving into learning c and i am following a tutorial on a website and it says "So in C you must declare all of your variables before you do anything else"

    so this code is suppose to be wrong
    Code:
    #include <stdio.h> 
    int main ()
    {
    
    /*wrong! the variable declarations must appear first*/
    	printf ("declare x next");
    	int x;
    
    	return 0 ;
    }
    and this is the good code
    Code:
    #include <stdio.h>
    int main ()
    {
    	int x;
    	printf ( "declare x first" ) ;
    
    	return 0;
    }
    but when i execute i get the same result from both so i am just wondering if i am missing anything here? is it suppose to be wrong and it is just not good way to format your code? or what?

  2. #2
    Just Joined!
    Join Date
    Sep 2012
    Location
    Nashville, TN
    Posts
    98
    In that code they are more referencing style instead of functionality. You are not actually making use of the variable x. But if you wrote the first program like this.
    Code:
    #include <stdio.h> 
    int main ()
    {
    
    /*wrong! the variable declarations must appear first*/
    	printf ("declare x next %d", x);
    	int x;
    
    	return 0 ;
    }
    then you would get an error. In short what I think they are trying to say is that you should always declare the variables first so that if you use them you know they are already defined and ready to be used.

  3. #3
    Linux Newbie champ_weller's Avatar
    Join Date
    Sep 2011
    Location
    ohio
    Posts
    106
    Thank you i appreciate it!

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •