Home > database >  Why is Cod::Blocks giving me Null instead of a input char?
Why is Cod::Blocks giving me Null instead of a input char?

Time:12-10

Source Code:

#3 main.c

# include "func.h"

int main(void) {
    func();
    return 0;
}

#3 func.h

#include <stdio.h>

void inputName();
void printName();
void func();

#3 func.c

#include "func.h"

char GLOBAL_NAME;

void inputName() {
    scanf("%s", &GLOBAL_NAME);
}

void printName() {
    printf("Your name is: %s.\n", &GLOBAL_NAME);
}

void func(void) {
    inputName();
    printName();
}

Out Put:

Your name is: (null).

I used https://www.online-cpp.com/online_c_compiler with the same code, it works fine on the online compiler. but when I try to use it on Code::Blocks it shows me:

Your name is: (null).

Don't know what's the problem, Could it be a compiler thing?

I'm using a windows machine for Code::Blocks using GCC I think as the compiler.

CodePudding user response:

Initialize your char variable with a length and since you have not initialized it with a length it returns NULL.

char GLOBAL_NAME[30];

CodePudding user response:

My Answer is simple I Have no idea how the heck Code::Blocks works. I've always wrote my code on Linux through the terminal: with nano File.c and gcc File.c -o File && ./File.

-In other words there's three buttons to "run" a program in Code::Blocks; one to build it, one to re-run it, and one to build and run the latest build. I hate IDEs so much. :D -I was clicking the re-run, instead of the build and run latest. User Error

  • Related