0% found this document useful (0 votes)
1 views19 pages

Module II-2

This document provides an introduction to C programming, focusing on algorithms, flowcharts, and the fundamental concepts of variables and data types. It outlines the characteristics of algorithms, the advantages of flowcharts, and the features and limitations of the C programming language. Additionally, it explains variable declarations, data types, and user-defined types, including examples for better understanding.

Uploaded by

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

Module II-2

This document provides an introduction to C programming, focusing on algorithms, flowcharts, and the fundamental concepts of variables and data types. It outlines the characteristics of algorithms, the advantages of flowcharts, and the features and limitations of the C programming language. Additionally, it explains variable declarations, data types, and user-defined types, including examples for better understanding.

Uploaded by

swalihhilaws
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MODULE II

Introduction to C programming

ALGORITHM:

The word “algorithm” relates to the name of the mathematician Al-khowarizmi, which
means a procedure or a technique. An algorithm is a sequence of steps to solve a
particular problem or algorithm is an ordered set of steps that produces a result and
terminates in a finite time.

Algorithm has the following characteristics

• Input: An algorithm may or may not require input

• Output: Each algorithm is expected to produce at least one result

• Definiteness: Each instruction must be clear and unambiguous.

• Finiteness: If the instructions of an algorithm are executed, the algorithm should


terminate after finite number of steps

The algorithm and flowchart include following three types of control structures.

1. Sequence: In the sequence structure, statements are placed one after the other and
the execution takes place starting from up to down.

2. Branching (Selection): In branch control, there is a condition and according to a


condition, a decision of either TRUE or FALSE is achieved. In the case of TRUE, one
of the two branches is explored; but in the case of FALSE condition, the other
alternative is taken. Generally, the ‘IF-THEN’ is used to represent branch control.

3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be executed


repeatedly based on certain loop condition e.g. WHILE, FOR loops.

Advantages of algorithm

• It is a step-wise representation of a solution to a given problem, which makes it easy


to understand. • An algorithm uses a definite procedure.
1
• It is not dependent on any programming language, so it is easy to understand for
anyone even without programming knowledge.

• Every step in an algorithm has its own logical sequence so it is easy to debug.

HOW TO WRITE ALGORITHMS

Step 1: Define your algorithms input

Step 2: Define the variables

Step 3 : Outline the algorithm's operations

Step 4: Output the results of your algorithm's operations

FLOWCHART:

The first design of flowchart goes back to 1945 which was designed by John Von
Neumann. Unlike an algorithm, Flowchart uses different symbols to design a solution
to a problem. It is another commonly used programming tool. By looking at a
Flowchart one can understand the operations and sequence of operations performed in
a system. Flowchart is often considered as a blueprint of a design used for solving a
specific problem.

Advantages of flowchart:

• Flowchart is an excellent way of communicating the logic of a program.

• Easy and efficient to analyse problem using flowchart.

• During program development cycle, the flowchart plays the role of a blueprint,
which makes program development process easier.

• After successful development of a program, it needs continuous timely maintenance


during the course of its operation. The flowchart makes program or system
maintenance easier.

• It is easy to convert the flowchart into any programming language code.

2
Flowchart is diagrammatic /Graphical representation of sequence of steps to solve a
problem. To draw a flowchart following standard symbols are used.

Symbols used in Flowchart

Program writing

C is a high-level structured oriented programming language, used in general-purpose


programming, developed by Dennis Ritchie at AT&T Bell Labs, the USA between
1969 and 1973.

Features of C Programming

 C is a robust language with a rich set of built-in functions and operators.

 Programs written in C are efficient and fast.

3
 C is highly portable; programs once written in C can be run on other machines with
minor or no modification.

 C is a collection of C library functions; we can also create our function and add it to
the C library.

 C is easily extensible.

Advantages of C

 C is the building block for many other programming languages.

 Programs written in C are highly portable.

 Several standard functions are there (like in-built) that can be used to develop
programs.

 C programs are collections of C library functions, and it's also easy to add functions
to the C library.

 The modular structure makes code debugging, maintenance, and testing easier.

Disadvantages of C

 C does not provide Object Oriented Programming (OOP) concepts.

 There are no concepts of Namespace in C.

 C does not provide binding or wrapping up of data in a single unit.

 C does not provide Constructor and Destructor.

The limitations of C programming languages are as follows:

 Difficult to debug.

 C allows a lot of freedom in writing code, and that is why you can put an empty line
or white space anywhere in the program. And because there is no fixed place to start
or end the line, so it is difficult to read and understand the program.

