954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

char in c?

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);
dewdropz
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

>>if (length=breadth)

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

>> shape= 'square';
Two problems with that line All strings (two or more characters) have to be enclosed in double quotes, such as "square"
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");

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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().

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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;
}
dewdropz
Newbie Poster
5 posts since Nov 2009
Reputation Points: 10
Solved Threads: 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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: