Why does my codeblock work in main() but not in its own function?












-2















A block of code produces the expected results when placed directly within the body of main(), but not when split off into its own function and called from main().



This is my first real try at C programming. As an exercise, I figured I'd try using ncurses to get an intro screen with centered text. Nice and simple, ncurses did the trick since printf isn't really capable of it.



So, I figure the next step would be to compartmentalize it within its own function as a first step to splitting it off into a separate .c file. I figure this would be a good way to practice splitting up code and referencing via header includes with the prototype in a .h file. Well, I never got that far. The code block simply doesn't do anything when compiled and run as its own function.



By "doesn't do anything" I mean that when I run the compiled program, nothing appears on the screen and I simply get the prompt again.



This is the version that produces the correct results:



#include <ncurses.h>
#include <string.h>

int main()
{

char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();

return 0;

}


...and the version that does not:



#include <ncurses.h>
#include <string.h>

void intro();

void main()
{
void intro();
}

void intro()
{
char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();
}









share|improve this question


















  • 2





    Inside main() this void intro(); looks like declaration which you already did above main(). Change it to intro(); And since intro doesn't expect any input argument, this void intro(void) looks proper rather than void intro();

    – Achal
    Dec 30 '18 at 9:15













  • That did the trick. Thank you.

    – senrew
    Dec 30 '18 at 9:19











  • Up to you, but I suggest spacing your code a bit more (helpful for older eyes), e.g. char mesg1 = "Space Tycoon"; or getmaxyx (stdscr, row, col); etc...

    – David C. Rankin
    Dec 30 '18 at 9:37
















-2















A block of code produces the expected results when placed directly within the body of main(), but not when split off into its own function and called from main().



This is my first real try at C programming. As an exercise, I figured I'd try using ncurses to get an intro screen with centered text. Nice and simple, ncurses did the trick since printf isn't really capable of it.



So, I figure the next step would be to compartmentalize it within its own function as a first step to splitting it off into a separate .c file. I figure this would be a good way to practice splitting up code and referencing via header includes with the prototype in a .h file. Well, I never got that far. The code block simply doesn't do anything when compiled and run as its own function.



By "doesn't do anything" I mean that when I run the compiled program, nothing appears on the screen and I simply get the prompt again.



This is the version that produces the correct results:



#include <ncurses.h>
#include <string.h>

int main()
{

char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();

return 0;

}


...and the version that does not:



#include <ncurses.h>
#include <string.h>

void intro();

void main()
{
void intro();
}

void intro()
{
char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();
}









share|improve this question


















  • 2





    Inside main() this void intro(); looks like declaration which you already did above main(). Change it to intro(); And since intro doesn't expect any input argument, this void intro(void) looks proper rather than void intro();

    – Achal
    Dec 30 '18 at 9:15













  • That did the trick. Thank you.

    – senrew
    Dec 30 '18 at 9:19











  • Up to you, but I suggest spacing your code a bit more (helpful for older eyes), e.g. char mesg1 = "Space Tycoon"; or getmaxyx (stdscr, row, col); etc...

    – David C. Rankin
    Dec 30 '18 at 9:37














-2












-2








-2








A block of code produces the expected results when placed directly within the body of main(), but not when split off into its own function and called from main().



This is my first real try at C programming. As an exercise, I figured I'd try using ncurses to get an intro screen with centered text. Nice and simple, ncurses did the trick since printf isn't really capable of it.



So, I figure the next step would be to compartmentalize it within its own function as a first step to splitting it off into a separate .c file. I figure this would be a good way to practice splitting up code and referencing via header includes with the prototype in a .h file. Well, I never got that far. The code block simply doesn't do anything when compiled and run as its own function.



By "doesn't do anything" I mean that when I run the compiled program, nothing appears on the screen and I simply get the prompt again.



This is the version that produces the correct results:



#include <ncurses.h>
#include <string.h>

int main()
{

char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();

return 0;

}


...and the version that does not:



#include <ncurses.h>
#include <string.h>

void intro();

void main()
{
void intro();
}

void intro()
{
char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();
}









share|improve this question














A block of code produces the expected results when placed directly within the body of main(), but not when split off into its own function and called from main().



This is my first real try at C programming. As an exercise, I figured I'd try using ncurses to get an intro screen with centered text. Nice and simple, ncurses did the trick since printf isn't really capable of it.



So, I figure the next step would be to compartmentalize it within its own function as a first step to splitting it off into a separate .c file. I figure this would be a good way to practice splitting up code and referencing via header includes with the prototype in a .h file. Well, I never got that far. The code block simply doesn't do anything when compiled and run as its own function.



By "doesn't do anything" I mean that when I run the compiled program, nothing appears on the screen and I simply get the prompt again.



This is the version that produces the correct results:



#include <ncurses.h>
#include <string.h>

int main()
{

char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();

return 0;

}


...and the version that does not:



#include <ncurses.h>
#include <string.h>

void intro();

