Monday, February 24, 2014

C Language




What is C programming:
C programming is one of thousands of computer programming languages that allow users to create instructions for a computer to follow. While C has a slightly more cryptic style than some other programming languages, it's fairly easy to learn and allows you to read and write code for many different platforms. Because it's so efficient and gives the user a lot of control, C is very popular with programmers.

The C programming language has been around since the early '70s, when it was developed by Ken Thompson and Dennis Ritchie at Bell Laboratories. They saw the need for a more user-friendly programming language, and after several attempts at new languages, C was eventually finalized and released. Throughout the '80s, the developers created various standards for the language. As computers became more complex, programmers were able to use C to build their own compilers and programming languages. The C programming language has led to the development of both Java and C++, which are popular today and both simplified programming even further.

C – Language History
  • C language is a structure oriented programming language, was developed at Bell Laboratories in 1972 by Dennis Ritchie
  • C language features were derived from earlier language called “B” Basic Combined Programming Language – BCPL
  • C language was invented for implementing UNIX operating system
  • In 1978, Dennis Ritchie and Brian Kernighan published the first edition  The C Programming Language and commonly known as K&R C
  • In 1983, the American National Standards Institute ANSI established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or “ANSI C”, was completed late 1988.

Which level the C language is belonging to:
S.no
High Level
Middle Level
Low Level
1
High level languages provides almost everything that the programmer might need to do as already built into the language
Middle level languages don’t provide all the built-in functions found in high level languages, but provides all building blocks that we need to produce the result we want
Low level languages provides nothing other than access to the machines basic instruction set
2
Examples:
Java, Python
C, C++
A



What is C Language:

A high-level programming language developed by Dennis Ritchie at Bell Labs in the mid 1970s. Although originally designed as a systems programming language, C has proved to be a powerful and flexible language that can be used for a variety of applications, from business programs to engineering. C is a particularly popular language for personal computer programmers because it is relatively small -- it requires less memory than other languages.
 
The first major program written in C was the UNIX operating system, and for many years C was considered to be inextricably linked with UNIX. Now, however, C is an important language independent of UNIX.

Although it is a high-level language, C is much closer to assembly language than are most other high-level languages. This closeness to the underlying machine language allows C programmers to write very efficient code. The low-level nature of C, however, can make the language difficult to use for some types of applications.

Why to use C:
C was initially used for system development work, in particular the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as code written in assembly language. Some examples of the use of C,

1.     Operating Systems
2.     Language Compilers
3.     Assemblers
4.     Text Editors
5.     Print Spoolers
6.     Network Drivers
7.     Modern Programs
8.     Databases
9.     Language Interpreters
10.      Utilities

C Basic Program
   We are going to learn a simple Hello World C program in this section. Also, all the below topics are explained in this section which are the basics of a C program.
1.     C basic program with output and explanation
2.     Steps to write C programs and get the output
3.     Creation, Compilation and Execution of a C program
4.     Basic structure of a C program

Program:

#include
#include
void main()
{
     printf("Hello World! ");
     getch();
     return 0;
}
          Output:
                   Hello World!
1. Explanation for above C basic Program:
S.no
Command
Explanation
1
#include
This is a preprocessor command that includes standard input output header file(stdio.h) from the C library before compiling a C program
2
int main()
This is the main function from where execution of any C program begins.
3
{
This indicates the beginning of the main function.
4
/*_some_comments_*/
whatever is given inside the command “/*   */” in any C program, won’t be considered for compilation and execution.
5
printf(“Hello_World! “);
printf command prints the output onto the screen.
6
getch();
This command waits for any character input from keyboard.
7
return 0;
This command terminates C program (main function) and returns 0.
8
}
This indicates the end of the main function.



2. Steps to write C programs and get the output:
      Below are the steps to be followed for any C program to create and get the output. This is common to all C program and there is no exception whether its a very small C program or very large C program.





3. Creation, Compilation and Execution of a C program:

    • If  you want to create, compile and execute C programs by your own, you have to install C compiler in your machine. Then, you can start to execute your own C programs in your machine.
    • You can refer below link for how to install C compiler and compile and execute C programs in your machine.
    • Once C compiler is installed in your machine, you can create, compile and execute C programs as shown in below link.
C – Environment Setup Using IDE tool
C – Environment Setup Using GCC compiler




4. Basic structure of C program:
     Structure of C program is defined by set of rules called protocol, to be followed by programmer while writing C program. All C programs are having sections parts which are mentioned below.
1.     Documentation section
2.     Link Section
3.     Definition Section
4.     Global declaration section
5.     Function prototype declaration section
6.     Main function
7.     User defined function definition section

C – Data Types
  • C data types are defined as the data storage format that a variable can store a data to perform a specific operation.
  • Data types are used to define a variable before to use in a program.
  • Size of variable, constant and array are determined by data types.

