Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views43 pages

Module 1

The document outlines the course 'Principles of Programming Using C', detailing its structure, evaluation pattern, and key topics including computer fundamentals, programming design tools, algorithms, and the C programming language. It includes information on textbooks, reference materials, and online resources, as well as characteristics of C, input/output operations, and error types. Additionally, it provides examples of algorithms and programming assignments to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views43 pages

Module 1

The document outlines the course 'Principles of Programming Using C', detailing its structure, evaluation pattern, and key topics including computer fundamentals, programming design tools, algorithms, and the C programming language. It includes information on textbooks, reference materials, and online resources, as well as characteristics of C, input/output operations, and error types. Additionally, it provides examples of algorithms and programming assignments to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Course : Principles of Programming Using C

Course Code: BPOPS203


Credits: 3
L:T:P - 2:0:2
Suhas Bharadwaj R
Assistant Professor
Department of Information Science & Engineering
NIE, Mysuru
Textbooks:
1. Computer Fundamentals and Programming in C,“Reema Thareja”, 2nd Edition, Oxford University, 2017.

Reference Books:
1. E. Balaguruswamy, Programming in ANSI C, 7th Edition,Tata McGraw-Hill, 2017.
2. Fundamentals of Computers,V Rajaraman, 6th Edition, PHI, 2014.

Online Resources:
1. https://nptel.ac.in/courses/106/105/106105171/
Evaluation pattern
Module 1: Introduction to Computers
&C
Computers: Electronic device that is designed to accept data, perform the required mathematical and
logical operations at high speed and output the result.

• In the past, computers were extremely large in size and often required an entire room for installation
• With technological advancements, the size of computers became smaller and their energy requirements reduced immensely
Characteristics of Computers:
Stored Program Concept :
• A stored program architecture is a fundamental computer architecture wherein, the computer executes the
instructions that are stored in in its memory.
• Before any data is processed, instructions are read into memory.
• Instructions are stored in the computer’s memory for execution.
• Instructions are stored in binary form (using binary numbers—only 0s and 1s).
• Processing starts with the first instruction in the program, which is copied into a control unit circuit. The control unit
executes the instructions.
• Instructions written by the users are performed sequentially until there is a break in the current flow.
• Input/Output and processing operations are performed simultaneously. While data is being read/written, the central
processing unit (CPU) executes another program in the memory that is ready for execution
Classification of Computers:
• Supercomputers
• Mainframe computers
• Minicomputers
• Microcomputers
Applications Of Computers:
• Word processing
• Internet / Networks
• Digital video or audio composition
• Desktop publishing (Page layouts)
• e-Business/ education
• Bioinformatics (biological information)
• Health care ( Storing records, surgical procedures)
• Geographic Information System and Remote Sensing
• Meteorology
• Multi-media and Animation
• Decision Support Systems
• Expert Systems, many more…...
Input and output devices:
Output Devices:
Designing efficient Programs
Programming design tools: Algorithms, Flowchart, Pseudocode
Algorithms:
• Step by step Procedure for performing some calculation, which is implemented in some formal language.
• It is considered as to be an effective method for solving a problem in a finite number of steps.
• Mainly used to achieve the software reuse.
Characteristics of Algorithms:
• Be precise
• Be unambiguous
• Not even a single instruction should be repeated indefinitely.
• Should end properly
• After the algorithm gets terminated, the desired result must be obtained.

Control structures:
1. Sequence
2. Decision
3. Repetition
Algorithm to Add two numbers:

Sequence

Algorithm to Check equality of two numbers:

Decision
Algorithm to print first 10 natural numbers:
Repetition:
Assignment:
1. Algorithm to print first 10 Natural number
2. Algorithm for swapping two numbers
3. Algorithm to find if the number is odd or even
4. Algorithm to find grade obtained by student
5. Algorithm to find sum of first N natural numbers.
Flowcharts:
• Graphical or symbolical representation of the process/ steps of programs
• To visualize complex process.
Flowchart to calculate the sum of the first 10 natural numbers.
Flow chart to determine the largest of three numbers:
Type of errors:
Run time errors
Runtime error occurs when the program is being run executed (divide by zero, file not exists, array out of bound etc)

Compile time errors


1) syntax errors - rules of a programming languages are violated.( missing semi-colon, missing brackets etc)
2) semantic errors- not meaningful to compiler (Not declaring a variable before using it, Performing an incorrect
operation on a primitive type value)

logical errors - undesirable output or wring output.s


linker errors- linker is not able to find the function definition for a given prototype or problems with header files.
Introduction to C
Characteristics of C
1. C is a high-level programming language
2. C has majorly 32 keywords
3. extensive use of function calls
4. well suited for structural programming
5. stable language
6. quicker than a program written in any other language
7. facilitates bitwise program
8. if you know C, learning other computer languages becomes much easier.
9. C is a portable language. ( Not hardware dependant)
10. Extensible language
11. Often treated as second best language
#include <stdio.h>
• To include standard input output library.
• Contains functions for input and output of data such as reading values from the keyboard and printing the results from
the screen.
• printf(), scanf()

int main()
• The last statement of the program will return an integer value to the operating system.

return 0
• Used to return the value zero to the operating system to keep an indication that there were no errors during the
execution of the program.
Files used in C
COMPILING AND EXECUTING C PROGRAMS
• The programming process starts with creating a source file that consists of statements of the program written in C
language
• the source file is processed by a special program called a compiler (Every programming language has its own compiler)
• the compiler translates the source code into an object code.
• The object code contains the machine instructions for CPU and calls to the operating system API.
• The object file is processed with another special program called a linker.
• The output of the linker is an executable or runnable file.
Using comments:
C supports 2 types of comments:
// is used to comment a single statement
/* Is used to comment multiple statements. Ending with */

C tokens:
a token is defined as the smallest individual element that holds significance to the compiler’s
functioning.
Keywords:
• C has a set of reserved words often known as keywords that cannot be used as an identifier or variable name
• Keywords have a fixed meaning.
• It must be written in lower case.
Identifiers:
• Identifiers help us to identify data and other objects in the computer.
• The are basically the names given to program elements such as variables and functions.
• Identifiers may consist of sequence of letters numerals or underscores.

Rules for forming identifier names:


1. Cannot include any special character or punctuation mark
2. cannot be 2 successive underscores
3. keywords cannot be used as identifiers
4. name is significant (upper case , lower case)
5. must begin with a letter or an underscore
6. can be of any reasonable length not more than 31 characters
Study Assignment:
List out basic datatypes of C and their size in bytes.
Variables:
• A variable is also an identifier.
• A variable is defined as a meaningful name given to a data storage location in a computer memory.
• When using a variable, we are actually referring to address of the memory where the data is stored
• Type specifiers (int - %d, char - %c, float - %f, string - %s)

Types of variables: (numerical , Character)


int em_num=7;
float salary = 2156.78;
char grade = ‘A’;
char course[ ] = “C programming”;
char course[10]; // if you are reading input from user. scanf(“%s”, course);
double balance amount = 1000000000;
int count=0, flag=1;
Constants:
• Constants are identifiers whose value do not change.
• It is an explicit data value specified by the programmer.
• C allows the programmer to specify constant of integer type floating point type character type and string
type.

const float PI=3.14


#define PI 3.1415
#define MAX 100
• Constant names are usually written in capital letter to distinguish from other variables. This is just a
convention.
• No blank spaces are permitted between # symbol and define keyword.
• Blank space must be used between #define and the constant nu and between constant name and constant
value.
• It does not end with semi-colon.
INPUT/ OUTPUT
SENTENCES IN C
Streams:
• A stream is the source of data as well as the destination the data.
• Streams are associated with a physical device such as monitor, or a file
stored in the secondary memory
• Supports two formatting functions:
• printf, scanf.
printf (“control string”, variable list); // printf(“the number is %d”, a);
the prototype of the control string can be given as follows:
%[flags][width][.precision][length modifier]type specifier
• Width specifies the minimum number of positions in the output
EXAMPLE
scanf()
• Scan formatting and is used to read the formatted data from the keyboard.
• Takes text streams from the keyboard, extracts and formats the data and stores it in specifies program variable.

Concept of input buffer is shown in the next slide.


Rule1: scanner function works until:
1. the maximum number of characters has been processed
2. a white space character is encountered or
3. another input is detected
Rule 2: every variable that has to be processed must have a conversion specification associated with.
scanf(“%d %d”, &num1, &num2, &num3);
Rule 3: & operator before each variable names specifies the address of the variable name.
Rule 4: data must match the character specified in the control string.
Rule 5: input data values must be separated by spaces or new line
When the scanf function completes reading all the data values, it returns the number of values that are successfully read.
scanf(“%d %f %c”, &a, &b, &c);
Will return 3.

The erroneous data will return -1.


Please refer Textbook and execute the programs given in exercise

You might also like