INTRODUCTION
Primary Objectives:
• to introduce the C programming language as software
tool
• to introduce the basic concepts of software design
• to learn to design, code and debug complete C
programs
• to learn the necessary skills to design large and
complex software systems
Important Features of C Language
1. A system programming language which provides
flexibility for writing compilers, operating systems, etc.
2. used for writing the application programs for
scientific, engineering and business applications.
3. C is famous for its portability, meaning program can
be easily loaded to another computer with little or no
changes.
4. Supports modular programming.
5. C supports variety of data types like integers, float
point numbers, characters, etc.
compilers
• Compiler: It is a system program that does
compilation. Turbo C, ANSI C, Borland C are
examples of Compiler software.
Structure of a C program
//First C program: Hello World
#include <stdio.h>
#include <conio.h>
int main()
{
printf("Hello World! ");
getch();
return 0;
}
Note: C is case sensitive. All commands must be in lower
case.
Documentation Section /* Sample C Program */
Preprocessor Directive or Link Section #include<stdio.h>
Global Declaration Section
main()
main() Function Section
{
{
int a=4, b=5, sum;
Local Declaration Part clrscr();
printf(“Enter two numbers”);
Executable Part sum=a+b;
} printf(“Sum = %d”,sum);
}
Sub program Section
Or
User Defined Function Section
Program Structure in C
1. The Documentation Section:
– consists of a set of comment lines giving the name of the
program, the author and other details
– Comment: // or /*……*/, giving a description of what the
program is.
2. The Pre-processor directive or Link section:
– It provides instruction to the compiler to link some functions or
do some processing prior to program execution. Contain header
files.
– #include <stdio.h>
• Allows standard input output operations
3. Global Declaration Section :
– variables that are used in more than one function are called
global variables and are declared in this section
Program Structure in C
4. The main() function section :
– Every C program must have one main() function. section
contains two parts, declaration part and executable part.
– Braces {}.
4.1. printf (“Hello World\n”);
Instructs the computer to perform an action
Specifically prints the string of characters within the (“ ”),
Entire line is called a statement and all statements end with a semicolon
(;)
Escape character (\)
(\n) is the new line character.
5. Sub program Section :
– It contains all the user defined functions that are called in
the main() function. User defined functions are generally
placed immediately after the main function
8
Executing a C program
• Steps:
1. Creating a program
2. Compiling the program
3. Linking the program with functions that
are needed from the C library.
4. Executing the program
Creating a program:
1. Open Borland C++ compiler.
Then open a new blank page
2. Use the keyboard to type the source code
3. Save the source code. You should name the file hello.c
Compiling and linking the program:
4. Compile and link welcome.c
5. Check the compiler messages. If you receive no errors or warnings,
everything should be okay.
6. If there is errors, go back and correct them and compile again.
7. If you made an error typing the program, the compiler will catch it
and display an error message. For example, if you misspelled the
word printf as prntf, you would see a message similar to the
following:
Error: undefined symbols: _prntf in welcome.c (welcome.obj)
Executing the program:
1. To execute, or run, WELCOME.EXE, simply click the run button.
The message Hello world! is displayed on-screen.
Program code
Enter program
Source program
Edit source program
C compiler Compile source program
Syntax errors?
Object code
System library Link with system library
Executable object code
Execute object code
Input data
Logic error
Data error Logic & data errors?
Correct output stop
Program errors
1. Syntax/Compile time errors. These are errors identified by the
compiler when it is compiling your program. They may include syntax
errors and wrong data types. A program will not execute until when a
compile time error is corrected.
2. Runtime errors. These occur during the execution of a program and
cause a program to abort abnormally e.g. division by zero, infinite
looping.
3. Logical errors. The program compiles and runs normally but it
produces wrong results. A logical error may occur when:
a. · A value is calculated incorrectly e.g instead of addition you
subtract.
b. · When a graphical button does not appear in the correct place.
Logical errors are the most difficult to debug because they manifest
themselves in many ways when their root cause is different.
Debugging is the process of finding and correcting errors/bugs in a
program.
Data Type Keyword Bytes Description Range
Required
Character char 1 integer quantity -128 to 127
Interger int 2/4 single character -32768 to 32767
Single- float (6 4 floating-point number 3.2E-38 to 3.4E381
precision digits.) (i.e. a number
floatingpoint containing a
decimalpoint and or an
exponent)
Double- double (19 8 double-precision 1.7E-308 to 1.8E3082
precision digits.) floating-point number
floatingpoint (i.e., more significant
figures, and an exponent
which may be larger in
magnitude)
2,147,438,647
C character set
A character indicates any English alphabet, digit or special
symbol
The C language character set includes:
Letter, Uppercase A ….. Z, Lower case a….z
Digits, 0….9.
Special Characters: comma, period. semicolon; colon:
question mark? Apostrophe’ quotation mark “
vertical bar | slash / backslash \ underscore _
dollar $ percent % hash # ampersand & , (, ),
[,], {, }
Identifiers and Keywords
• Identifiers are the names given by user to various
program elements such as variables, functions
and arrays.
• Rules for identifiers:
1. Letters, digits and underscore can be used.
2. Must not start with a digit.
3. Avoid using underscore at the start of identifier
4. White space is not allowed between names
5. Can not use any special character
6. Identifier cannot be same as reserved word (Key
Word) or C library function names
Key words or Reserve words of the C language are the
words whose meaning is already defined and explained to
the C language compiler.
Reserve words can not be used as identifiers or variable
names.
Keywords
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
Variables
• A variable is an identifier that may be used to store data
value.
• A value or a quantity which may vary during the program
execution can be called as a variable. Each variable has a
specific memory location in memory unit, where numerical
values or characters can be stored.
• Thus variable name refers to the location of the memory in
which a particular data can be stored.
• Variables names are also called as identifiers since they
identify the varying quantities.
– For Ex : sum = a+b.
– In this equation sum, a and b are the identifiers or
variable names representing the numbers stored in the
memory locations.
Rules to be followed for constructing the Variable
names(identifiers)
1. They must begin with a letter and underscore is considered
as a letter.
2. It must consist of single letter or sequence of letters, digits or
3. underscore character.
4. Uppercase and lowercase are significant. For ex: Sum, SUM
and sum are three distinct variables.
5. Keywords are not allowed in variable names.
6. Special characters except the underscore are not allowed.
7. White space is also not allowed.
Variable Declarations
In the program data types are written as given below.
(i) Integer - int
(ii) Floating point - float
(iii) Double floating point - double
(vi) Character – char
A declaration associates a group of variables with a specific data types.
All variables must be declared before they appear in executable statements
The syntax of variable declaration and some examples are given below.
Syntax :
Data type ‘variable’
For example:
int a; float c; char name;
int sam; double count;
If two or more variables of the same data type has to be declared they can be
clubbed as shown in example given below.
i. int a; int b; int c;
this is same as
int a, b, c;
ii. float d; float e; float f;
this can be written as
Assigning Values to Variables
Values can be assigned to variables using assignment operator “=”.
Syntax:
Variable name = value;
a = 10;
It is also possible to assign a value to a variable at the time the variable is declared. The
process of giving initial value to the variable is called initialization of the variable.
For Example:
int a=10, b=5;
float x=10.5, y=1.2e-9
The data item can be accessed in the program simply by referring to the variable name.
Data type associated with the variable cannot be changed.
However variables hold the most recently assigned data.
Int a, b, c;
Char d;
---
a = 3;
b = 5;
c=a+b;
---
a = 4;
b = 5;
// operating with variables
.
#include <stdio.h>
main ()
{
// declaring variables:
int a, b;
int result;
// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;
// print out the result:
printf ( “Result = %d”, result);
// terminate the program:
return 0;
}
21
Constants
• Contain values that do not change.
• C constants:
1. Character Constants
2. String Constants
3. Symbolic constant
4. Integer constants
a) decimal constants (base 10),
b) Octal constants (base 8)
c) hexadecimal constants (base 16).
Integer constants
Integer constants have to follow the set of rules:
Constant must have at least one digit.
Must not have decimal point.
Constant can be preceded by minus (-) sign, to
indicate negative constants.
Positive integer constants can be preceded by
either positive sign or no sign.
Commas and blank spaces can’t be included with
in the constant.
A) Decimal Integer Constants
These consists of any combination of digits taken
from the set 0 to 9. If the constant consists of two or more
digits, the first digit must be other than 0.
Valid decimal integer constants:
0 1 743 32767 8888
Invalid decimal integer constants
080 First digit cannot be zero
50.3 Illegal character ( . )
12,542 Illegal character ( , )
25 35 60 Illegal character ( Blank Space )
B) Octal Integer Constants
These consists of combination of digits taken from the
set 0 through 7. However the first digit must be 0, in order
to identify the constant as an octal number.
Valid octal integer constants
00 06 0753 0663
Invalid octal integer constants
543 Does not begin with zero
07865 Illegal character ( 8 )
06, 593 Illegal character ( , )
06.512 Illegal character ( . )
C) Hexadecimal Integer Constants
Hexadecimal integer is identified by ox or OX.
They consists of digits taken from the set of 0 to 9 and
letters taken from the set A to F (upper or lower case).
The letter A to F represent the decimal values from 10
to 15 respectively i.e., a=10, b=11, c=12, d=13, e=14
and f=15.
Valid hexadecimal integer constants:
0x0 0x1a 0x1BEC 0x7FFF
Invalid hexadecimal integer constants
0x16.8b Illegal character ( . )
563c Does not begin with 0 or 0x
0x7bcg Illegal character ( g )
0x12.53 Illegal character ( . )
Character Constants
A character constant is a single alphabet a
single digit or a single special symbol enclosed
in apostrophes (i.e. Single quotation marks).
Here both quotation marks should point to left
for example is ’A’ valid character constant
Example:
‘A’
‘b’
‘3’
String Constants
String constants consist of any number of
consecutive characters enclosed in (double)
equation marks.
“green”
“Rs95.50”
“Hi \n How are \n you”
The string “Hi \n How are \n You” is printed as
Hi
How are
You ?
Symbolic Constants
They are represented as follows:
#define BLOCK_SIZE 100
#define TRACK_SIZE (16*BLOCK_SIZE)
#define HELLO "Hello World\n"
#define EXP 2.7183
Syntax:
#define symbolic name value
printf(HELLO);
prints the string Hello World.
Question bank
1. Give classification of c character set
2. State the rules for identifiers. Are uppercase
letters equivalent to lowercase letters? Can digits
be included in an identifier name? Can any special
characters be included?
3. What are keywords in c ? Can they be used as
variable names? Justify your answer.
4. Explain basic data types along with their qualifiers.
5. Give classification of constants and explain them.
6. What are variables and how are they classified ?