C Programming basic program with explanation

C Programming Exercise

C PROGRAMMING BASIC EXAMPLE AND SOLUTION

A C program is divided into five sections:

  • Preprocessor Statements
  • Functions
  • Variables
  • Statements & Expressions
  • Comments
Example: Write a program simple program.
				
					#include<stdio.h>

int main()
{

    int n;
   printf("enter the number:);
   scanf("%d",&n);
   
   /* simple c program output */
   printf("number= %d",n);
return 0;
}
				
			

1. Preprocessor statement

All header files are included in this section which contains different functions from the libraries. A copy of these header files is inserted into your code before compilation.

 

A header file is a file that consists of C declarations that can be used between different files. It helps us in using others’ code in our files. A copy of these header files is inserted into your code before compilation.

 
				
					#include<stdio.h>
				
			

2. Main( ) function

In the structure of a C program, this section contains the main function of the code. The C compiler starts execution from the main() function.

 It can use global variables, static variables, inbuilt functions, and user-defined functions. The return type of the main() function can be void and also not necessarily int.

 
				
					#include<stdio.h>
    int main()
				
			

3. Variables

In simple words, variable is a name given to memory box with a name, where we can “store” some value. Its value can be changed depending upon conditions and it can be reused many times.

  • A variable can support different type of data such as integer, float, character etc
  • Value of variables can be changed according to the information passed to the program.
  • You can perform different set of operation to the variables.

Rules for naming C Variable

  • A Variable name must begin with letter or underscore.
  • Variables are case sensitive
  • You can use letters & digits in variable names.
  • No special symbols can be used other than underscore.
  • Variable  Should not be Reserved Word.
Declaring & initializing Variable
S.NoTypeSyntaxExample
1.Variable declarationdata_type variable_name;int x, y, z; char ch;
2.Variable initializationdata_type variable_name = value;int x = 10, y = 20; ch=’l’;
				
					#include<stdio.h>
    int main()
    {
      int n;
				
			

4. Statements and Expressions

The printf() function is used to display output and the scanf() function is used to take input from users.

The printf() and scanf() functions are commonly used functions in C Language. These functions are inbuilt library functions in header files of C programming.


1. Printf() Function

In C Programming language, the printf() function is used for output.

printf() function can take any number of arguments. First argument must be enclosed within the double quotes “hello” and every other argument should be separated by comma ( , ) within the double quotes.

				
					#include<stdio.h>
    int main()
    {
       int n;
       printf("Enter the number:");
       
				
			

scanf() Function

The scanf() function is used to read input data from the console.

The scanf() function is builtin function available in the C library. scanf() function can read character, string, numeric & other data from keyboard in C language.

scanf() reads formatted data from user and assign them in the variables provided the additional arguments. Additional arguments must point to variables that have the same datatype as of user input data format.

				
					#include<stdio.h>
    int main()
    {
       int n;
       printf("Enter the number:");
       scanf("%d",&n);
				
			

5. Comment

Comments are the non-executable code used to provide documentation for the C program. Comments provide details about the code so that other users can easily understand the code by reading those comments.

				
					#include<stdio.h>

int main()
{

    int n;
   printf("enter the number:);
   scanf("%d",&n);
   
   /* simple c program output */

				
			

Description of the sections in the above C program

#include <stdio.h>This is a preprocessor statement that includes standard input output header file (stdio.h) from the C library. By including header file, you can use many different functions such as printf()
void main()The execution of the C program starts with main() function. “void” is the return type of the function. Every C program can have only one main function.
Braces {}Two curly brackets {} are used to group all code statements. This indicates begins & ends of the main function.
/* Comments */Comments are just used to document or explain the code, to help others understand it. Everything written inside the command /*  */ will be ignored by the compiler.
printf()printf() is a function in C, which prints the text output on the screen.
getch()This is used to read any character input from the keyboard.
Spread the love