Introduction to C Program.
Hello Everyone,
Today I will talk about basics of C language. In previous Blogs I almost completed SQL and PL/SQL parts will add more topics on DBA part. Previous Blogs you can see in my below blog where in archive you will able to see all the topics.
dheeraj60.blogspot.com
So Let’s Start with Basic Concept of C.
C is a most Popular Language in Computer Science as so many software’s built by using C language like Microsoft Windows , Notepad , calculator , Games etc. C programming language is must for students and working professionals to become a good Software Engineer especially when they are working in Software Development Projects. Key Advantage of C language is as below:
Structured Language
Very Easy to Learn
Produces Efficient Programs
C program can be compiled on variety of computer platforms
C Program can handle low-level activities
Note: In Today's world most popular RDBMS Linux and OS have been written in C. C is also known as mother of all languages.
Before Starting To C Program we need to understand few important things:
As we Know Computer only understand Machine Language that is binary digit 0 and 1 in other word we can say computer can only understand Machine language (Stream of 0s and 1s). So to convert our C program source code to Machine code, we need to compile it. So we use Compiler, which converts source code to Machine code. In other words we can say that a compiler converts human written code to a machine readable format. So we need compiler to write C program which used to convert human readable code to machine readable format.
For this we have several compilers available, so you need to install Turbo, code block etc. Code Block you can easily install as it’s freeware and you can write starting code in C program.
Once you install any compiler you are able to write C program that will convert C program source code to Machine code.
So let’s start writing some simple program in C to display my name and understand how it works and what are the syntax, keywords used in that program.
Note : Later we need to know more about data type and other keywords which used in C program. Here I am using code block as compiler
First C Program to Display name:
Syntax is as below :
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello welcome to Diksha Oracle Training Centre !\n");
return 0;
}
Once you compile the code and there is no Syntax error then output will be as below:
Hello welcome to Diksha Oracle Training Centre!
Let’s understand the Syntax for this program because if you want to write another code it’s useful for you to understand how it works after that I will show you the source code and output of this program in image. So please try to understand each syntax and keywords one by one.
#include <stdio.h> -- stdio.h is the header file in C Program used for standard input and output. This is useful for getting the input from the user by using Keyboard and output result to the monitor/screen. Without header file, we can not able to display the results to the users on the screen or cannot input the values through the keyboard . Stdio.h is contain the input and output operations like "printf" , "scanf" etc. print f used to display the output whereas scanf used to take input from the user .
#include <stdlib.h> -- stdlib stands for "standard library” whereas .h is header . stdlib.h is the header used for general purpose standard library of C programming language which includes functions involving memory allocation, process control, conversions and others.
int main() -- int main is a function that means that our function needs to return some integer so at the end of the execution and we do it by returning 0 at the end of the program. 0 is the standard for the successful execution of the program. In some compiler they used void main() so no need to worry as both are same. Return type for main function default is integer.
Printf – To print the value
return 0 -- Since we use int main so we need to end the program by using return 0;
This is the basic Syntax and it’s definition for this program as you should know about this because when you write some other code it’s easy for you to write the program. Yes we need to know some other things as well when we write other code as per our requirement.
The output of above program image in code block is as below:
As we know about the basic Syntax and it’s uses we can write some other basic programs by using data types.
The Sum of two numbers Program in C . Here in these program we only use data type int that is integer will explain the code after running the below code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a , b ,c ;
a=10;
b=20;
c=a+b;
printf("The Sum of 2 number is !\n %d",c);
return 0;
}
Here in this program we use int as integer data type to take only numeric value . we take a , b and c as integer and assigned the value of a and b is 10 and 20 and in c we simply use addition to print the sum of two number that is a and b .
Note : in printf we use %d for taking the value for output in integer in other word we can say %d is used to display integer type of value by using printf function and \n means it will go to new line . There is several keywords for anther data types that we will talk later when we write different programs.
The output of above program with source code and output in code block is as below :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a , b ,c ;
printf("please enter two number \n");
scanf("%d",&a);
scanf("%d",&b);
c=a+b;
printf("The Sum of 2 number is !\n %d",c);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a , b ,c ,d ,e ;
printf("please enter four number \n");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%d",&d);
e=a+b+c+d;
printf("The Sum of 4 number is !\n %d",e);
return 0;
}
Here we simply initialize 5 variables i.e a,b,c,d and e to get the sum of four numbers.
Output of the above Program with source code in Code block is as below :
Things to remember:
When we initialize any integer we need to close each value with semicolon that is ; same applied for input function scanf() and print f() .
Today we only came to know about how to display output by using C program and by using data type integer we are able to write some piece of code for addition by taking user inputs or simply assigning hard value to get the result .
In next Blog I will come up with different data types like float(decimal values) , character etc and write some program accordingly and will cover some condition operations with examples and explain it accordingly.Please go through this blog Carefully as it will help you to write some basic codes in C Program and once you understand then it’s easy for you to jump on some other programs in C.
Please let me know if you have any doubts in today topic.
Thanks.
Good for Students who want to learn computer science . C is very popular language and in this blog basics of C is well defined.
ReplyDelete