UNIT1
UNIT1
LECTURE NOTES ON
PROGRAMMING FOR PROBLEM SOLVING USING C
UNIT I
Introduction to Computers: Creating and running Programs, Computer Numbering System,
IMPORTANT QUESTIONS:
3.what is variable?what are the rules to create variable ?and explain declaration and assigning
value of variable?
The input device is usually a keyboard where programs and data are
entered into the computers. Examples of other input devices include a
mouse, a pen or stylus, a touch screen, or an audio input unit.
The central processing unit (CPU) is responsible for executing
instructions such as arithmetic calculations,comparisons among data,
and movement of data inside the system.
The output device is usually a monitor or a printer to show output. If the
output is shown on the monitor, we say we have a soft copy. If it is
printed on the printer, we say we have a hard copy.
Auxiliary storage, also known as secondary storage, is used for both
input and output. It is the place where the programs and data are stored
permanently. When we turn off the computer, or programs and data
remain in the secondary storage, ready for the next time we need them.
Computer Software :-
Computer software is divided in to two broad categories: system
software and application software.
System software manages the computer resources .It provides the
interface between the hardware and the users.
Application software, on the other hand is directly responsible for
helping users solve their problems.
Computing Environments:-
Computing Environment is a collection of computers / machines,
software, and networks that support the processing and exchange of
electronic information meant to support various types of computing
solutions. With the advent if technology the computing environments have
been improved.
Computer Languages:-
To write a program for a computer, we must use a computer
language. Over the years computer languages have evolved from machine
languages to natural languages.
Machine Languages:-
In the earliest days of computers, the only programming languages
available were machine languages. Each computer has its own machine
language, which is made of streams of 0’s and 1’s.
Instructions in machine language must be in streams of 0’s and 1’s because the
internal circuits of a computer are made of switches transistors and other
electronic devices that can be in one of two states: off or on. The off state is
represented by 0 , the on state is represented by 1.
The only language understood by computer hardware is machine language.
Symbolic Languages:-
In early 1950’s Admiral Grace Hopper, A mathematician and naval
officer developed the concept of a special computer program that would
convert programs into machine language.
Computer does not understand symbolic language it must be
translated to the machine language. A special program called assembler
translates symbolic code into machine language.
High Level Languages:-
Symbolic languages greatly improved programming effificiency; they
still required programmers to concentrate on the hardware that they
were using.
Working with symbolic languages was also very tedious because each
machine instruction has to be individually coded. The desire to improve
programmer efficiency and to change the focus from the computer to
the problem being solved led to the development of high-level language.
Overall Process:-
Decimal equivalent of any octal number is sum of product of each digit with its
positional value.
726 = 7×8 + 2×8 + 6×8
8
2 1 0
= 448 + 16 + 6
= 470 10
Storing Integers:-
Integers are commonly stored using a word of memory, which is 4 bytes
or 32 bits, so integers from 0 up to 4,294,967,295 (232 - 1) can be stored.
Below are the integers 1 to 5 stored as four-byte values (each row
represents one integer).
0 : 00000001 00000000 00000000 00000000 | 1
4 : 00000010 00000000 00000000 00000000 | 2
8 : 00000011 00000000 00000000 00000000 | 3
12 : 00000100 00000000 00000000 00000000 | 4
16 : 00000101 00000000 00000000 00000000 | 5
This may look a little strange; within each byte (each block of eight bits), the
bits are written from right to left like we are used to in normal decimal notation,
but the bytes themselves are written left to right! It turns out that the computer
does not mind which order the bytes are used (as long as we tell the computer
what the order is) and most software uses this left to right order for bytes.7.3
Two problems should immediately be apparent: this does not allow for
negative values, and very large integers, 232 or greater, cannot be stored in a
word of memory.
Real numbers:-
Real numbers (and rationals) are much harder to store digitally than
integers.
Recall that k bits can represent 2k different states. For integers, the first
state can represent 0, the second state can represent 1, the third state can
represent 2, and so on. We can only go as high as the integer 2k - 1, but at
least we know that we can account for all of the integers up to that point.
Unfortunately, we cannot do the same thing for reals. We could say that the
first state represents 0, but what does the second state represent? 0.1? 0.01?
0.00000001? Suppose we chose 0.01, so the first state represents 0, the
second state represents 0.01, the third state represents 0.02, and so on. We
can now only go as high as 0.01 x (2k - 1), and we have missed all of the
numbers between 0.01 and 0.02 (and all of the numbers between 0.02 and
0.03, and infinitely many others).
2. LINKING SECTION : This section tells the compiler to link the certain occurrences of keywords
or functions in your program to the header files specified in this section.
e.g. #include <stdio.h>
3. DEFINITION SECTION : It is used to declare some constants and assign them some value.
e.g. #define MAX 25
Here #define is a compiler directive which tells the compiler whenever MAX is found in the
program replace it with 25.
4. GLOBAL DECLARATION SECTION : Here the variables which are used through out the program
(including main and other functions) are declared so as to make them global(i.e accessible to all
parts of program)
e.g. int i; (before main())
5. MAIN FUNCTION SECTION : It tells the compiler where to start the execution from
main()
{
point from execution starts
}
main function has two sections
1. declaration section : In this the variables and their data types are declared.
2. Executable section : This has the part of program which actually performs the task we need.
6. SUB PROGRAM OR FUNCTION SECTION : This has all the sub programs or the
functions which our program needs.
/* simple program in c */
#include<stdio.h>
main()
{
printf(“welcome to c programming”);
} /* End of main */
IDENTIFIERS :-
Names of the variables and other program elements such as functions,
array, etc, are known as identifiers.
There are few rules that govern the way variable are named (identifiers).
1. Identifiers can be named from the combination of A-Z, a-z, 0-9,
_(Underscore).
2. The first alphabet of the identifier should be either an alphabet or an
underscore. digit are not allowed.
3. It should not be a keyword.
Eg: name, ptr, sum
After naming a variable we need to declare it to compiler of what data type it
is .
The format of declaring a variable is
where data type could be float, int, char or any of the data types.
id1, id2, id3 are the names of variable we use. In case of single variable no
commas are required.
Eg float a, b, c;
int e, f, grand total;
char present_or_absent;
DATA TYPES :-
To represent different types of data in C program we need different data
types. A data type is essential to identify the storage representation and the
type of operations that can be performed on that data. C supports four
different classes of data types namely
1. Basic Data types
2. Derives data types
3. User defined data types
4. Pointer data types
BASIC DATA TYPES:
All arithmetic operations such as Addition , subtraction etc are possible on
basic data types.
E.g.: int a,b;
Char c;
VARIABLES :-
A quantity that can vary during the execution of a program is known as a
variable. To identify a quantity we name the variable for example if we are
calculating a sum of two numbers we will name the variable that will hold
the value of sum of two numbers as 'sum'.
CONSTANTS : -
A quantity that does not vary during the execution of a program is
known as a constant supports two types of constants namely Numeric
constants and character constants.
NUMERIC CONSTANTS:
1. Example for an integer constant is 786,-127
2. Long constant is written with a terminal ‘l’or ‘L’,for example 1234567899L
is a Long constant.
CHARACTER CONSTANTS:-
A character constant is written as one character with in single quotes
such as ‘a’. The value of a character constant is the numerical value of the
character in the machines character set.
INPUT AND OUTPUT STATEMENTS :-
The simplest of input operator is getchar to read a single character from
the input device.
varname=getchar();
you need to declare varname.
The simplest of output operator is putchar to output a single character on
the output device.
putchar(varname)
The getchar() is used only for one input and is not formatted. Formatted
input refers to an input data that has been arranged in a particular format, for
that we have scanf.
#include<stdio.h>
main()
{
int a,b;
float c;
printf("Enter any number");
a=getchar();
printf("the char is ");
putchar(a);
printf("Exhibiting the use of scanf");
printf("Enter three numbers");
scanf("%d%d%f",&a,&b,&c);
printf("%d%d%f",a,b,c);
}
Scope:-
A scope in any programming is a region of the program where a
defined variable can have its existence and beyond that variable it
cannot be accessed. There are three places where variables can be
declared in C programming language −
Inside a function or a block which is called local variables.
Outside of all functions which is called global variables.
In the definition of function parameters which are
called formal parameters.
Local Variables:-
Variables that are declared inside a function or block are called local
variables. They can be used only by statements that are inside that
function or block of code. Local variables are not known to functions
outside their own.
Global Variables:-
Global variables are defined outside a function, usually on top of the
program. Global variables hold their values throughout the lifetime of
your program and they can be accessed inside any of the functions
defined for the program.
A global variable can be accessed by any function.
Storage Classes:-
A storage class defines the scope (visibility) and life-time of variables
and/or functions within a C Program. They precede the type that they
modify. We have four different storage classes in a C program −
auto
register
static
extern
The example above defines two variables with in the same storage
class. 'auto' can only be used within functions, i.e., local variables.
The register Storage Class:-
The register storage class is used to define local variables that
should be stored in a register instead of RAM. This means that the
variable has a maximum size equal to the register size (usually one
word) and can't have the unary '&' operator applied to it (as it does not
have a memory location).
{
register int miles;
}
The register should only be used for variables that require quick
access such as counters.
Type Qualifiers:-
The keywords which are used to modify the properties of a variable are called
type qualifiers.
Syntax:
const data_type variable_name; (or) const data_type *variable_name;
VOLATILE KEYWORD:
When a variable is defined as volatile, the program may not change the
value of the variable explicitly.
But, these variable values might keep on changing without any explicit
assignment by the program. These types of qualifiers are called volatile.
Syntax:
volatile data_type variable_name; (or) volatile data_type *variable_name;
When you start writing your code in C, C++ or any other programming
language, your first objective might be to write a program that works.
After you accomplished that, the following are few things you should
consider to enhance your program.
This article will give some high-level ideas on how to improve the speed
of your program.
You could optimize your code for performance using all possible
techniques, but this might generate a bigger file with bigger memory
footprint.
You might have two different optimization goals, that might
sometimes conflict with each other. For example, to optimize the code
for performance might conflict with optimize the code for less memory
footprint and size. You might have to find a balance.
Performance optimization is a never-ending process. Your code might
never be fully optimized. There is always more room for improvement
to make your code run faster.
Sometime we can use certain programming tricks to make a code run
faster at the expense of not following best practices such as coding
standards, etc. Try to avoid implementing cheap tricks to make your
code run faster.
Expressions:-
Evaluation of Expressions:-
Expressions are evaluated using an assignment statement of the form.
Variable = expression;
Variable is any valid C variable name. When the statement is
encountered, the expression is evaluated first and then replaces the
previous value of the variable on the left hand side.
. All variables used in the expression must be assigned values before evaluation is
attempted.
Example of evaluation statements are
X=a*b-c
Y=b/c*a
Z=a-b/c+d;
1. #include<stdio.h>
2. int main(){
3. int n,i,m=0,flag=0;
4. printf("Enter the number to check prime:");
5. scanf("%d",&n);
6. m=n/2;
7. for(i=2;i<=m;i++)
8. {
9. if(n%i==0)
10.{
11.printf("Number is not prime");
12.flag=1;
13.break;
14.}
15.}
16. if(flag==0)
17.printf("Number is prime");
18.return 0;
19. }
Output:
These values are called command line arguments and many times
they are important for your program especially when you want to
control your program from outside instead of hard coding those
values inside the code.