0% found this document useful (0 votes)
17 views

1 Introduction

The document provides an introduction to programming concepts like algorithms, flowcharts, programs, variables, constants, data types, and memory. It discusses how variables and constants are stored in memory locations and mapped to addresses. Examples of flowcharts and programs to solve problems like addition, maximum of numbers, summation, and grade computation are presented. Basic C programming concepts like keywords, identifiers, tokens, and character set are also introduced. The document gives an overview of key concepts needed to start learning the C programming language.

Uploaded by

Aayushman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

1 Introduction

The document provides an introduction to programming concepts like algorithms, flowcharts, programs, variables, constants, data types, and memory. It discusses how variables and constants are stored in memory locations and mapped to addresses. Examples of flowcharts and programs to solve problems like addition, maximum of numbers, summation, and grade computation are presented. Basic C programming concepts like keywords, identifiers, tokens, and character set are also introduced. The document gives an overview of key concepts needed to start learning the C programming language.

Uploaded by

Aayushman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Introduction to Programming

Some Terminologies
◻ Algorithm / Flowchart
🞑A step-by-step procedure for solving a particular
problem.
🞑Independent of the programming language.

◻ Program
🞑A translation of the algorithm/flowchart into a form
that can be processed by a computer.
🞑Typically written in a high-level language like C,
C++, Java, etc.
Variables and Constants
◻ Most important concept for problem
solving using computers

◻ All temporary results are stored in terms of


variables
🞑 The value of a variable can be changed.
🞑 The value of a constant do not change.

◻ Where are they stored?


🞑 In main memory.
Variables and Constants…

◻ How does memory look like (logically)?


🞑As a list of storage locations, each having a
unique address.
🞑Variables and constants are stored in these
storage locations.
🞑A variable is like a bin
■ The contents of the bin is the value of the variable
■ The variable name is used to refer to the value of the
variable
■ A variable is mapped to a location of the memory,
called its address
Memory map
Address 0
Address 1
Address 2
Address 3
Every variable is
Address 4
mapped to a particular
Address 5
memory address
Address 6

Address N-1
Variables in Memory

Instruction executed Variable X

X = 10 10
T
i X = 20 20
m
e
X=X+1 21

X=X*5 105
Variables in Memory…
Variable
Instruction executed X Y

X = 20 20 ?
T
i Y = 15 20 15
m
e
X=Y+3 18 15

Y=X/6 18 3
Data Types
◻ Three common data types used:

🞑Integer :: can store only whole numbers


■ Examples: 25, -56, 1, 0

🞑Floating-point :: can store numbers with fractional


values.
■ Examples: 3.14159, 5.0, -12345.345

🞑Character :: can store a character


■ Examples: ‘A’, ‘a’, ‘*’, ‘3’, ‘ ’, ‘+’
Data Types…

◻ How are they stored in memory?


🞑Integer ::
■ 16 bits
Actual number of bits vary from
■ 32 bits
one computer to another
🞑Float ::
■ 32 bits
■ 64 bits
🞑Char ::
■ 8 bits (ASCII code)
■ 16 bits (UNICODE, used in Java)
Problem solving
◻ Step 1:
🞑Clearly specify the problem to be solved.
◻ Step 2:
🞑Draw flowchart or write algorithm.
◻ Step 3:
🞑Convert flowchart (algorithm) into program code.
◻ Step 4:
🞑Compile the program into object code.
◻ Step 5:
🞑Execute the program.
Flowchart: basic symbols

Computation

Input / Output

Decision Box

Start / Stop
Flowchart: basic symbols…

Flow of
control

Connector
Example 1: Adding three numbers
START

READ A,
B, C

S=A+B+C

OUTPUT
S

STOP
Example 2: Larger of two numbers

START

READ X,
Y

YES IS NO
X>Y?

OUTPUT OUTPUT
X Y

STOP STOP
Example 3: Largest of three numbers
START

READ X,
Y, Z

YES IS NO
X > Y?