There are four data types in C language.
S.no
Types
Data Types
1
Basic data types
int, char, float, double
2
Enumeration data type
enum
3
Derived data type
pointer, array, structure, union
4
Void data type
void





          Integer:
·        Integer data type allows a variable to store numeric values.
·        The storage size of int data type is 2 or 4 or 8 byte.
·        int 2 byte can store values from -32,768 to +32,767
·        int 4 byte can store values from -2,147,483,648 to+2,147,483,647.
·        If you want to use the integer value that crosses the above limit, you can go for long int and long long int for which the limits are very high.
Character:
o   Character data type allows a variable to store only one character.
o   Storage size of character data type is 1. We can store only one character using character data type.
o   A can be stored using char data type. You can’t store more than one character using char data type.
o   C – Strings topic to know how to store more than one character in a variable.
Float:
v Float data type allows a variable to store decimal values.
v We can use up-to 6 digits after decimal using float data type.
v For example, 10.456789 can be stored in a variable using float data type.
Double:
Ø Double data type is also same as float data type which allows up-to 10 digits after decimal.
Ø The range for double data type is from 1E–37 to 1E+37.



C Token And Keywords
C tokens:
C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in a C program are known as C tokens.
C tokens are of six types
        1. Keywords              
        2. Identifiers              
        3. Constants              
        4. Strings                    
        5. Special symbols  
        6. Operators              
C token example pro:
#include
#include
void main()
{
    int x, y, total;
    x = 10, y = 20;
    total = x + y;
    Printf (“Total = %d \n”, total);
    Getch();
}

Keywords in C language:
v  Keywords are pre-defined words in a C compiler.
v  Each keyword is meant to perform a specific function in a C program.
v  Since keywords are referred names for compiler, they can’t be used as variable name.
C language supports 32 keywords which are given below


auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while




C Variable:
              C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. The value of the C variable may get change in the program. C variable might be belonging to any of the data type like int, float, char,
Rules for naming C variable:
o   Variable name must begin with letter or underscore.
o   Variables are case sensitive
o   They can be constructed with digits, letters.
o   No special symbols are allowed other than underscore.
o   sum, height, _value are some examples for variable name

C – Operators and Expressions
The symbols which are used to perform logical and mathematical operations in a C program are called C operators. These C operators join individual constants and variables to form expressions. Operators, functions, constants and variables are combined together to form expressions. Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.


Types of C operators:
C language offers many types of operators.
1.     Arithmetic operators
2.     Assignment operators
3.     Relational operators
4.     Logical operators
5.     Bit wise operators
6.     Conditional operators
7.     Increment decrement operators
8.     Special operators
    •  
S.no
Types of Operators
             Description              
1
Arithmetic operators
These are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus
2
Assignment operators
These are used to assign the values for the variables in C programs.
3
Relational operators
These operators are used to compare the value of two variables.
4
Logical operators
These operators are used to perform logical operations on the given two variables.
5
Bit wise operators
These operators are used to perform bit operations on given two variables.
6
Conditional operators
Conditional operators return one value if condition is true and returns another value is condition is false.
7
Increment decrement operators
These operators are used to either increase or decrease the value of the variable by one.
8
Special operators
&, *, size of ( ) and ternary operators.


C – Loop statements
Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.
Types of loop statements in C:
There are 3 types of loop statements in C language.
1.     for
2.     while
3.     do-while

S.no
Loop Name
Syntax
Description
1
for
for (exp1; exp2; expr3)
{ statements; }
Where,
exp1 – variable initialization
( Example: i=0, j=2, k=3 )
exp2 – condition checking
( Example: i>5, j<3 br="" k="3"> exp3 – increment/decrement
( Example: ++i, j–, ++k )
2
while
while (condition)
{ statements; }
where,
condition might be a>5, i<10 span="">
3
do while
do { statements; }
while (condition);
where,
condition might be a>5, i<10 span="">




While loop Program:
#include
#include
void main()
{
  int i=1;

  while(i<=10)
  {
      printf("%d\n",i);
      i++;
getch();
  }    
Output:

                1 2 3 4 5 6 7 8 9 10

Do while loop program:
# include
# include
void main()
{       
  int i=1;

  do
  {  
      printf("Value of  is %d\n",i);
      i++;
  }
while(i<=4 && i>=2);    
 getch();
}
Output:
                Value if is 1
                Value if is 2
                Value if is 3
                Value if is 4
For loop Program:
#include
#include

void main()
{
  int i;

  for(i=0;i<10 i="" span="">
  {
      printf("%d ",i);
  }    
 Getch();
}

Output:
0 1 2 3 4 5 6 7 8 9

Difference between while & do while loops in C:
S.no
while
do while
1
Loop is executed only when condition is true.
Loop is executed for first time irrespective of the condition. After executing while loop for first time, then condition is checked.

No comments:

Post a Comment