Introduction To C
Introduction To C
Why use C ?
C is often called a "Middle Level" programming language. This is not a reflection on its
lack of programming power but more a reflection on its capability to access the system's
low level functions. Most high-level languages (e.g. Fortran) provides everything the
programmer might want to do already built into the language. A low level language (e.g.
assembler) provides nothing other than access to the machines basic instruction set. A
middle level language, such as C, probably doesn't supply all the constructs found in
high-languages - but it provides you with all the building blocks that you will need to
produce the results you want!
C was initially used for system development work, in particular the programs that make-
up the operating system. Why use C? Mainly because it produces code that runs nearly as
fast as code written in assembly language. Some examples of the use of C might be:
Operating Systems , Language Compilers, Assemblers ,Text Editors ,Print Spoolers
,Network Drivers ,Modern Programs ,Data Bases ,Language Interpreters ,Utilities
Features of C
a. Simple
b. Portability
c. Powerful
d. Platform dependent
e. Structure oriented
f. Case sensitive
g. Compiler based
h. Modularity
i. Middle level language
j. Syntax based language
k. Use of Pointers
a. Simple
Every c program can be written in simple English language so that it is very easy to understand
and developed by programmer.
b. Platform dependent
A language is said to be platform dependent whenever the program is execute in the same
operating system where that was developed and compiled but not run and execute on other
operating system. C is platform dependent programming language.
Note: .obj file of C program is platform dependent
c. Portability
C is highly portable this means that programs once written can be run on another machines with
little or no modification.
d. Powerful
C is a very powerful programming language, it have a wide variety of data types, functions,
control statements, decision making statements, etc.
e. Structure oriented
C is a Structure oriented programming language. Structure oriented programming language
aimed on clarity of program, reduce the complexity of code, using this approach code is divided
into parts using functions. So, it is easy to understand and modify
f. Modularity
In C programming we can break our code in subprogram.
g. Case sensitive
It is a case sensitive programming language. In C programming 'break and BREAK' both are
different.
h. Middle level language
C programming language can supports two level programming instructions with the combination
of low level and high level language that's why it is called middle level programming language.
i. Compiler based
C is a compiler based programming language that means without compilation no C program can
be executed. First we need compiler to compile our program and then execute.
j. Syntax based language
C is a strongly tight syntax based programming language. If any language follow rules and
regulation very strictly known as strongly tight syntax based language.
k. Pointers
Pointers is a variable which hold the address of another variable. C provides the feature of
pointers.
Structure of C Program
Document Section
The document section contains a set of comment lines, giving the name of the program, the
author details etc.
Pre-processor
Before a C program is compiled in a compiler, source code is processed by a program
called preprocessor.
#include is the first word of any C program.
The main work of pre-processor is to initialize the environment of program, i.e to link the
program with the header file.
Commands used in preprocessor are called preprocessor directives and they begin with
# symbol.
Header file
Header file is a collection of built-in functions that help us to code easily in our program.
Header files contain definitions of functions and variables which can be incorporated into
any C program by pre-processor #include statement.
stdio.h is a header file in C used for input & output purpose.
It contains the predefined input function like scanf() and output function printf() etc.
main() function
The program starts execution from here.
When C program executes, the operating system will send resource and that resource is
picked by the main function. So without main function we cannot execute a C program.
Opening Curly Brace
The opening curly brace { states the beginning of the main function or starting of the
program.
The closing curly brace } states the end of the main function or end of the program.
All the statements between the two braces forms the function body. The function body
contains set of instructions to perform the given task.
Each expression statement must end with a semicolon (;)
First C Program
/* Name : Ram
Date : 22/08/2017
Description : Write a program to print Hello World
*/
#include<stdio.h> // preprocessor directive
int main() // main function
{
printf(Hello, World! \n"); // Printing here
return 0;
}
#include <stdio.h> This is a preprocessor command that includes standard input output header
file(stdio.h) from the C library before compiling a C program
int main() This is the main function from where execution of any C program begins
{ This indicates the beginning of the main function.
/*_some comments_*/ whatever is given inside the command /* */ or // in any C program,
wont be considered for compilation and execution.
// some comment
printf(Hello,World! ); printf command prints the output onto the screen.
return 0; This command terminates C program (main function) and returns 0.
} This indicates the end of the main function.
Flow of C Program
Tokens in C
A C program consists of various tokens and a token is either a keyword, an identifier, a constant,
a string literal, or a symbol.
In C Programming punctuation,individual words,characters etc are called tokens.
For example, the following C statement consists of five tokens
Elements of C
Every language has some basic elements and grammatical rules.
The basic elements include:
1. Character Set
Character sets affect the fundamental part of the program code, the storage and transmission of
data and the logic with which you manipulate text. A C compiler may use any character set that
includes at least following character sets.
1. 52 alphabetic characters (A-Z and a-z)
2. 10 decimal digits (0-9)
3. 29 special characters (! # % ^ & * ( ) - _ + = ~ [ ] , . : / ?).
4. 5 formatting characters (backspace, horizontal tab, vertical tab, form feed and carriage
return). These are treated as spaces.
5. Dollar ($) and at @ are also used but required by the standard.
White Space
It includes blanks (space character, horizontal tab, vertical tab, end of line, form feed and carriage
return and comments. White spaces are ignored by compiler except when required to separate
tokens or when used in a character or string constant. These are used freely by the programmer to
make the program easy for a person to read.
The characters that are used in C programs are given below:
Alphabets
A,B,C,.,Z
a,b,c,.,z
Digits
0,1,2,3,4,5,6,7,8,9
Special characters
2. Escape sequence
Some characters such as newline, tab and backspace cant be printed like other normal
characters.
C supports the combination of backslash(\) and some characters from the C character
set to print these characters.
These character combinations are known as escape sequences and are represented by two
characters.
4. Keywords
These are certain reserved words called keywords, that have standard,
Predefined meaning in C.
These keywords can be used only for their intended purpose.
The keyword have fixed meaning and there meaning cannot be changed.
They are written in lowercase letter.
Total 32 keywords are there.
ANSI C keywords:
6. Data type
Defines a domain of allowed values and the operations that can be performed on those
values.
Data type is a keyword used to identify type of data.
Data types are used for representing the input of the user in the main memory (RAM) of
the computer.
What is common between a suitcase and a bottle ?
They all are containers.
They store specific type of thing.
The program stores the values being supplied by the user, so
we need containers, which can store values in them.
These containers are referred to technically as variables.
Similarly, every variable is created to store specific type of data, which is called
data type of variable.
Basic /Fundamental data types
#include <stdio.h>
NOTE : %.2f means that the variable to be
int main() printed will be of type float and '.2' means that
{ the number rounded to only two decimal places
int sum = 100; will be printed. We can use any number to
char letter = 'Z'; which we have to round our decimal value.
float set1 = 23.567;
double num2 = 11e+23;
Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount
of storage space allocated to a variable.
For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the
range by using long int which is 8 byte. We can decrease the range by using short int which is 2
byte
Note: in real time no need to write signed keyword explicitly for any data type
Size and range of Datatypes
Type Storage size Value range
Numeric constants
Numeric constants of numeric digits, they may or may not have decimal point (.).
These are rules for defining numeric constants
Numeric constant should have at least one digit.
No comma or space is allowed within the numeric constants.
Numeric constants can either be positive or negative but default sign is always positive.
There are two types of numeric constants
Integer Constant
Integer constants are whole numbers, which have no decimal point (.). There are three types of
integer constants based on different number systems. These constants are
a) Decimal constants 0,1,2,3,4,5,6,7,8,9 (base 10)
b) Octal constants 0,1,2,3,4,5,6,7 (base 8)
c) Hex decimal constants 0,1,2,3,4,5,6,7,8,9 A,B,C,D,F (base 16)
Valid integer constant: 123, 3705, 0, -25
Invalid integer constant: 2.5, 3#5, 98 5
Real Constants
The numbers are containing fractional parts like 12.45 are called real (or floating point) constant.
The real constants can be written in two forms
a) Fractional form: In this form, the numbers has a decimal point.
Example: +325.35, 42.0,-21.5
b) Exponential form: The real number can be expressed by exponential notation. The general
form
mantisa@exponent
Constructing real constant in exponential form:
The mantissa part and the exponential part should be separated by a letter e or E.
The mantissa part may have a positive or negative sign.
Default sign of mantissa part is positive.
The exponent must have at least one digit, which must be a positive or negative integer.
Default sign is positive.
Example: 0.65e4, 22e-3, 2.11E3
Single Character Constants
A single character constant (or simply) character constant contains a single character enclosed with
a pain of single quote marks. Every character constant has a unique integer value associated with
it. This integer is the numeric value of the character in the machines character code. If the machine
is using ASCII (American Standard Code for Information Interchange), then the character A
represents integer value 65. Each character constant represents an integer value. So, it is possible
to perform arithmetic operations on character constant. Examples of character constants are
a Y *
Some ASCII values are
A-Z ASCII value (65-90)
a-z ASCII value(97-122)
0-9 ASCII value (48-57)
; ASCII value (59)
String Constants
A string constant has zero, one or more than one characters. The characters may be letters,
numbers, special characters and blank space. A string constant is enclosed within double quotes (
). At the end of string, \0 is automatically places b the compiler. Some examples of string
constants are
imedia 463 5 X
#include<stdio.h>
int main()
{
const int a=10;
printf("%d",a);
a=20; // gives error you can't modify const
return 0;
}
Symbolic Constants
A symbolic constant is a name that substitutes for a sequence of characters. The character may
represent a numeric constant, a character or named constant.
Note: A symbolic constant is required if we want to use a constant several times.
For example: If we have to use the constant 3.14159265 at many places in our program, than we can give
it a name PI and use this name instead of writing the constant value everywhere.
These constants are generally defined at the beginning of the program as
#define name value
name: is the symbolic name for the constant , and is generally written in uppercase letters
Value: can be numeric, character and string constant.
#define statement is a preprocessor compiler directive.
# define MAX 75
# define P1 3.14159625
# define Ch Y
# define NAME tapan
8. Variable
Variable is an identifier which holds data or another one variable.
It is an identifier whose value can be changed at the execution time of program.
It is used to identify input data in a program.
The variables need to be declared before their use.
If no input values are assigned by the user than system will gives a default value called
garbage value.
Garbage value
Garbage value can be any value given by system.
This is a disadvantage of C programming language and in C programming it can overcome
using variable initialization
Initialization of Variables
When a variable is declared it contains undefined value commonly known as garbage
value.
If we want we can assign some initial value to the variable during the declaration itself.
This is called initialization of the variable.
It is the process of allocating sufficient memory space with user defined values.
Syntax : Datatype variable_name = value;
Example : int b = 20;
Variable assignment
Syntax : variable_name = value;
Example : b = 20;
Example :
int a = 20;
int b;
b = 25; // directly assign variable
b = a; //assigned variable in term of variable 35
9. Expressions
An expression is a combination of operators, constants, variables and function calls. The
expression can be arithmetic, logical or relational .
Example:
x+y
a=b+c
a>b
a==b
Func(a,b)
10. Statements
In a C program instructions are written in the form of statements.
Statements can be categorized as :
i. Expression statements
ii. Compound statements
iii. Selection statements ( if, if.. .else, switch)
iv. Iterative statements (for, while, dowhile)
v. Jump Statements (goto, continue, break, return)
vi. Label statements (case, default, label statement used in goto)
11.Comments
Comments are used for increasing readability of the program.
They explain the purpose of the program and are helpful in understanding the program.
In 'C' language two types of comments are possible
Single line comments
Multiple line comments
Single line comments
Single line comments can be provided by using
//....................
Multiple line comments
Multiple line comments can be provided by using /*......................*/
Error
Error is a abnormal condition whenever it occurs execution of the program is stopped these are mainly
classified into following types.
Compile time error
Run time error
Compile time error
If any error is generated at the time of compilation it is known as compile time error, in general these
are raised while break down the rules and regulation of programming language.
Example:
Missing semicolon, writing keyword in upper case, writing variable declaration, initialization after calling
clrscr() function. Compile time errors also known as syntax errors.
Warning
Warning is also an abnormal condition but whenever it occurred execution of program will never be
stopped.