Monday, 20 July 2015

Getting started with C : Basic of C Language !

C is high level programming developed by Dennis m. Ritchie in 1969. It is the most widely used language in computer world. The only way to learn a new programming language is by writing programs in it. So let's write a computer program in C. There is a tradition that whenever you learn a new programming language, the first program that you write and run is the 'Hello World' program. So before we start complex program, we will start with simple "Hello world" program.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello World");
 
   system("PAUSE");   
  
}


I would hope that This program does not scare you. it just simply print Hello World on your screen. Let me discussing in detail.
When you compile and run it then it's output will be :
                                                   


The first line is called header file.Any statement that start with # sign are directives. It tells the compiler to include the header file stdio and stdlib.
The next line shows the starting of main function. Every functions has it's code included in a pair of curly braces ({}) so that code below line int main() inside { } is code of function main() or body of main program
In the next line the symbol { tells us that main body of the program starts from here.
The next line display the string  Hello World on your screen.The next line i.e.system("PAUSE"), pauses the output otherwise your output will disappear after few seconds.And in the last line, the program ends with } .
NOTE: every instruction ends with a semicolon(;).

Before we start to write more program and we make our hands dirty, I would like to tell you about some technical programmings concepts  that we often use in almost every high level programming languages. These terms are : Data types,operators, conditional expressions,  Variables,loops, Function,Array, structure, pointer etc.

Data types: Data  can be of many types e.g. character, integer, string etc. C language provides ways and facilities to handle different types of data by providing data types. There are four basic data types in C:
char : character can store any number, letter of the C's basic character set.
int: Integer are whole numbers such as 2, 10, -100, 0 but they don't have any fractional parts.
float: A number having fractional parts is floating-point number.
double: This is also used for handling floating point numbers but it occupies twice as much memory as type float and stores floating point numbers with much larger and precision.
In addition, there are number of qualifiers(modifiers) that can be applied to these basic type:
long    short    signed    unsigned
The amount of memory space to be allocated for a variable is derived by modifiers.Modifiers are prefixed with basic data types to modify the amount of storage space allocated to a variable.
short int a;
long int b;

Each compiler is free to choose appropriate sizes for its own hardware, subjected only to restriction that shorts and int are at least16 bits, longs are at least 32 bits, and short is no longer than int, which is no longer than long.
                       

Operators:There are various kind of operators exist in C : Arithmetic Operators, Relational Operators,Logical Operators, Bit wise Operators,Assignment Operators,Special Operators.

Arithmetic Operators:
Operator:              Meaning :
+                         addition or unary plus
-                         subtraction or  unary minus
*                        multiplication
/                         division
%                      remainder after division

Relational Operators:
Operator:              Meaning :
==                        Equal to   
>                         Greater than   
<                         Less than   
!=                        Not equal to   
>=                      Greater than or equal to   
<=                      Less than or equal to   

Logical Operators:
Operator:                              Meaning :         
&& (Logical AND )      If both the operands are non-zero, then condition becomes true.
||   ( Logical OR )         If any of the two operands is non-zero, then condition becomes true.
!   ( Logical NOT )      Use to reverses the logical state of its operand. If a condition is true then      
                                     Logical NOT operator will make false.

Bitwise Operators:
Operator:              Meaning :
&                         Bitwise AND
|                          Bitwise OR
^                         Bitwise exclusive OR
~                        Bitwise complement
<<                     Shift left
>>                     Shift right

Assignment Operators:
Operator:              Meaning :
=                           a=b    a=b
+=                        a+=b    a=a+b
-=                        a-=b    a=a-b
*=                       a*=b    a=a*b
/=                        a/=b    a=a/b
%=                     a%=b

Special Operators:
Operator:              Meaning :
sizeof()               Returns the size of an variable.   
&                      Returns the address of an variable.   
*                      Pointer to a variable.   
? :                    Conditional Expression

Conditional Expressions :They deciding the order of execution of statement based on certain condition or repeat a group of statement until certain certain specified condition are met. if else statement, nested if statement, switch statement, conditional operator statement are examples of conditional expressions.. We will discuss in detail later.

Variables:A variable is a container that you can use store data in. Variables are used in programs to store pieces of information that may change and may be used in the program.We will discuss in detail later.

Loops: A loop allow us to execute a statement multiple times in our program.There are 4 types of loops in C programming:
for loop
while loop
do...while loop
nested loop
We will discuss in detail later.

Function: A functions can be defined as :
It is the set of information that can be grouped into smaller sub-program called a function
OR
It is set of instructions that performs a particular task and we need several times in our program so it can be grouped  into a smaller subprogram called a function.
We will discuss in detail later.

Array:An array allows us to declare a collections of variables that referenced by a common name.Why we use arrays?
Suppose you have to write a program which stores marks of 50 students.
In normal way, you declare  50 variables  which store the value of 50 student's marks.
So instead of creating individually variables, this can be done by using Arrays.
We will discuss in detail later.

Structure: Structures can be defined as the structure is a collection of variables under a single name.
And now question arise is why we need Structures or why we use Structures ?
Suppose you want to store the information about a student about roll no, class, marks etc. you can do it separately But C offers Structures to handle such situations.
We will discuss in detail later.

Pointer :Pointers are just variables that store memory addresses. This address is usually the location of another variable in memory. Or  in simple word you can say, A pointer is a variable whose value is the address of another variable.
We will discuss in detail later.

Now You must have the basic idea of c programming language. On this blog Programming :Ethical Hacker way, We'll cover  every topics in the detail.
if you like this post or have any question, please feel free to comment !
Facebook Twitter Google+

 
Back To Top