void main()
{
void intro();
}

void intro()
{
char mesg1="Space Tycoon";
char mesg3="Press Any Key To Continue";
int row,col;
initscr();
getmaxyx(stdscr,row,col);
mvprintw(row/2-1,(col-strlen(mesg1))/2,"%s",mesg1);
mvprintw(row/2+5,(col-strlen(mesg3))/2,"%s",mesg3);
refresh();
getch();
endwin();
}






c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 9:13









senrewsenrew

12




12








  • 2





    Inside main() this void intro(); looks like declaration which you already did above main(). Change it to intro(); And since intro doesn't expect any input argument, this void intro(void) looks proper rather than void intro();

    – Achal
    Dec 30 '18 at 9:15













  • That did the trick. Thank you.

    – senrew
    Dec 30 '18 at 9:19











  • Up to you, but I suggest spacing your code a bit more (helpful for older eyes), e.g. char mesg1 = "Space Tycoon"; or getmaxyx (stdscr, row, col); etc...

    – David C. Rankin
    Dec 30 '18 at 9:37














  • 2





    Inside main() this void intro(); looks like declaration which you already did above main(). Change it to intro(); And since intro doesn't expect any input argument, this void intro(void) looks proper rather than void intro();

    – Achal
    Dec 30 '18 at 9:15













  • That did the trick. Thank you.

    – senrew
    Dec 30 '18 at 9:19











  • Up to you, but I suggest spacing your code a bit more (helpful for older eyes), e.g. char mesg1 = "Space Tycoon"; or getmaxyx (stdscr, row, col); etc...

    – David C. Rankin
    Dec 30 '18 at 9:37








2




2





Inside main() this void intro(); looks like declaration which you already did above main(). Change it to intro(); And since intro doesn't expect any input argument, this void intro(void) looks proper rather than void intro();

– Achal
Dec 30 '18 at 9:15







Inside main() this void intro(); looks like declaration which you already did above main(). Change it to intro(); And since intro doesn't expect any input argument, this void intro(void) looks proper rather than void intro();

– Achal
Dec 30 '18 at 9:15















That did the trick. Thank you.

– senrew
Dec 30 '18 at 9:19





That did the trick. Thank you.

– senrew
Dec 30 '18 at 9:19













Up to you, but I suggest spacing your code a bit more (helpful for older eyes), e.g. char mesg1 = "Space Tycoon"; or getmaxyx (stdscr, row, col); etc...

– David C. Rankin
Dec 30 '18 at 9:37





Up to you, but I suggest spacing your code a bit more (helpful for older eyes), e.g. char mesg1 = "Space Tycoon"; or getmaxyx (stdscr, row, col); etc...

– David C. Rankin
Dec 30 '18 at 9:37












1 Answer
1






active

oldest

votes


















1














int main(){
intro(); // not void intro()
}


