Home > Software engineering >  printf not printing integer stored in variable
printf not printing integer stored in variable

Time:10-20

For some weird reason when I input the values in the marks section, the printed values are all 0, please help, I tried running a smaller version of the code and it worked I don't know why this doesn't. here is the code: Stack overflow keeps giving me an error since apparently most of my question is code so pls ignore this

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

int main()
{
    int marksComm;
    char gradeComm[1];
    int marksEco;
    char gradeEco[1];
    int marksCompsys;
    char gradeCompsys[1];
    int marksProg;
    char gradeProg[1];
    int marksDis;
    char gradeDis[1];
    int marksLab;
    char gradeLab[1];
    int marksPhy;
    char gradePhy[1];
    printf("CCS001\nMarks> ");
    scanf("%d", &marksComm);
    printf("Grade> ");
    scanf("%s", &gradeComm);

    printf("\nCCS009\nMarks> ");
    scanf("%d", &marksEco);
    printf("Grade> ");
    scanf("%s", &gradeEco);

    printf("\nCSC111\nMarks> ");
    scanf("%d", &marksCompsys);
    printf("Grade> ");
    scanf("%s", &gradeCompsys);

    printf("\nCSC112\nMarks> ");
    scanf("%d", &marksProg);
    printf("Grade> ");
    scanf("%s", &gradeProg);

    printf("\nCSC113\nMarks> ");
    scanf("%d", &marksDis);
    printf("Grade> ");
    scanf("%s", &gradeDis);

    printf("\nCSC126\nMarks> ");
    scanf("%d", &marksPhy);
    printf("Grade> ");
    scanf("%s", &gradePhy);

    printf("\nCSC115\nMarks> ");
    scanf("%d", &marksLab);
    printf("Grade> ");
    scanf("%s", &gradeLab);

    printf("\nCourse Code       Marks      Grade\n");
    printf("CCS001             %d         %s \n", marksComm, gradeComm);
    printf("CCS009             %d         %s \n", marksEco, gradeEco);
    printf("CSC111             %d         %s \n", marksCompsys, gradeCompsys);
    printf("CSC112             %d         %s \n", marksProg, gradeProg);
    printf("CSC113             %d         %s \n", marksDis, gradeDis);
    printf("CSC115             %d         %s \n", marksLab, gradeLab);
    printf("CSC126             %d         %s \n", marksPhy, gradePhy);
}

CodePudding user response:

Your arrays are two small. C strings are NUL-terminated, so you need a char for the letter, and a char for the NUL. That's two char. But you reserve space for one.

CodePudding user response:

Strings must be at least null-terminated

  • Related