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 .

C Varibale Naming Convensions

In C programming, variables are used to store data of various types. When naming variables in C, there are some conventions that programmers follow to make their code more readable and understandable. Here are some naming conventions for C variables:

  1. Use meaningful names:

Variable names should be descriptive and convey the purpose of the variable. Use names that describe the data stored in the variable, such as “age”, “salary”, “name”, etc. Avoid using short or abbreviated names, as they may not be meaningful to other programmers.

  1. Use lowercase letters:

Variable names should always start with a lowercase letter. This makes it easier to distinguish between variables and functions, which usually start with an uppercase letter.

  1. Use underscores for multi-word names:

If a variable name consists of multiple words, use underscores to separate the words. This makes the variable name easier to read and understand. For example, “student_name”, “total_marks”, etc.

  1. Avoid using reserved words:

Do not use reserved words or keywords as variable names. Reserved words are words that have a special meaning in the C language and cannot be used as variable names. Examples of reserved words include “int”, “float”, “char”, “if”, “while”, etc.

  1. Use camel case for structure and typedef names:

For structure and typedef names, use camel case naming convention. Camel case is a naming convention in which the first letter of each word is capitalized except for the first word. For example, “personInfo”, “employeeRecord”, etc.

  1. Use constants for fixed values:

For values that do not change, such as pi or the number of days in a week, use constants instead of variables. Constants are declared using the #define preprocessor directive and are named in all uppercase letters. For example, #define PI 3.14159, #define DAYS_IN_WEEK 7