Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C program hangs...

deicool

Coder
Hello

I am a newbie.

My following program hangs when I run it:

C:
#include <stdio.h>

char* myName() {
    /*
    char name1[20];
    scanf("%s", name1);
    printf("%s", name1);
    return name1;
    */

    char *name = "Flavio";
    scanf("%s", name);
    printf("%s", name);
    return name;
}

int main(void) {

  char name[20];
  scanf("%s", name);
  printf("%s", name);
  printf("%s", myName());

}

Where could I be going wrong?

Please advise.

Thanks.
 
Hello there,

I don't do a lot of programming now so don't take everything I say here as truth - I'm making errors here and I know I am.

C:
#include <stdio.h>                                                                                                                                                                  
                                                                                                                                                                                    
char myName(char *name) {                                                                                                                                                           
    scanf("%s", name);                                                                                                                                                              
    printf("%s\n", name);                                                                                                                                                           
    return *name;                                                                                                                                                                   
}                                                                                                                                                                                   
                                                                                                                                                                                    
int main(void) {                                                                                                                                                                    
                                                                                                                                                                                    
  char *name;                                                                                                                                                                       
  myName(&name);                                                                                                                                                                    
                                                                                                                                                                                    
  return 0;                                                                                                                                                                         
}
I have cleaned up your code. To take note of some changes:

  • I've removed the pointer(*) symbol from the function type for myName()[/CODE] and made it so that an argument is passed to it, rather than no argument. [*]Got rid of your print and scan calls in the main function, which aren't needed when you already have them in the other function. [*]Also added [ICODE]return 0; - that is a must at the end of every main function you write. To simply put it, it's your program exit statement(there's more to it than that obviously).
  • Got rid of the commented out code(why was it there?)
  • Also got rid of *name = "Flavio" - I don't understand the purpose of this when you're going to be typing in a new string to replace it.

I hate to say it, but this isn't really good programming. You've been a bit careless with how you've used pointers(which MUST BE USED CAREFULLY) and the code just doesn't make sense until you rid of the unnecessary and duplicated parts. The reason your code is hanging is, from what I believe it to be anyway, due to your careless use of pointers.

Pointers can be the thing that brings an entire program down if used in the wrong way. If I were you, I would focus on writing basic programs without them just now, and once you've practiced for a couple of weeks, then begin to incorporate them into your programs because at that point, you'll have a better understanding of how C works.

As some tips to improve:
  • Comment your code so that others can read what it does.
  • Pick up a copy of the K&R C book(if you can afford it or instead, look for it in a library). It's a must for any C programmer and covers everything you need to know about the language, and it showcases good practice.

Hope this helps!
 
Hello @BorkedSystem32,

Btw, i am getting the following error when I run the code written by you:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
D:\Deepak\C\Test\stringReturn1.c.cpp||In function 'int main()':|
D:\Deepak\C\Test\stringReturn1.c.cpp|12|error: cannot convert 'char**' to 'char*'|
D:\Deepak\C\Test\stringReturn1.c.cpp|3|note: initializing argument 1 of 'char myName(char*)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
 
Thank you so much for giving me a direction. I will be careful using pointers. Also will pick up the book mentioned by you.
You're very welcome. C is a good language, just some parts of it you'll find tricky, so that's why I recommend that you take your time, and also pick up the K&R book because it goes in-depth as to how it works(not too in-depth though, that you'll understand how everything works underneath the hood).

In regards to your error, can I ask what compiler you're using? I'm using GCC 9.4 and it ran fine.
 
Thanks once again. Here is the version I am using:

gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
Okay. I use GCC on Linux which is all setup for me once the OS is installed; since it's Windows, you'll need to check your environment variables to make sure that the compiler runs properly.

If you haven't got it configured to an environment variable, follow this guide and scroll down until you get to the section on environment variables.

If you've already got that done, then great!

Looking at the error message you posted, since it contains the words "build file", I'll assume you're trying to build the program into an executable, yes? If so, don't bother building it. Just compile and run it. You only need to build for large projects; if it's a small program like this, it's enough to just compile it and run it then.
gcc file.c -o file
This will compile the file(use whatever the name of the file is called) and create an output file with whatever name you give it(if you don't use the output argument -o, you will get an automatic one titled a.out).

Hope this helps!
 
Thank You.

I will try on Linux too.

As other programs are running (dozens of them), I doubt it is because of environment variable (btw, i am using CodeBlocks 20.03 IDE).

Will try this out as well (gcc file.c -o file)

Thanks.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom