Press "Enter" to skip to content

Basic Programs in C

Basic Programs in C

This is simple code which helps you to write C programs.

 

Single level functions

The following function is a simple function of one level:

main()
 {
       /*print a message*/
    printf(“Welcome to w3htmlschool.com”);
 }

Let’s understand each line of code in the above code.

main() The first line search by the c compiler is the main() function in the c program ,main() is always the first function to be executed by the computer and should be present in all programs. It is like any other function available in the the C library or like one developed by a user.

Parenthesis () are used for passing parameters to functions.

{,} mark the beginning and end of a function and is only for documenting
various parts of a function. The comment line may be in a seperate line or
part of a line . The comment line is optional .

; is used for marking the end of an executable line.

Also , in the above function printf () is a C function for printing constant or variable data and “Welcome to w3htmlschool.com” is the parameters passed to it .

Multiple level functions in C Programming Language

The following is an example showing functions at multiple levels- one being called by another

 main()
{
/*print a message*/
Printf (“welcome to C”);

disp_msg()
printf (“for good learning”);
}
disp_msg()
{
/*print another message*/
printf(‘All the best”);
}

 

Output :
Welcome to C

Here,
main() is a function written by programmers but has to be called main() since it is the first function to be executed.

printf() is a function provided by C

disp_msg() is a programmer defined function that can be independent called by any other function.

(), used for passing values for functions , may or may not contain any values , depending on whether the receiving function is expecting any parameter.


All executable lines are terminated by a semi- colon(;) . In case of functions, a statement calling/executing a function needs to be terminated by a semi – colon, but a statement marking the beginning of a function definition does not have a semi- colon at the end .