C Programming
C Programming
1.1 INTRODUCTION TO C:
1.1.1 Features of C:
It is a flexible high-level structured programming language.
It is portable
It is much faster and efficient.
It has an ability to extend itself.
It has a number of built in functions, which makes the programming task simple.
1.1.2 List of C Compilers
Compiler:
Compiler is a program that converts high level language into a machine-code or
lower-level form so that they can be read and executed by a computer.
List of C compilers:
1) Turbo C:
Turbo C is one of the basic and popular compilers for the C programming
language. This was first introduced in 1987; it was popular for its small size,
compilation speed, and low price. Once Turbo C++ got released in 1990, both the
compilers are merged. In 2006, Embarcadero Technologies had re-released Turbo C
as freeware.
2) Tiny C Compiler:
The Tiny C Compiler is designed to work on slow computers with little disk
space. This is an ARM processor C compiler. This compiler started its support to
Windows from 2005. The fastness of this compiler is around nine times faster than GCC.
This compiler allows automatic execution of programs during the compile-time only
using command line arguments.
3) Portable C Compiler:
The Portable C Compiler (PCC) was a very early used compiler for the C
programming language that is almost around mid-1970. This compiler had a long-life
span. This was prevalent during a period in such a way that many of the C compilers
were based on it. PCC compiler was made such that source files were machine-
dependent, not all but only a few of them. It can detect syntax errors and can perform
perfect validity checks.
4) GNU Compiler Collection:
GNU Compiler Collection (GCC) is the compiler produced by the GNU Project.
This supports many programming languages and it is a free software foundation under
the General Public License. This compiler was first released in 1987 and it supported
only C- Programming language during the start. Slowly it expanded to C++, Java,
Android, and IOS. Here, each of the different language compilers has its own program
that reads the code written and sends the machine code as the output. All of these have
a common internal structure. When a high-level language is written, as per the language
it is written, the compiler parses the code in that language and produces an abstract
syntax tree.
5. Clang:
Clang is a compiler for C, C++, Objective-C, and objective-C++ programming
languages. This compiler uses LLVM for the back-end code related compilations. One of
the major goals for Clang is to provide library-based architecture. It is designed to keep
more information during the compilation process than GCC. The error report generated
by Clang during compilation is always in a detailed format. Clang had always aimed to
reduce the over usage of memory space and increase the compilation speed as
compared with GCC, and due to these qualities, it had become one of the fastest-growing
used compilers during a point of time.
1.1.3 Structure of C program:
Documentation Section:
The documentation section consists of a set of comment lines giving the name of
the program, the author and other details. It consist of a set of comment lines . These
lines are not executable.
Preprocessor Section:
It is used to link system library files, for defining the macros and for defining the
conditional inclusion.
Definition section :
The definition section defines all symbolic constants.
Global Declaration Section:
There are some variables that are used in more than one function. Such variables
are called global variables and are declared in the global declaration section that is
outside of all the functions. This section also declares all the user-defined functions.
main () function section :
Every C program must have one main function section. This section contains two
parts; declaration part and executable part.
a) Declaration part : The declaration part declares all the variables used in the
executable part.
b) Executable part : There is at least one statement in the executable part.
These two parts must appear between the opening and closing braces. The
program execution begins at the opening brace and ends at the closing brace.
Subprogram section :
The subprogram section contains all the user-defined functions that are called in
the main () function. User-defined functions are generally placed immediately after the
main () function, although they may appear in any order.
Example:
1. Creating a program: Type the program and edit it in standard ‘C’ editor and save the
program with .c as an extension. This is the source program .The file should be saved as
'*.c' extension only.
2. Compiling the Program:
• This is the process of converting the high level language program to Machine level
Language (Equivalent machine instruction) .
• Errors will be reported if there is any, after the compilation
• Otherwise the program will be converted into an object file (.obj file) as a result of the
compilation
• After error correction the program has to be compiled again
3. Linking a program to library: The object code of a program is linked with libraries
that are needed for execution of a program. The linker is used to link the program with
libraries. It creates a file with '*.exe' extension.
4. Execution of program: This is the process of running and testing the program with
sample data. If there are any run time errors, then they will be reported.
1.2 VARIABLES, CONSTANTS & DATA TYPES:
1.2.1 C Character Set:
Every C program contains statements. These statements are constructed using
words and these words are constructed using characters from C character set. C
language character set contains the following set of characters...
1. Alphabets 2. Digits 3. Special Symbols
Alphabets:
C language supports all the alphabets from English language. Lowercase and
uppercase letters together supports 52 alphabets.
lower case letters - a to z
upper case letters - A to Z
Digits :
C language supports 10 digits which are used to construct numerical values in C
language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols :
C language supports rich set of special symbols that include symbols to perform
mathematical operations, to check conditions, white spaces, back spaces and other
special symbols.
1.2.2 Keywords:
The meaning of these words has already been explained to the C compiler. All the
keywords have fixed meanings. These meanings cannot be changed. So these words
cannot be used for other purposes. All keywords are in lower case. The keywords are
known as Reserved words. Keywords or Reserved words are Pre-defined identifiers. 32
keywords are available in C.
Properties of Keywords
1. All the keywords in C programming language are defined in lowercase letters only.
2. Every keyword has a specific meaning; users can not change that meaning.
3. Keywords cannot be used as user defined names like variable, functions, arrays,
pointers etc...
4. Every keyword in C programming language represents some kind of action to be
performed by the compiler.
1.2.3 Identifiers:
The names of variables, functions, labels and various other user-defined objects
are called Identifiers. Identifiers are used for defining variable names, function names
etc. The general rules to be followed when constructing an identifier are:
1) Identifiers are a sequence of characters. The only characters allowed are alphabetic
characters, digits and the underscore character. Special characters are not allowed in
identifier name.
2) The first character of an identifier is a letter or an underscore character.
3) Identifiers are case sensitive. For example, the identifiers TOTAL and Total are
different.
4) Keywords are not allowed as identifier name.
VALID IDENTIFIERS:
Name area interest circum amount rate_of_interest sum
INVALID IDENTIFIERS:
1.2.4 Datatypes:
C supports several data types. Each data type may be represented differently
inside the computer’s memory. There are four data types in C language. They are,
1.2.6 Constants:
The data values are usually called as Constant. Constant is a quantity that does
not change during program execution. This quantity can be stored at a location in the
memory of the computer. C has four types of constants: Integer, Float, String and
Character.
Integer Constant:
An Integer Constant is an integer number. An integer constant is a sequence of
digits. There are 3 types of integers namely decimal integer, octal integer and
hexadecimal integer.
Decimal Integer consists of a set of digits 0 to 9 preceded by an optional + or -
sign. Spaces, commas and non digit characters are not permitted between digits.
Examples of decimal integer constants are : 123 -31 0 562321 + 78
Octal Integer constant consists of any combination of digits from 0 through 7
with a O at the beginning.
Examples of octal integers are : O26 O347 O676
Hexadecimal integer constant is preceded by OX or Ox. They may contain
alphabets from A to F or a to f. or a decimal digit ( 0 to 9). The alphabets A to F refers to
10 to 15 in decimal digits.
Example of valid hexadecimal integers are : OX2 OX8C OXbcd Ox123
Real Constant:
Real constants are numbers with fractional part. Real constants are often called
as Floating Point constants.
RULES:
1. A real constant must have atleast one digit.
2. It must have a decimal point.
3. It could be either positive or negative.
4. Default sign is positive (If no sign)
5. Special characters are not allowed.
6. Omitting of digit before the decimal point, or digits after the decimal point is allowed.
(Ex .655,12.)
1.2.9 Variables:
A quantity, which may vary during program execution, is called variable. Variables
may be used to store a data value. Variables are actually memory locations , used to
store constants. The variables are identified by names. The program may modify the
values stored in a variable.
Rules for constructing variable names:
1. A variable name is the combination of characters. The length of a variable depends
upon the complier.
2. The first character must be an alphabet or underscore.
3. Special characters like comma or blank are not allowed except an underscore
character. The only characters allowed are letters, digits and underscore.
4. The variable name should not be a keyword.
where data-type is a valid data type plus any other modifiers. variable-name(s) may
contain one or more variable names separated by commas.
Examples : int a, b, c;
long int interest;
unsigned char ch;
Declaration statement is used to allocate memory space for the variable.
Declaration statement also provides a name for the location. Declaration statement
declares that the program will use that variable name to identify the value stored at the
location.
i) int i = 10;
ii) int i;
i = 10;
The above two methods declares that i is an integer variable with an initial value of 10.
Examples : (i) float pi = 3.14; (ii) char alpha = ‘’h’’; (iii) int account = 10;
More than one variable can be initialized in a single statement. For example, the
statement
int x = 1, y = 2, z;
declares x to be an int with value 1, y to be an int with value 2 and z to be an int
of unpredictable value. Same value can be initialized to several variables with the single
assignment statement.
Examples :
int i, j, k, l, m, n;
float a, b, c, d, e, f;
i = j = k = l = m = n = 20;
a = b = c= d = e = f = 13.56;
1.2.10 Comments:
Comments are used to make a program more readable. Comments are not
instructions. Comments are remarks written in a program. These remarks are used to
give more information about the program. The compiler will ignore the comment
statements.
In C, there are two types of comments.
1. Single Line Comments: Single line comment begins with // symbol. Any number of
single line comments can be written.
2. Multiple Lines Comments: Multiple lines comment begins with /* symbol and ends
with */. Any number of multiple lines comments can be included in a program.
In a C program, the comment lines are optional. All the comment lines in a C
program just provide the guidelines to understand the program and its code.
Examples:
(e.g.) /* Program to find the Factorial */
Any number of comments can be placed at any place in a program. Comments cannot be
nested. For example
/* Author Arul /* date 01/09/93 */ */ is not possible.
A comment can be split over more than one line. For example, the following is valid.
/* This statement is used to find the sum of two
numbers */
1.3 C OPERATORS:
1.3.1 Operator:
An operator is a symbol which represents some operation that can be performed
on data.
Types of Operators:
1.Arithmetic Operators
2.Relational Operators
3.Logical Operators
4.Increment and Decrement Operators
5.Conditional Operators
6.Assignment Operators
7.Special Operators
8.Bitwise Operators
1.Arithmetic Operators:
Arithmetic Operators are used to do arithmetic calculations.
Operator Operation Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Remainder a%b
Program:
#include<stdio.h>
void main()
{
int a=10,b=5;
printf(“Addition=%d”,a+b);
printf(“Subtraction=%d”,a-b);
printf(“Multiplication=%d”,a*b);
printf(“Division=%d”,a/b);
printf(“Remainder=%d”,a%b);
}
2.Relational Operators:
Relational Operators are used to compare two values.
Relational Operators has two results TRUE or FALSE.
program:
#include<stdio.h>
void main()
{
int a=10,b=5;
if(a>b)
printf(“a is big”);
else
printf(“b is big”);
}
3.Logical Operators:
The Logical Operators are used to combine multiple conditions into one
condition.
Program:
#include<stdio.h>
void main()
{
int a=5,b=4,c=15;
if((a>b)&&(a>c))
printf(“a is big”);
else if(b>c)
printf(“b is big”);
else
printf(“c is big”);
}
5.Conditional Operators:
The Conditional Operators ? and : are used to build simple conditional
expression.
It requires three data so it is called as Ternary Operator.
Syntax:
expr1?expr2:expr3;
Example: (a>b)?a:b;
Program:
#include<stdio.h>
void main()
{
int a=5,b=2,c;
c=(a>b)?a:b;
printf(“%d is big”,c);
}
6.Assignment Operators:
Assignment operators are used to simplify the coding of certain type of
assignment statement.
Operator Example
= x=10
+= x+=10 [x=x+10]
-= x-=10 [x=x-10]
*= x*=10 [x=x*10]
/= x/=10 [x=x/10]
%= x%=10 [x=x%10]
Program:
#include<stdio.h>
void main()
{
int x=10;
printf(“%d”,x+=10);
printf(“%d”,x-=10);
printf(“%d”,x/=10);
printf(“%d”,x*=10);
}
7.Bitwise Operators:
Program:
#include<stdio.h>
void main()
{
int a=5,b=2;
printf(“%d”,a&b);
printf(“%d”,a|b);
printf(“%d”,~b);
}
8.Special Operators:
C supports some special operators like comma operator, sizeof operator, pointer
operators (& and *) and member selection operators (. And ->).
Comma Operator:
Comma operator is used in the assignment statement to assign many values to
many variables.
Example: int a=10, b= 20, c- 30;
Comma operator is also used in for loop in all the three fields.
Example: for (i=0, j=2; i<10; i=i+1;j=j+2);
sizeof Operator: Another unary operator is the sizeof operator . This operator returns
the size of its operand, in bytes. This operator always precedes its operand. The
operand may be a variable, a constant or a data type qualifier.
Consider the following program.
main()
{
int sum;
printf("%d", sizeof(float));
printf("%d", sizeof (sum));
printf("%d", sizeof (234L));
printf("%d", sizeof ('A'));
}
Here the first printf( ) would print out 4 since a float is always 4 bytes long. With
this reasoning, the next three printf( ) statements would output 2, 4 and 2. Consider an
array school[]= “National” . Then, sizeof (school) statement will give the result as 8.
Pointer Operator:
Pointer Operator ‘*’ is used to define pointer variable.
Example: int *a;
Dot Operator:
Dot operator ‘.’ is used to access the members of the structure or union.
Constant Constant
(or) Arithmetic Operator (or)
Variable Variable
Example:
i) x-z ii)x+y*2 iii)4+2/6
ii)Relational Expressions:
Relational expression is the combination of Variables, Constants and Arithmetic
expressions connected by any one of the relational operators.
Syntax:
Constant Constant
(or) (or)
Variable Relational Operator Variable
(or) (or)
Arithmetic Expression Arithmetic Expression
Example:
i) x>z ii)(x+y)<2 iii)4<=6
iii)Logical Expressions:
Logical expression is formed by connecting relational expression by logical
operators.
Syntax:
Relational Relational
Logical Operator
Expression Expression
Example:
i) (a<b)&&(a>b) ii)(a==2)||(a>2)
Example:
int a=2,b=3;
float c;
c=a+b;
Answer: c=5.0
Syntax:
(datatype)Variable or Constant;
Example:
int x;
x=(int) 5.4;
Answer:x=5