Basic Structure of C program

C basic structure Each instruction in C are written in a statement.
Each statement ends with ;
One C program consist of a group of statements
Program is executed in a sequence from top to bottom. Based on logic or requirement steps can be shifted in up or down direction. Small case letters are used and space is used between two commands.
We must declare a variable before using it.

How to write comment

This is part of the program which is not executed by the compiler.
Learn more on comments here
We will start with writing a program to display a string Hello World
#include
#include

int main()
{
   char str[30] = "Hello World"; // Storing string data
   printf("%s",str); // Print the string
   return 0;
}
The output of above code is here
Hello Wold
We have first included to header files

#include
#include

First one is to include standard input output and other one for string functions. We will understand these header files later. Functions are written in C with two brackets, main() function is the function which C compiler first executes. We will also learn how to develop our own functions and use them in our program.

We declare variable str and assigned 30 cells ( memory ) to it. We stored our string ‘hello world’ inside it and finally our function printf() display the output to screen.

Reading a string and then display

In the above code, we have assigned string data to the variable, now we will ask the user to input a string data and then we will display the same.
#include
#include

int main()
{
   char str[30];//
   printf("Enter your name ");
   scanf("%s", str);
   printf("Welcome %30s",str); // Print the string
   return 0;
}
scanf()

Sum of two integers

#include
#include

int main()
{
   int a,b,c;// Declaring three variables
   a=4;  // assigning data
   b=5;
   c= a + b;
   printf("Sum = %d", c); // Print the string
   return 0;
}
Output
Sum = 9

Sum and average of three numbers

#include
#include
int main()
{
   int a,b,c;// Declaring three variables
   float x,avg;
   a=3;  // assigning data
   b=5;
   c=6;
   x= (a + b + c);
   avg=x/3;
   printf("Sum of  three numbers  = %f", x); // Print the string
   printf("\n Average of three numbers  = %.2f", avg); // Print the string
   return 0;
}
Output is here
Sum of three number = 14.000000
Average of three numbers = 4.67
Data entered by user
#include
#include
int main()
{
   int a,b,c;// Declaring three variables
   float x,avg;
   printf("enter first number :");
   scanf("%d",&a);
   printf("enter second number :");
   scanf("%d",&b);
   printf("enter third number :");
   scanf("%d",&c);
   x= (a + b + c);
   avg=x/3;
   printf("Sum of  three numbers  = %f", x); // Print the string
   printf("\n Average of three numbers  = %.2f", avg); // Print the string
   return 0;
}
Output is here
enter first number : 4
enter second number : 5
enter third number : 6
Sum of three numbers = 15
Average of three numbers = 3


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer