Thread: Declaring Variable

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    30

    Declaring Variable

    Ciao,

    This is a function to find sum of squared number between [a,b] interval. So my question is how do i get an output if i dont declare acc and bcc. Of course the output is wrong but, shouldnt compiler give an error? Or what is the value of an undeclared variable?

    Code:
    #include <stdio.h>
    
    int main (void) {
    	
    	int a,b,ac,acc,bc,bcc;
    	int result;
    	scanf("%d%d",&a,&b);
    	acc=0;
    	bcc=0;
    	if(a<=100 && b<=100){
    		for(ac=1; ac<=a-1; ac++){
    			acc+=ac*ac;
    		}
    		for(bc=1;bc<=b; bc++){
    			bcc+=bc*bc;
    		}
    	}
    	
    	result=bcc-acc;
    	printf("%d",result);
    	return 0;
    }
    Code:
    #include <stdio.h>
    
    int main (void) {
    	
    	int a,b,ac,acc,bc,bcc;
    	int result;
    	scanf("%d%d",&a,&b);
    	if(a<=100 && b<=100){
    		for(ac=1; ac<=a-1; ac++){
    			acc+=ac*ac;
    		}
    		for(bc=1;bc<=b; bc++){
    			bcc+=bc*bc;
    		}
    	}
    	
    	result=bcc-acc;
    	printf("%d",result);
    	return 0;
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Something that is not initialised is not the same as something that is not declared.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Hodor is right, but even the uninitialized case is caught by most C compilers, if you just ask for such warnings.

    If I save the latter code as example.c, and compile it using
    gcc -Wall -Wextra -O2 example.c -o example
    GCC helpfully tells me:
    Code:
    example.c: In function ‘main’:
    example.c:7:10: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d",&a,&b);
              ^
    example.c:17:11: warning: ‘bcc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         result=bcc-acc;
               ^
    example.c:17:11: warning: ‘acc’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    The return value of scanf() is the number of successful conversions (or EOF if read error, or end of input is reached before the first conversion or match). You really should verify it returned 2 (meaning both a and b were read); and if not, complain and exit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring variable in a for loop
    By GokhanK in forum C Programming
    Replies: 4
    Last Post: 08-01-2013, 04:38 PM
  2. SQL Exception ( declaring a variable )
    By Aga^^ in forum C# Programming
    Replies: 3
    Last Post: 07-21-2009, 03:49 AM
  3. Declaring pointer to variable in struct
    By movl0x1 in forum C Programming
    Replies: 5
    Last Post: 07-09-2007, 11:24 AM
  4. Does declaring a variable cause lag in the executable?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2005, 08:15 PM
  5. Conditional Variable Declaring
    By ibelievekip in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 11:38 AM