Module 2slides
Module 2slides
com/c/EDULINEFORCSE
STUDENTS
MODULE 2
PROGRAM BASICS
STRUCTURE OF A C PROGRAM
DOCUMENTATION SECTION comments
LINKAGE SECTION #include files
DEFINITION SECTION #define
GLOBAL DECLARATION SECTION variable and functions
Function 1 ( )
Function 2 ( )
……………
Function N ( )
Points to remember
• Every statement in C should end with a semicolon.
• In C everything is written in lower case. However upper case letters
used for symbolic names representing constants.
• #include is a pre-processor directive.
• Stdio.h is a header file, for standard input output functions. It
activate keyboard and monitor.
• Single line comment is represented using //
• Multiple line comment is represented using /*……………….*/
• Comment lines are not executable statements and therefore
anything between/*and*/ is ignored by the compiler.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 4
C PROGRAM TOKENS
The smallest individual unit in a program is known as tokens. C has
the following tokens
1. Keywords: these are the words whose meaning has already been
explained to the C compiler. The keywords cannot be used as
variable names. The keywords are also known as “Reserved
words”. There are only 32 keywords in C. Examples are char,
double, void, if
2. Identifiers: These are fundamental building block of a program.
Identifiers refer to the names of variables; functions and arrays.
These are user-defined names and consist of a sequence of
letters and digits with the letter as a first character.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 6
3. Constants (literals): These are data items that never change their
value during a program run. These are fixed values. The types of
constants are
a) Integer constant: These are whole numbers without any
fractional part. It must have at least one digit and must not
contain any decimal point. It can be either positive or negative.
Examples are 426,+200,-760
b) Character constant: A character constant is one character
enclosed in single quotes. Examples are ‘A’, ’5’ ,’=’ . A character
constant have corresponding ASCII (American Standard Code for
Information Interchange) values. For example ASCII value of ‘A’ is
65 and ASCII value of ‘a’ is 97.
4. Punctuators :
[ ] Bracket (Arrays)
( ) Parentheses (Function call & Function parameters)
{ } Braces (Opening and closing of executable statements)
, Comma (Separator)
; Semicolon (Terminator)
: Colon (Labelled statement)
* Asterisk (Pointer declaration & Multiplication operator)
# Pound sign OR called Hash (Preprocessor directive)
= Equal to (Assignment operator)
BITWISE OPERATORS
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
LOGICAL OPERATORS
&& Logical AND
| | Logical OR
! Logical NOT
ASSIGNMENT OPERATORS
= Assignment
*= Assign product
/= Assign quotient
+= Assign sum
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 13
RELATIONAL OPERATORS
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to
COMPONENT SELECTION
. Direct component selector
→ Indirect component selector
1st
* / % multiplication, division, modular division
3rd
= assignment
a x b – c xd a*b–c*d
(m + n) (a + b) (m + n) * (a + b)
3x2 + 2x + 5 3*x*x+2*x+5
a + b +c (a+b+c)/(d+e)
d +e
Let us consider the following program which add two numbers and
print the result. The following program uses the tokens which we
discussed above and you can see the structure of the normal C
program also
DATA TYPES
Data type is a classification identifying one of various types of data. There are
three classes of data types.
• Fundamental (primary) Data types
• Derived Data types
• User defined data types
Fundamental (primary) data types
1. int : for integers (2 byte memory space allocates in memory)
2. char : for characters(1 byte memory space allocates in memory)
3. float : for floating point numbers(4 byte memory space allocates in memory)
4. double : for double precision floating point numbers(8 byte memory space
allocates in memory)
5. void: for empty set of values and non-returning functions. The void type has no
value.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 24
• Enumerated means all the values are listed. The enum specifier
automatically enumerates a list of words by assigning them values
0, 1, 2, 3, and so on.
• The compiler automatically assigns integer digits beginning with 0
to all the enumeration constants. That is the enumeration constant
Monday is assigned 0, Tuesday is assigned 1 and so on.
• We can give values explicitly (changing default original values) by
the following way
enum day {Monday=1, Tuesday, Wednesday=6, Thursday, Friday….};
Now, Monday=1, Tuesday=2, Wednesday=6, Thursday=7, Friday=8
etc.
4. Union:
• Structure and union are same but different in memory allocation.
• A union can be declared using the keyword is union.
• A union is a memory location that is shared by two or more
different variables, generally of different types at different times.
• Defining a union is similar to defining a structure.
VARIABLES
A variable is a data name that may be used to store a data value.
A variable may take different values at different times during
execution.
Each variable has a specific storage location in memory where its
value is stored. The variables are called symbolic variables because
these are named.
There are two values associated with a symbolic variable.
• Data value: stored at some location in memory. This is sometimes
referred to as a variable’s r- value.
• Location value: This is the address in memory at which its data
value is stored. This is sometimes referred to as variable’s l-value.
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 31
Variable Declaration
A declaration associates a group of variables with a specific data
type. Declaration of variables must be done before they are used in
the program.
Syntax
Data_ type variable_ name;
Eg: int a;
float a, b, c;
char name [10]; // Character array declaration
Variable Initialization
• The process of giving initial values to variables is called
initialization.
Eg: int x=10;
float n=22.889;
char answer=’y’;
Expression
• An expression represents a single data item, such as number or a
character. Expression can also represent logical conditions
Eg: x+y
y=z
x=y+z
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 34
#include <stdio.h>
#include<conio.h>
void main ( )
{
int x, y,
float sum;
printf (“enter two integer numbers”);
scanf (“%d%d”,&x,&y);
sum =(float) x+y;
printf (“The sum is %f”,sum);
getch ( );
}
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 40
PREPROCESSOR DIRECTIVES
The pre-processor is a program that processes the source code
before it passes through the compiler. It operates under the control
of directives.
Before the source code passes through the compiler, it is examined
by the pre-processor for any pre-processor directives.
If there are any, appropriate actions are taken and then the source
program is handed over to the compiler. The three categories of
directives are
1. Macro substitution directives (#define)
2. File inclusion directives (#include)
3. Compiler control directives (#if, #else, #endif)
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 43
MACRO SUBSTITUTION
We can define a symbolic constant using #define. It has the
following form
Syntax
#define symbolic_ name value_ of_ constant
Eg: #define NUMVAL 100
#define statement must not end with semi colon. Symbolic names
are written in CAPITALS.
Eg: #include<stdio.h>
#define VALUE 25
void main( )
{
int i ;
for ( i = 1 ; i <= VALUE ; i++ )
printf ( "\n%d", i ) ;
}
In this program, instead of writing 25 in the for loop we are writing
it in the form of VALUE, which has already been defined before
main ( ) through the statement, #define VALUE 25
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 46
2. putchar( )
• It displays a single character on the screen. This function takes one
argument, which is the character to be sent. It also returns this
character as its result. The general form is
putchar ( variable-name);
Eg: char ans=’y’
putchar (ans);
3. getche ( )
• The ‘e’ in getche () function means it echoes (displays) the character.
4. gets( ) : receives a string from the keyboard.
Syntax
2. IF…..ELSE STATEMENT
• Here, either true block or false block will be executed, not both. In both
cases control is transferred to the statement-x.
Syntax
if (test expression)
{
True statements;
}
else
{
False statements;
}
Statement-x;
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 57
3. NESTED IF……ELSE
• When a series of decisions are involved, we may to use more than one
if….else statement in nested form
Syntax Example
4. IF ELSE IF LADDER
Syntax Example
5. SWITCH STATEMENT
• The switch statement provides a way of choosing between a set of
alternatives, based on the value of an expression. It allows us to
make a decision from the number of choices.
Syntax
Example
You are also allowed to use char values in case and switch as shown in the
following program:
1. WHILE LOOP
• It is an entry control loop. In a while loop, loop control variable
should be initialized before the loop begins. The loop variable
should be updated inside the body of the while
Syntax
initialize loop counter;
while (test loop counter using a condition)
{
do this;
and this;
increment loop counter;
}
Prepared By Mr.EBIN PM, AP, IESCE EDULINE 69
2. FOR LOOP
• It is an entry control loop.
Syntax
for (initialization ; test-condition ; increment)
{ body of the loop
}
• Initialization of the control variable is done first. The variable ‘i’ is
known as loop control variable.
• Next check the test condition. If it is true, the body of the loop is
executed. After this, the control variable is incremented
OUTPUT
0 1 2 3 4 5 6 7
3. DO –WHILE LOOP
• It is an exit control loop. That is, it evaluates its test expression at
the bottom of the loop after executing its loop body statement.
• do -while loop execute at least once even when the test expression
evaluates to false initially
Syntax
do
{
this;
and this;
and this;
} while (this condition is true);
NESTING OF LOOPS
• The way if statements can be nested, similarly while and for can
also be nested. To understand how nested loops work; look at the
program given below:
• OUTPUT
Eg:
Eg:
OUTPUT
12
21