Max = X Max = Y

IS
YES NO
Max >
OUTPUT Z? OUTPUT
Max Z
STOP STOP
Example 4: Sum of first N natural numbers
START

READ N

SUM = 0
COUNT = 1

SUM = SUM + COUNT

COUNT = COUNT + 1

NO IS YES OUTPUT
COUNT > SUM
N?
STOP
Example 5: SUM = 1 + 2 + 3 + N
2 2 2 2

START

READ N

SUM = 0
COUNT = 1

SUM = SUM + COUNT * COUNT

COUNT = COUNT +
1
NO IS YES OUTPUT
COUNT >
SUM
N?
STOP
Example 6: SUM = 1.2 + 2.3 + 3.4 + to N terms
START

READ N

SUM = 0
COUNT = 1

SUM = SUM + COUNT * (COUNT +


1)
COUNT = COUNT +
1
NO IS YES
COUNT > OUTPUT
N? SUM

STOP
Example 7: Computing Factorial
START

READ N

PROD = 1
COUNT = 1

PROD = PROD * COUNT

COUNT = COUNT + 1

NO IS YES OUTPUT
COUNT >
PROD
N?
STOP
Example 8: Roots of a quadratic equation

ax2 + bx + c = 0

TRY YOURSELF
Example 9: Grade computation
MARKS ≥ 90 🡺 Ex
89 ≥ MARKS ≥ 80 🡺 A
79 ≥ MARKS ≥ 70 🡺 B
69 ≥ MARKS ≥ 60 🡺 C
59 ≥ MARKS ≥ 50 🡺 D
49 ≥ MARKS ≥ 35 🡺 P
34 ≥ MARKS 🡺 F
Example 9: Grade computation…
START

READ
MARKS

MARK NO MARK NO MARK NO


S≥ S≥ S≥ A
90? 80? 70?
YES YES YES
OUTPUT OUTPUT OUTPUT
“Ex” “A” “B”

STOP STOP STOP


Programming in C: Basics
History of C
◻ Originally developed in the 1970’s by Dennis Ritchie at AT&T Bell
Laboratories.
🞑 Outgrowth of two earlier languages BCPL and B.

◻ Popularity became widespread by the mid 1980’s, with the


availability of compilers for various platforms.

◻ Standardization has been carried out to make the various C


implementations compatible.
🞑 American National Standards Institute (ANSI)
🞑 GNU
Why teach C?
◻ C is small (only 32 keywords).
◻ C is common (lots of C code about).
◻ C is stable (the language doesn’t change much).
◻ C is quick running.
◻ C is the basis for many other languages (Java,
C++, awk, Perl).
◻ It may not feel like it but C is one of the easiest
languages to learn.
Some programmer jargon
◻ Some words that will be used a lot:
🞑 Source code: The stuff you type into the computer. The program you are
writing.
🞑 Compile (build): Taking source code and making a program that the
computer can understand.
🞑 Executable: The compiled program that the computer can run.
🞑 Language: The core part of C central to writing C code.
🞑 Library: Added functions for C programming which are bolted on to do
certain tasks.
🞑 Header file: Files ending in .h which are included at the start of source
code.
Our First C Program: Hello World

Preprocessor

#include <stdio.h> Comments are good


/* This program prints “Hello World” */

main( ) means “start here”


main()
{
printf(“Hello World!\n”);
}
Library command
Brackets define code blocks
C doesn’t care much about spaces
#include <stdio.h> /* This program prints “Hello
World” */
int main( ) {printf(“Hello World!\n”);}

#include
<stdio.h>
/* This program Both of these programs are exactly
prints “Hello
the same as the original as far as
World”
*/
your compiler is concerned.
int
main( )
{
printf(“Hello
World!
\n”)
;
}
The C Character Set
◻ The C language alphabet:
🞑Uppercase letters ‘A’ to ‘Z’
🞑Lowercase letters ‘a’ to ‘z’
🞑Digits ‘0’ to ‘9’
🞑Certain special characters:
! # % ^ & * ( )
- _ + = ~ [ ] \