4
 C compilers can only identify errors and are incapable of handling exceptions (run-
time errors).

 C provides no data protection.

 It also doesn't feature reusability of source code extensively.

 It does not provide strict data type checking (for example an integer value can be
passed for floating datatype).

Source Code:

Source code is the fundamental component of a computer program that is created by a


programmer. It can be read and easily understood by a human being. When a
programmer types a sequence of C programming language statements into Windows
Notepad, for example, and saves the sequence as a text file, the text file is said to
contain the source code. In C, we save the source code as <filename.c>.

5
Eg: sum.c

Object Code:

Object code is a set of instruction codes that is understood by a computer at the lowest
hardware level. Object code is usually produced by a compiler that reads some higher
level computer language source instructions and translates them into equivalent
machine language instructions.

Eg: [Link]

Executable file:

A .c file is a source code file while an .exe file is the executable file, which is
obtained after the successful compilation and proper linking and loading of the object
code.

Eg: [Link]

Variables

If you declare a variable in C (later on we talk about how to do this), you ask the
operating system for a piece of memory. This piece of memory you give a name and
you can store something in that piece of memory (for later use). There are two basic
kinds of variables in C which are numeric and character.

1. Numeric variables
2. Character variables

1. Numeric variables

Numeric variables can either be of the type integer (int) or of the type real (float).
Integer (int) values are whole numbers (like 10 or -10). Real (float) values can have a
decimal point in them. (Like 1.23 or -20.123).

2. Character variables
6
Character variables are letters of the alphabet, ASCII characters or numbers 0-9. If
you declare a character variable you must always put the character between single
quotes (like so ‘A’). So, remember a number without single quotes is not the same as
a character with single quotes.

Constants

The difference between variables and constants is that variables can change their
value at any time but constants can never change their value. (The constants value is
locked for the duration of the program). Constants can be very useful, Pi for instance
is a good example to declare as a constant.

Declaration and rules for naming variables:

Declaring a variable is very easy. First you have to declare the type-name. After the
type-name you place the name of the variable. The name of a variable can be
anything you like as long it includes only letters, underscores or numbers (However
you cannot start the name with a number). It is easier if a variable name reflects the
use of that variable. (For instance: if you name a float PI, you always know what it
means).

It is possible to declare more than one variable at the same time:

Data Types

So, you now know that there are three types of variables: numeric – integer,
numeric-real and character. A variable has a type-name, a type and a range
(minimum / maximum). In the following table you can see the type-name, type and
range:

Type-
Type Range
name

int Numeric – Integer -32 768 to 32 767

7
short Numeric – Integer -32 768 to 32 767

-2 147 483 648 to 2


long Numeric – Integer
147 483 647
1.2 X 10-38 to 3.4 X
float Numeric – Real
1038
2.2 X 10-308 to 1.8 X
double Numeric – Real
10308

char Character All ASCII characters

Eg:

void main()

int a=5,b=2,sum;

sum=a+b;

printf(“Sum= ”,sum);

getch();

To declare a constant is not much different then declaring a variable. The only
difference is that you have the word ‘const’ in front of it:

int main()

const float PI = 3.14;

8
char = 'A';

return 0;

Note: As you can see, you can assign a value with the equal sign during declaration.

Signed and unsigned variables

The difference between signed and unsigned variables is that signed variables can
be either negative or positive but unsigned variables can only be positive. By using
an unsigned variable you can increase the maximum positive range. When you
declare a variable in the normal way it is automatically a signed variable. To
declare an unsigned variable you just put the word unsigned before your variable
declaration or signed for a signed variable although there is no reason to declare a
variable as signed since they already are.
Eg:
int main()
{
unsigned int positive_var;
signed int negandposit_var;
int negandpositive var;
}

Calculations and variables

There are different operators that can be used for calculations which are listed in
the following table:

9
Operator Operation

+ Addition

– Subtraction

* Multiplication

/ Division

Modulus (Remainder of integer


%
division)

Now that we know the different operators, let’s calculate something:

int main()

int a, b;

a = 1;

b = a + 1;

a = b - 1;

return 0;

Data types

10
A data-type in C programming is a set of values and is determined to act on those
values. C provides various types of data-types which allow the programmer to select
the appropriate type for the variable to set its value.

The data-type in a programming language is the collection of data with values having
fixed meaning as well as characteristics. Some of them are an integer, floating point,
character, etc. Usually, programming languages specify the range values for given
data-type.

C Data Types are used to:

 Identify the type of a variable when it declared.

 Identify the type of the return value of a function.

 Identify the type of a parameter expected by a function.

ANSI C provides three types of data types:

1. Primary (Built-in) Data Types: void, int, char, double and float.

2. Derived Data Types: Array, References, and Pointers.

3. User Defined Data Types: Structure, Union, and Enumeration.

Table of Contents

[Link] Data Types


[Link] of Primary Data Types with Variable Names
[Link] Data Types
[Link] Defined Data Types
[Link] Types and Variable Declarations in C

Primary Data Types

Every C compiler supports five primary data types:


11
void As the name suggests, it holds no value and is generally
used for specifying the type of function or what it returns. If
the function has a void type, it means that the function will
not return any value.

int Used to denote an integer type.

char Used to denote a character type.

float, double Used to denote a floating point type.

int *, float *, char Used to denote a pointer type.


*

Declaration of Primary Data Types with Variable Names

After taking suitable variable names, they need to be assigned with a data type. This is
how the data types are used along with variables:

Example:

int age;

char letter;

float height, width;

Derived Data Types

12
C supports three derived data types:

Data Types Description

Arrays Arrays are sequences of data items having homogeneous


values. They have adjacent memory locations to store values.

References Function pointers allow referencing functions with a


particular signature.

Pointers These are powerful C features which are used to access the
memory and deal with their addresses.

User Defined Data Types

C allows the feature called type definition which allows programmers to define their
identifier that would represent an existing data type. There are three such types:

Data Types Description

Structure It is a package of variables of different types under a single


name. This is done to handle data efficiently. "struct" keyword
is used to define a structure.

Union These allow storing various data types in the same memory
location. Programmers can define a union with different
members, but only a single member can contain a value at a
given time. It is used for

13
Enum Enumeration (or enum) is a user defined data type in C. It is
mainly used to assign names to integral constants, the names
make a program easy to read and maintain.

Example for enum

1. In this example, the for loop will run from i = 0 to i = 11, as initially the value of i is
Jan which is 0 and the value of Dec is 11.

#include<stdio.h>

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

int main()

int i;

for (i=Jan; i<=Dec; i++)

printf("%d ", i);

return 0;

Output:

0 1 2 3 4 5 6 7 8 9 10 11

[Link] enum names can have same value. For example, in the following C program
both ‘Failed’ and ‘Freezed’ have same value 0.

14
#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};
int main()
{
printf("%d, %d, %d", Working, Failed, Freezed);
return 0;
}
Output:

1, 0, 0

3. If we do not explicitly assign values to enum names, the compiler by default


assigns values starting from 0. For example, in the following C program, sunday gets
value 0, monday gets 1, and so on.

#include <stdio.h>

enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int main()

enum day d = thursday;

printf("The day number stored in d is %d", d);

return 0;

Output:

The day number stored in d is 4

15
4. We can assign values to some name in any order. All unassigned names get value as
value of previous name plus one.

#include <stdio.h>

enum day {sunday = 1, monday, tuesday = 5, wednesday, thursday = 10, friday,

saturday};

int main()

printf("%d %d %d %d %d %d %d", sunday, monday, tuesday, wednesday,

thursday, friday, saturday);

return 0;

Output:

1 2 5 6 10 11 12

4. All enum constants must be unique in their scope. For example, the following
program fails in compilation.

enum state {working, failed};

enum result {failed, passed};

int main()

16
{ return 0; }

Output:

Compile Error: 'failed' has a previous declaration as 'state failed'

Data Types and Variable Declarations in C

Example:

#include <stdio.h>

int main()

int a = 4000; // positive integer data type

float b = 5.2324; // float data type

char c = 'Z'; // char data type

long d = 41657; // long positive integer data type

long e = -21556; // long -ve integer data type

int f = -185; // -ve integer data type

short g = 130; // short +ve integer data type

short h = -130; // short -ve integer data type

double i = 4.1234567890; // double float data type

17
float j = -3.55; // float data type

The storage representation and machine instructions differ from machine to


machine. sizeof operator can use to get the exact size of a type or a variable on a
particular platform.

Example:

#include <stdio.h>

#include <limits.h>

int main()

printf("Storage size for int is: %d \n", sizeof(int));

printf("Storage size for char is: %d \n", sizeof(char));

return 0;

Reading and printing (Simple I/O statements)

To read input from the keyboard we will use the command scanf(). To print
something to the screen, we use printf().

#include<stdio.h>
int main()

18
{
int n;
scanf("%d", &n);
n = n * 10;
printf("Ten times the input equals %d\n",n);
return 0;
}

Note: The input must be a whole number (integer).

The %d is for reading or printing a decimal integer value (It is also possible to use
%i). In the table below you can find the commands for other types:

%i or %d int

%c char

%f float

%lf double

%s string

19

You might also like