0

I am an absolute newbie, and I am learning to code.

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

int main(){
    
    char characterName[] = "Khan";
    int characterYear = 2022;
    float characterScore = 8.8;
    char characterGrade = 'A';
    printf("%s passed out of college in %d and had a grade of %s i.e. a score of %f\n",characterName,characterYear,characterGrade,characterScore);

    return 0;
}

Can someone help me with this — I was just trying to incorporate all simple data types in one line (for reference) and after I run the program there is no output whatsoever, it just exits! It didn't happen when I had fewer data types in the line.

P.S. This might be the dumbest ever question ever asked on Stack Overflow, but please forgive if I am dumb/silly :)

2
  • 1
    Enable warnings with -Wall or /Wall and you'll see the problem immediately
    – Chris Dodd
    Commented Sep 10, 2021 at 19:48
  • regarding: float characterScore = 8.8; This is trying to push a double literal into a float variable. Not a good idea. A double literal can be rewritten as a float literal by appending a f to the end of the literal. I.E.: 8.8 --> 8.8f` Commented Sep 11, 2021 at 16:08

1 Answer 1

3

Your format is wrong:

characterGrade has type char and you want to print it using %s format which requires char * parameter. It has to be %c instead:

"%s passed out of college in %d and had a grade of %c i.e. a score of %f\n"
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.