| ; : ‘ “ { } ,

. < > / ? blank


C Tokens
◻ Individual word or a punctuation marks
◻ In C, a smallest individual unit is called token.
C Tokens

Keywords Constants Strings Operators

Identifiers Special Symbols


Identifiers and Keywords
◻ Identifiers
🞑 Names given to various program elements (variables,
constants, functions, etc.)
🞑 May consist of letters, digits and the underscore (‘_’)
character, with no space between.
🞑 First character must be a letter or underscore.
🞑 An identifier can be arbitrary long.
■ Some C compilers recognize only the first few characters of
the name (16 or 31).
🞑 Case sensitive
■ ‘area’, ‘AREA’ and ‘Area’ are all different.
Valid and Invalid Identifiers
◻ Valid identifiers ◻ Invalid identifiers
X 10abc
abc my-name
simple_interest “hello”
a123 simple interest
LIST (area)
stud_name %rate
Empl_1
Empl_2
avg_empl_salary
Keywords of C
◻ Flow control – if, else, return, switch, case, default

◻ Loops – for, do, while, break, continue

◻ Common types – int, float, double, char, void

◻ Structures – struct, typedef, union

◻ Counting and sizing things – enum, sizeof

◻ Rare but still useful types – extern, signed, unsigned, long,


short, static, const

◻ Evil keywords which we avoid – goto

◻ Wierdies – auto, register, volatile


Constants
Constants

Numeric Character
Constants Constants

integer floating-point single string


character
Integer Constants

◻ Consists of a sequence of digits, with possibly a plus or a minus sign


before it.
🞑 Valid examples are 123, -321, 0, 654321, +78
🞑 Embedded spaces, commas and non-digit characters are not permitted
between digits.
■ E.g. 15,320 $23400
◻ Octal integer constants:
🞑 Consists of any combination of digits from the set 0 through 7 with a leading 0
🞑 E.g. 064, 012, 01, 037
◻ Hexadecimal constants:
🞑 Consists of any combination of digits from the set 0 to 9 and A to F or a to f.
🞑 Preceded by 0x or 0X
🞑 E.g. 0x1234, 0x35a, 0X390F
Integer Constants…

◻ Maximum and minimum values (for 32-bit representations)


Maximum :: 2147483647
Minimum :: – 2147483648
◻ Can use qualifiers
🞑 U, L and UL
🞑 E.g.

56789U Or 56789u Unsigned integer


987334335353UL Or Unsigned long
987334335353ul integer
8774343L Or 8774343l long integer
Integer Constants…
◻ Program
🞑 Representation of integer constants on a 16-bit computer

#include <stdio.h>
main( )
{
printf(“%d %d %d \n”, 32767, 32767+1, 32767+10);
printf(“%ld %ld %ld \n”, 32767L, 32767L+1L,
32767L+10L);

Output:

32767 -32768 -32677


32767 32768 32777
Real/floating point constants
◻ Can contain fractional parts.
◻ Very large or very small numbers can be represented.

◻ Two different notations:


1. Decimal notation e means “10 to the
25.0, 0.0034, .84, -2.234 power of”
2. Exponential (scientific) notation
mantissa e exponent
3.45e23, 0.123e-12, 123E2
Embedded white spaces are not allowed
Character and string constants
◻ Single character constants
🞑 ‘5’ ‘X’ ‘,’
🞑 Have integer values known as ASCII values
🞑 printf(“%d”, ‘a’); would print 97
🞑 printf(“%c”, ‘a’); would print a
◻ String constants
🞑 Sequence of characters enclosed in double quotes
🞑 E.g. “Hello” “72747” “?-sdfd” “5+3” “X”
🞑 ‘X’ and “X” are not same !!!!
◻ Backslash character constants
🞑 E.g. ‘\n’ ‘\b’ ‘\t’ ‘\v’ ‘\0’
Data Types
◻ Primary (or fundamental) data types
◻ Derived data types
◻ User-defined data types
Data Types…

Primary Data Types

Integral Types

Integer character
signed unsigned type char
int unsigned int signed char
short int unsigned short int unsigned char
long int unsigned long int
Floating point type
float void
double
long double
Data Types…
Type Size - bits(bytes) Range
char or signed char 8(1 byte) -128 to +127
unsigned char 8(1) 0 to 255
int or signed int 16(2) -32768 to +32767
unsigned int 16(2) 0 to 65535
short int or signed short 8(1) -128 to +127
int
unsigned short int 8(1) 0 to 255
long int or signed long int 32(4) -2147483648 to
+2147483647
unsigned long int 32(4) 0 to 4294967295
float 32(4) 3.4E-38 to 3.4E+38
double 64(8) 1.7E-308 to 1.7E+308
long double 80(10) 3.4E-4932 to 3.4E+4932
Declaration of Variables
◻ Declaration does two things:
🞑Tells the compiler what the variable name is
🞑Specifies what type of data the variable will hold
◻ Primary type declaration
data-type v1,v2, … vn;
◻ Valid declarations are
🞑int count;
🞑int number, total;
🞑double ratio;
Data types and their keyword equivalents

Data Type Keyword equivalent


Character char
Unsigned character unsigned char
Signed character signed char (char)
Signed integer signed int (or int)
Signed short integer signed short int (or short int or short)
Signed long integer signed long int (or long int or long)
Unsigned integer unsigned int (or unsigned)
Unsigned short integer unsigned short int (or unsigned short)
Unsigned long integer unsigned long int (or unsigned long)
Floating point float
Double precision floating point double
Extended double precision floating long double
point
A Program for declaration of variables

main()
{
float x,y;/8
int code;/2
short int count;/1
long int amount;/4
double deviation;/8
unsigned n;/2
char c;/1
}
User defined type declaration
◻ Allow user to define an identifier that would
represent an existing data type
◻ The user defined data type can later be used
to declare variables
typedef type identifier;
◻ e.g. typedef int units;
typedef float marks;
units batch1, batch2;
marks stud_1, stud_2;
User defined type declaration…
◻ Another user-defined data type is enumerated
data type
enum identifier {value1, value2, …
valuen};
1 2 3
enum identifier v1, v2, …vn;
0

v1=value3; v2=valuen;
enum day {Monday, Tuesday, Friday, Sunday};
enum day week_st, week_end;
week_st = Monday;
week_end = Sunday;
User defined type declaration…
◻ Another user-defined data type is enumerated
data type
enum identifier {value1, value2, …
valuen};
2 v1, v2,
enum identifier 3 …vn; 4
1
v1=value3; v2=valuen;
enum day {Monday=1, Tuesday=2, Friday=3,
Sunday=4} week_st, week_end;
week_st = Monday;
week_end = Sunday;
Assigning values to variables
◻ variable_name=constant;
◻ e.g.
🞑 initial_value =0;
🞑 final_value=100;
🞑 balance = 75.84;
🞑 yes = ‘x’;
🞑 year = year +1;
◻ Combined declaration and assignment
🞑 data_type variable_name = constant;
🞑 e.g.
■ int final_value = 100;
■ char yes = ‘x’;
■ double balance = 75.84;
◻ Initialization of more than one variables
🞑 p=q=s=0;
🞑 x = y = z = ‘a’;
Program illustrating assignments
main()
Output:
{
float x,p; m = -11234
double y,q; n = 1234567890
unsigned k;
int m=54321;//signed
x = 1.234567880630
long int n = 1234567890; x = 1.234568
x=1.234567890000;
y = 9.876543210000
y=9.87654321;
k=54321; y = 9.876543
p=q=1.0; k = 54321 p = 1.000000 q =
printf(“m = %d\n”, m); 1.000000000000
printf(“n = %ld\n”, n);
printf(“x = %.12f\n”, x);
printf(“x = %f\n”, x);
printf(“y = %.12f\n”, y);
printf(“y = %lf\n”, y);
printf(“k = %u p = %f q = %.12f \n”, k,p,q);
}
Reading data from keyboard
◻ using scanf function
scanf(“Control string”, &variable1,
&variable2, …);
◻ e.g.
scanf(“%d”, &number);
scanf(“%d %f %d”, &intvar1,
&floatvar, &intvar2);
◻ A program to illustrate scanf
Reading data from keyboard
◻ A program to illustrate scanf
main()
{
int number;
printf(“Enter the number\n”);
scanf(“%d”,&number);
}
Defining symbolic constants
◻ Certain constants that appear repeatedly in a
number of places in the program
◻ e.g. 3.14 i.e. the value of pi
◻ We face two problems in the subsequent use
of such program
🞑problem in modification of the program, and
🞑problem in understanding the program
Defining symbolic constants…

#define symbolic-name value of constant


◻ e.g.
🞑#define PI 3.14159
🞑#define STRENGTH 100
🞑#define PASS_MARK 50
🞑#define MAX 200
Defining symbolic constants…
◻ Check the validity of the following statements:
Statement Validity
#define MAX=5;
# define True 1;
#define TRUE 1
#define N 25
#Define ARRAY 11
#define PRICE$ 100
#DEFINE symbol 10;
#define symbol 10;
#define N 5, M 10
Defining symbolic constants…
◻ Check the validity of the following statements:
Statement Validity
#define MAX=5; Invalid
# define True 1; Invalid
#define TRUE 1 Valid
#define N 25 Valid
#Define ARRAY 11 Invalid
#define PRICE$ 100 Invalid
#DEFINE symbol 10; Invalid
#define symbol 10; Invalid
#define N 5, M 10 Invalid
Declaring a variable as constant
◻ If one may like to have value of certain
variables to remain constant during the
execution of a program
const int class_size = 40;
Declaring a variable as volatile
◻ Can be used to tell explicitly the compiler that
a variable’s value may be changed at any time
by some external sources
volatile int date;
◻ Then what about the following C statement?
volatile const int location = 100;
Desirable Programming Style
◻ Clarity
🞑 The program should be clearly written.
🞑 It should be easy to follow the program logic.

◻ Meaningful variable names


🞑 Make variable/constant names meaningful to enhance program clarity.
■ ‘area’ instead of ‘a’
■ ‘radius’ instead of ‘r’

◻ Program documentation
🞑 Insert comments in the program to make it easy to understand.
🞑 Never use too many comments.

◻ Program indentation
🞑 Use proper indentation.
🞑 Structure of the program should be immediately visible.
Indentation Example: Good Style
#include <stdio.h>

/* FIND THE LARGEST OF THREE NUMBERS */

main()
{
int a, b, c;

scanf(“%d%d%d”, &a, &b, &c);

if ((a>b) && (a>c))


printf(“\n Largest is %d”, a);
else
if (b>c)
printf(“\n Largest is %d”, b);
else
printf(“\n Largest is %d”, c);
}
Indentation Example: Bad Style
#include <stdio.h>

/* FIND THE LARGEST OF THREE NUMBERS */


main()
{
int a, b, c;
scanf(“%d%d%d”, &a, &b, &c);
if ((a>b) && (a>c))
printf(“\n Largest is %d”, a);
else
if (b>c)
printf(“\n Largest is %d”, b);
else
printf(“\n Largest is %d”, c);
}
Assignment-1
◻ At the end of each chapter, you will be given a list
(selected) of assignment questions.
◻ Prepare 200 pages full scape assignment book and
write down all assignments in that.
◻ This carries some weightage in your internal marks.
◻ Solve the following exercise questions from the book
by E. Balagurusamy
◻ 2.1, 2.2, 2.15, 2.16, 2.17, 2.18, 2.19

You might also like