hi, i am new to c have to write a program to check the shape.
I think the error is somewhere at shape= square. I know i can use printf statements but i want it in this format. Can someone help me please?

#include <stdio.h>
int main (void)
{
int length,breadth;
char shape;
printf("Input your length: ");
scanf("%f", &length);
printf("Input your breadth: ");
scanf("%f", &breadth);

if (length=breadth)
       { 
            shape= 'square';      
       } 
else {
            shape = 'rectangle'
        }
printf(" the length is%d, breadth is %d , hence given shape is %s", length, breadth, shape);

Edited 7 Years Ago by __avd: Add [code] tags. Encase your code in: [code] and [/code] tags.

In C language, char value is stored in 1 byte. A char type may contain a single letter, digit, control character or special character.

>>if (length=breadth)

you are using the wrong operator -- use == boolean operator instead of = assignment operator.

>> shape= 'square';
Two problems with that line

  1. All strings (two or more characters) have to be enclosed in double quotes, such as "square"
  2. variable [shape] can only hold a single character, such just do this: shape = 's'; . If you want shape to contain the entire string then redefine it as char shape[15]; and use strcpy() to copy it strcpy(shape,"square");

Edited 7 Years Ago by Ancient Dragon: n/a

scanf("%f", &length);

length is an int, you are passing a float. Same with scanf("%f", &breadth); Either you change the type in the declaration of length and breadth, or change the f to d in the calling of scanf().

hi thx for the replies... i corrected it all and now it gives a warning saying
"assignment makes integer from pointer without a cast". otherwise can i use a string instead of char ?
Now the code looks like

#include <stdio.h>
int main (void)
{
int length,breadth;
char shape;
printf("Input your length: ");
scanf("%f", &length);
printf("Input your breadth: ");
scanf("%f", &breadth);

if (length==breadth)
{ 
shape= 's'; 
} 
else {
shape = 'r'
}
printf(" the length is%d, breadth is %d , hence given shape is %s", length, breadth, shape);

system("pause");
return 0;
}

Edited 7 Years Ago by __avd: Add [code] tags.

1) you failed to read Aia's comment.

2) The printf() statement is incorrect. %s is for a charcter array, shape is not a character array. What you want os %c

This question has already been answered. Start a new discussion instead.