Need help with initialization errors

This is a discussion on Need help with initialization errors within the C Programming forums, part of the General Programming Boards category; Hi All, I was wondering if someone could help me out with the following code. I am just learning C ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    Need help with initialization errors

    Hi All,
    I was wondering if someone could help me out with the following code. I am just learning C and am trying to compile the following program but keep running into errors regarding the initialization of 'x' and 'y' even though I initialized them in the beginning. Can someone offer assistance? Thank you. Here's the code followed by the errors:
    insert
    Code:
    #include <stdio.h>
    
    main()
    { 
    	int x,y;
    	float f,z;
    	char c = 'A';
    	double d;
    /** Let us initialize some variables * /
    	x = 5;
    	y = 10.0;
    	z = 7.5;
    	f = 105.10;
    
    	if(y = 10)           /*if y equals 10 */
    	x += 20;            /*add 20 to x */
    
    	z = y + x;	         /*** ***/
    
    	f = z*z*z;	        /* raise z to the third power*/
    	f = z/15;
    
    	d = z*f;	       // yet another product
    
    	y = (int)z;	      // cast z into an integer
    
    	c = c - 32;	     /*convert character into lower character
    	*/
    
    	printf("x = %d, y = %d, z = %-5d\n",x,y,z);
    	printf("f = %d \t %x \t %8.4f\n",f,f,f);
    	printf("d = %i\n", d);
    	printf("c = %c \t %x \t %o\n",c,c,c);
    	printf("d = %i\n", d);
    
    	return 0;
    }
    These are the errors I'm getting:
    (18) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
    (16) : warning C4700: uninitialized local variable 'x' used
    (18) : warning C4700: uninitialized local variable 'y' used

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Except from the part where you make an assignment in an if() statement:
    Code:
    /* Wrong */
    	if(y = 10)           /*if y equals 10 */
    	x += 20;            /*add 20 to x */
    
    /* Correct */
            if( y == 10 )
                    x += 20;
    The results are warnings because of using float's and ints without explicit casts in some places.
    You'd like to declare main to return int too.
    Try not to initialize an int with a double value.
    Code:
    int y;
    ...
    y = 10.0;
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 11:50 AM
  2. Unknown Errors in simple program
    By neandrake in forum C++ Programming
    Replies: 16
    Last Post: 04-06-2004, 03:57 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 09:18 PM
  5. I need help on these few errors!!
    By BubbleMan in forum Windows Programming
    Replies: 3
    Last Post: 09-10-2001, 12:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21