user3460758
user3460758

Reputation: 987

Using character constant in C

I'm trying to write a program and I'm having some trouble assigning a certain word to a variable. the part that is giving me problems is this simple bit of code:

char x;
if (friction > fN || friction < -fN) {
    x = 'false';
    printf("\%s\n", x);
}
else {
    x = 'true';
    printf("\n%s\n", x);
}

fprintf(Output,"%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf, %s\n", theta, theta_rad, V, Fa, Uc, Vc, moment, fN, friction, x);

Essentially I'm just trying to output whatever the x "value" is and include it in my file.

I'm getting the following two warnings, all in relation to the lines x = 'false' and x = 'true' and how I am declaring my char x.

warning: character constant too long for its type
warning: overflow in implicit constant conversion
warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

Note: the rest of the arguments in the fprintf are printing fine, the issue is only in how I'm declaring the char. In the end, it is not printing the x 'value' at all...

Thank you in advance and I appreciate any help!

Upvotes: 0

Views: 502

Answers (5)

WileTheCoyot
WileTheCoyot

Reputation: 523

The problem you have with your code is that you declared x as a char. A char is a type that can store only 1 character. So, if you want to store more than 1 character, you need to use what is called a string. A string can be declared as a char vector. For example, char str[20] declare a string of 20 characters. However, in your case is better to use (note the third warning you get from your compiler) a pointer to a constant char, defined with char* str="string content". Please, take note the use of double quotes (") istead of the single quote (').The difference between them is that a single quote is used to initialize a char, for example char c='a'. When you declare a string, you have to use the double quotes, for example char *str="hello world!", or char str[]="hello world!". When you declare a string with double quotes, a special character, called string termintor ('\0', note that this is a single character so you use the single quote around it) is added at the end of the string. Here is an example of your code:

char* x; // x is declared as a constant string
if (friction > fN || friction < -fN) {
    x = "false";
    printf("\%s\n", x);
}
else {
    x = "true";
    printf("\n%s\n", x);
}

Upvotes: 2

ArthurChamz
ArthurChamz

Reputation: 2049

char store only one character. You need to declare an array of characters (more commonly known as string) and use the string.h functions (namely, strcpy) to copy and store your strings.

Also note that strings are enclosed in double quotes, not single quotes.

Having put #include <string.h> with your headers:

char x[255];
if (friction > fN || friction < -fN) {
    strcpy(x, "false");
    printf("\%s\n", x);
}
else {
    strcpy(x, "true");
    printf("\n%s\n", x);
}

fprintf(Output,"%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf, %s\n", theta, theta_rad, V, Fa, Uc, Vc, moment, fN, friction, x);

Of course, you might just want to print the "true" and "false" strings directly, if you're not using the x variable later.

Upvotes: 2

opalenzuela
opalenzuela

Reputation: 3171

As other users pointed out, char can only store one character (as the name says). An alternative code for what you are trying to do would be:

    if (friction > fN || friction < -fN) {
        printf("false\n");
        }
    else {
        printf("true\n");
        }

Upvotes: 1

JaredPar
JaredPar

Reputation: 754665

The char type is used to store a single character but you are trying to define constants that are of several characters. If you want a sequence of characters then use quotes (") and const char*.

Upvotes: 0

hugomg
hugomg

Reputation: 69934

chars are for only a single character. You will need to use strings for multiple characters.

char * x = "false"; /*double quotes*/
printf("%s\n", x);

That said, if just want to represent true and false its better to use the integers 1 and 0, respectively, since this way you can pass them to if statements and other conditionals.

Upvotes: 1

Related Questions