Basic Structure of C program

C basic structure Like any other programming language, C also maintains its own system to execute the code.

There is no starting or ending marks used in C code. So all codes are written at one place without embedding with any other code.


How to write comment

Comment part of the program is not executed by the compiler.
Comment is writtin by using two forward slashs ( // )
Learn more on comments here
We will start with writing a program to display a string Hello World
#include<stdio.h>
#include<string.h>

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

#include<stdio.h>
#include<string.h>

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<stdio.h>
#include<string.h>

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<stdio.h>
#include<string.h>

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<stdio.h>
#include<math.h>
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); //the string
   return 0;
}
Output is here
Sum of three number = 14.000000
Average of three numbers = 4.67
Data entered by user
#include<stdio.h>
#include<math.h>
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);//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

Subscribe

* indicates required
Subscribe to plus2net

    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-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer