1

I tried running a C program with the code:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char grade;
    float mark;
    printf("Enter total mark percentage : ");
    scanf("%f", &mark);
    if(mark>=90){
        grade="A";
    }else{
        grade="B";
    }
    printf("Your grade is %c", grade);
    return EXIT_SUCCESS;
}

It produces a warning 'assignment from 'char' to 'char*' makes integer from pointer without a cast'.
I am able to solve it by changing " to ' in line 10 and 12

        grade='A';
    }else{
        grade='B';
    }

This solves the warning. But I want to use " to contain A & B. I tried initializing mark, as int instead of char in line 5 but it also didn't work. I just want to know what is the difference in using ' and " .

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Nidheesh
  • 37
  • 8
  • Single quote is used for `char` type while double quote for string literals. `grade="A";` is an ill formed code. `grade` expects a `char` data type while you are assigning `"A"` which is of string literal type. – haccks Apr 15 '21 at 08:42
  • 3
    This question is a tad basic, you probably should not explore the entire language like this, it could save time all around if you read a book about the language. [Here's a random list of books of varying quality.](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) –  Apr 15 '21 at 08:44
  • 1
    Does this answer your question? [Single quotes vs. double quotes in C or C++](https://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c-or-c) – mediocrevegetable1 Apr 15 '21 at 09:00
  • You could try to read a beginner-level C book and the answer will be revealed in the string chapter. – Lundin Apr 15 '21 at 09:13
  • "_I want to use " to contain A & B_" - why? Change `char grade;` to `const char* grade;` and the format specifier from `%c` to `%s` and you can. But there is no reason in the world to do so unless you want to assign grades such as `"B+"` or `"A*"` perhaps. I'd post that as an answer, but your question was _"I just want to know what is the difference in using ' and ""_ rather than _"how can I do what I want?"_. Maybe it is an X-Y problem? You could ask the question you really want to know the answer to and it would be less likely to be a duplicate and closed. – Clifford Apr 15 '21 at 09:20

3 Answers3

3

The symbol ' is used to specify an integer character constant. For example 'A' is an integer character constant that has the type int.

The symbol " is used to specify a string literal. For example "A" is a string literal that has the type char[2]. That is the string literal is a character array that is stored like

{ 'A', '\0' }

String literals contain sequences of characters terminated by the zero terminating character '\0' that also is stored in string literals.

Character arrays designators used in expressions are implicitly (with rare exceptions) converted to pointers to their first elements of the type char * or const char *.

This means that in this assignment statement

grade="A";

the string literal "A" that has the array type char[2] is converted to a pointer to its first element 'A'. As a result the compiler issues a message that you are trying to assign a pointer to an object of an integer type.

You can write for example

grade = "A"[0];

or

grade = *"A";

But this will only confuse readers of the code because it will be more simpler and clear to write

grade = 'A';

That is there is no need to use a whole array like "A" (remember that it internally represents an array of two elements { 'A', '\0' }) to use only its one element 'A'.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

"..." is used for strings - ie. a NUL-terminated char-array (char[] or char*)

'.' is used for a single character - ie. a char.

For example: "Hello" is a string (char-array) with the following 6 single characters:

'H', 'e', 'l', 'l', 'o', '\0' (The last is the NUL-character).

Thus

'A' is just 'A' (a single char containing the ASCII-value for A)

while

"A" is a char array, containing two chars - 'A' and '\0' (and thus a string)

Baard Kopperud
  • 577
  • 1
  • 5
  • 12
0

" " are used for string and ' ' is used for the character. as A and B are characters we will use single quotes for them.

this is why this format is correct.

grade='A';
    }else{
        grade='B';
    } 
Sukant Jha
  • 36
  • 4