because you want to call the intro function from your main. If you code void intro(); you just are declaring (see C11 §6.7.6.3) inside main that intro function (and then you'll better give its signature, e.g. write void intro(void);).



BTW your main has to return int. See C11 specification n1570 §5.1.2.2.1



Look also into some C reference site.






share|improve this answer


























  • Again, thanks. It feels good to finally know why this hasn't worked.

    – senrew
    Dec 30 '18 at 9:34











  • See C11 Standard - 5.1.2.2.3 Program termination - if the closing '}' is reached before a return n', 0 is returned by default (C99+).

    – David C. Rankin
    Dec 30 '18 at 9:35











  • @DavidC.Rankin That's about the missing return statement. The return type should still be int instead of void.

    – Gerhardh
    Dec 30 '18 at 12:18











  • @Gerhardh Right you are, unless he is coding on an embedded (freestanding) system without an OS, it's all implementation defined there. See C11 Standard - 5.1.2.1 Freestanding environment

    – David C. Rankin
    Dec 30 '18 at 12:21













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976427%2fwhy-does-my-codeblock-work-in-main-but-not-in-its-own-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














int main(){
intro(); // not void intro()
}


because you want to call the intro function from your main. If you code void intro(); you just are declaring (see C11 §6.7.6.3) inside main that intro function (and then you'll better give its signature, e.g. write void intro(void);).



BTW your main has to return int. See C11 specification n1570 §5.1.2.2.1



Look also into some C reference site.






share|improve this answer


























  • Again, thanks. It feels good to finally know why this hasn't worked.

    – senrew
    Dec 30 '18 at 9:34











  • See C11 Standard - 5.1.2.2.3 Program termination - if the closing '}' is reached before a return n', 0 is returned by default (C99+).

    – David C. Rankin
    Dec 30 '18 at 9:35











  • @DavidC.Rankin That's about the missing return statement. The return type should still be int instead of void.

    – Gerhardh
    Dec 30 '18 at 12:18











  • @Gerhardh Right you are, unless he is coding on an embedded (freestanding) system without an OS, it's all implementation defined there. See C11 Standard - 5.1.2.1 Freestanding environment

    – David C. Rankin
    Dec 30 '18 at 12:21


















1














int main(){
intro(); // not void intro()
}


because you want to call the intro function from your main. If you code void intro(); you just are declaring (see C11 §6.7.6.3) inside main that intro function (and then you'll better give its signature, e.g. write void intro(void);).



BTW your main has to return int. See C11 specification n1570 §5.1.2.2.1



Look also into some C reference site.






share|improve this answer


























  • Again, thanks. It feels good to finally know why this hasn't worked.

    – senrew
    Dec 30 '18 at 9:34











  • See C11 Standard - 5.1.2.2.3 Program termination - if the closing '}' is reached before a return n', 0 is returned by default (C99+).

    – David C. Rankin
    Dec 30 '18 at 9:35











  • @DavidC.Rankin That's about the missing return statement. The return type should still be int instead of void.

    – Gerhardh
    Dec 30 '18 at 12:18











  • @Gerhardh Right you are, unless he is coding on an embedded (freestanding) system without an OS, it's all implementation defined there. See C11 Standard - 5.1.2.1 Freestanding environment

    – David C. Rankin
    Dec 30 '18 at 12:21
















1












1








1







int main(){
intro(); // not void intro()
}


because you want to call the intro function from your main. If you code void intro(); you just are declaring (see C11 §6.7.6.3) inside main that intro function (and then you'll better give its signature, e.g. write void intro(void);).



BTW your main has to return int. See C11 specification n1570 §5.1.2.2.1



Look also into some C reference site.






share|improve this answer















int main(){
intro(); // not void intro()
}


because you want to call the intro function from your main. If you code void intro(); you just are declaring (see C11 §6.7.6.3) inside main that intro function (and then you'll better give its signature, e.g. write void intro(void);).



BTW your main has to return int. See C11 specification n1570 §5.1.2.2.1



Look also into some C reference site.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 30 '18 at 9:27









Basile Starynkevitch

177k13167364




177k13167364










answered Dec 30 '18 at 9:19









SpinkooSpinkoo

77513




77513













  • Again, thanks. It feels good to finally know why this hasn't worked.

    – senrew
    Dec 30 '18 at 9:34











  • See C11 Standard - 5.1.2.2.3 Program termination - if the closing '}' is reached before a return n', 0 is returned by default (C99+).

    – David C. Rankin
    Dec 30 '18 at 9:35











  • @DavidC.Rankin That's about the missing return statement. The return type should still be int instead of void.

    – Gerhardh
    Dec 30 '18 at 12:18











  • @Gerhardh Right you are, unless he is coding on an embedded (freestanding) system without an OS, it's all implementation defined there. See C11 Standard - 5.1.2.1 Freestanding environment

    – David C. Rankin
    Dec 30 '18 at 12:21





















  • Again, thanks. It feels good to finally know why this hasn't worked.

    – senrew
    Dec 30 '18 at 9:34











  • See C11 Standard - 5.1.2.2.3 Program termination - if the closing '}' is reached before a return n', 0 is returned by default (C99+).

    – David C. Rankin
    Dec 30 '18 at 9:35











  • @DavidC.Rankin That's about the missing return statement. The return type should still be int instead of void.

    – Gerhardh
    Dec 30 '18 at 12:18











  • @Gerhardh Right you are, unless he is coding on an embedded (freestanding) system without an OS, it's all implementation defined there. See C11 Standard - 5.1.2.1 Freestanding environment

    – David C. Rankin
    Dec 30 '18 at 12:21



















Again, thanks. It feels good to finally know why this hasn't worked.

– senrew
Dec 30 '18 at 9:34





Again, thanks. It feels good to finally know why this hasn't worked.

– senrew
Dec 30 '18 at 9:34













See C11 Standard - 5.1.2.2.3 Program termination - if the closing '}' is reached before a return n', 0 is returned by default (C99+).

– David C. Rankin
Dec 30 '18 at 9:35





See C11 Standard - 5.1.2.2.3 Program termination - if the closing '}' is reached before a return n', 0 is returned by default (C99+).

– David C. Rankin
Dec 30 '18 at 9:35













@DavidC.Rankin That's about the missing return statement. The return type should still be int instead of void.

– Gerhardh
Dec 30 '18 at 12:18





@DavidC.Rankin That's about the missing return statement. The return type should still be int instead of void.

– Gerhardh
Dec 30 '18 at 12:18













@Gerhardh Right you are, unless he is coding on an embedded (freestanding) system without an OS, it's all implementation defined there. See C11 Standard - 5.1.2.1 Freestanding environment

– David C. Rankin
Dec 30 '18 at 12:21







@Gerhardh Right you are, unless he is coding on an embedded (freestanding) system without an OS, it's all implementation defined there. See C11 Standard - 5.1.2.1 Freestanding environment

– David C. Rankin
Dec 30 '18 at 12:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53976427%2fwhy-does-my-codeblock-work-in-main-but-not-in-its-own-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas