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);

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");
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;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.