简体   繁体   中英

C Hello World Program in a subroutine

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

void message(char m)
{
print("Hello\n");
}

int main()
{
message(m);    
}

Error message when I try to compile

danielc@Ubuntu11:$ gcc Messagef.c -o Messagef
    Messagef.c: In function ‘main’:
    Messagef.c:11:9: error: ‘m’ undeclared (first use in this function)
    Messagef.c:11:9: note: each undeclared identifier is reported only once for each function it appears in

I know that am doing a 'silly' mistake but I just see where am going wrong

Your function takes a char parameter but never uses it. The simplest fix is to remove the unused parameter:

#include <stdio.h>

void message()
{
    printf("Hello\n");
}

int main()
{
    message();    
    return 0;
}

Alternatively, change your method to use the parameter, and pass in a character as an argument:

#include <stdio.h>

void message(char m)
{
    printf("Hello%c\n", m);
}

int main()
{
    message('!');    
    return 0;
}

See it working online: ideone

  1. Declare m in your main (char m = '?';)
  2. Try "printf" instead of "print"

the variable "m" your passing to the message function has not been defined before its passed.

define the m variable above message() or pass a char literal to the function

Your function expects a char and you are passing m without declaring it. You need to declare m first like this:

char m = 'a';

And then call the function. BTW, you are not doing anything with this variable so it is redundant anyway.

Pick up a book of C language and start following that.

alternately u can initialise m with Hello message. pass the pointer to message to function and then print in message function, somewhat like this:

void message(char *msg)
{
printf("%s", msg);
}

int main()
{
char *m = "Hello";
message(m);
return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM