Home » Programming Tips & Tricks » C - Tips & Tricks

Declaring a function within the main() function in C language

By: IncludeHelp, on 03 MAR 2017

Learn: How to declare a function within main() function and how to define it outside of main().

In the previous post [Correct way to declare and define a function in C], I have discussed a function should be declared before the main() function, but we can also declare a function within the main() function.

Remember: Function can be declared within the main() only. But, function definition should be outside of the main(), after the main() function body.

Consider this example

#include <stdio.h>

int main()
{
	//function declaration
	void printText(void);
	
	printf("I am going to call the function...\n");
	printText();
	printf("Bye bye!!!\n");
	
	return 0;
	
}

//function definition
void printText(void)
{
	printf("Hello friends\n");
}

Output

I am going to call the function...
Hello friends 
Bye bye!!!





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.