0% found this document useful (0 votes)
21 views40 pages

Basics of C Unit 1

Basics of c programming

Uploaded by

lakshyagpta8
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)
21 views40 pages

Basics of C Unit 1

Basics of c programming

Uploaded by

lakshyagpta8
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/ 40

UNIT 1

•Problem Solving Tools


Problem Solving Tools:
Algorithms: Definition,
Flowcharts: Symbols,
A sequential flowchart, Conditional and iterative flowchart,
Control Flow Statements: Sequential,
Selection – If-else, Switch-Case; Iteration,
Program Design Methodologies: Top-down and bottom-up design
approaches, Modular approach,
Haistory, Importance of C,
Structure of C program,
Data Types, primitive type and user defined type: typedef and enum,
Variables and Constants,
String Constant, Numeric Constant,
Declaration of variables Modifiers,
Identifiers and keywords,
Symbolic constants; Statements & Expressions.
What is a computer?

•Computer is an electronic device which is


designed to accept the data, perform some
required mathematical and logical
calculations and output the result.
• Charles Babbage is the Father of Computer.
What language does computer
understand?
The only language that the computer can
process or execute is called machine language.
It consists of only 0s and 1s in binary, that a
computer can understand.
Software and program

•Software is a collection of programs.


•Program is a collection of instructions
given to the computer.
Program Design Methodologies:

Top-down
bottom-up design approaches,
Modular approach
Top-down
• Top down design method starts with top level
components to lowest level component.
• In Top-down Model, the focus is on breaking
the bigger problem into smaller one and then
repeat the process with each problem.
• Top-Down Model is followed by structural
programming(Programs are divided into small
programs or functions) languages like C, Fortran
etc.
Bottom-up design approaches

• It is reverse of top down approach.


• It starts from the lowest level to the highest level
component.
• It first designs the basic components and from these
basic components higher level components are
designed.
• In Bottom-Up Model, the focus is on identifying and
resolving smallest problems and then integrating
them together to solve the bigger problem.
• Bottom-Up Model is mainly used by object oriented
programming languages like Java, C++ etc.
Modular approach
• Modular programming is a software design technique that
emphasizes separating the functionality of a program into
independent, interchangeable modules.
• Each module is a separate part of a program.
• The process of subdividing a computer program into separate
sub programs.
Programming language

•Low Level
•High Level
Low Level: Machine language and
assembly language
• Machine language
Computers can understand only digital signal, which are
binary digits i.e. 0 and 1. Writing program in machine level
language is a difficult task. Also machine language programs
are not portable.
• Assembly level language
The difficulties faced by machine level language were reduced
to some extent by using the modified form of machine level
language.
Instructions used were MOV, ADD, SUB…Since the computer
can understand only machine level language, hence assembly
level language must be translated into machine level language.
Here Translator used is assembler.
High Level Language

• Was developed keeping in mind the feature of portability i.e.


language is machine independent.
• Attention is only paid to the logic of the program.
• Forton, Python , Cobol
Translators: Computers can understand only
machine level language, which is binary i.e. 0
and 1.As it is difficult to maintain language in
0 and 1. So there is a need for converting the
code of high level and low level to machine
language. For this purpose translators are used.
Translators are just computer programs, which
accept a program written in high or low level
language and produce equivalent machine
language as output. Three types of translators
are:
• Assembler
• Interpreter
• Compiler
Assembler

• Converting the code of low level language(assembly language) into


machine language.
Compiler and interpreter

• Convert the code of high level into machine language.


• High level program is known as source program
• Machine level program is known as object program.
S.No. Compiler Interpreter
Translates the program one statement
1. The compiler scans the whole program in one go.
at a time.

As it scans the code in one go, the errors (if any) are shown at the end Considering it scans code one line at a
2.
together. time, errors are shown line by line.

It requires source code for later


3 It does not require source code for later execution.
execution.

Execution of the program takes place only after the whole program is Execution of the program happens after
4
compiled. every line is checked or evaluated.

Python, Ruby, Perl, SNOBOL,


Eg. C, C++, C#, etc are programming languages that are compiler-based. MATLAB, etc are programming
languages that are interpreter-based.
History Of C

• C Language was developed in 1970’s at Bell labs by Dennis Ritchie.


• It was developed for programming in os such as Unix.
• The standards for C language was introduced known as ANSI C
• ANSI(American National standards Institute)
C Language

• C is a structured, mid level, general purpose programming language.


• Dennis Ritchie in the year 1972-73
• C is called structured programming language because a program in c
language can be divided into small logical functional modules or
structures with the help of function procedure.
• Structured programming
Decision making: if else, switch case
Repetitive: for Loop, while loop, do-while
Subroutines/Procedures: Functions
C programs are portable i.e. programs written in one type of computer
system can run on another type of computer system.
Structure of C program
/*Documentation Section:
Program Name: Program to find the area of circle
Author: Akshita Patwal
Date : 06/11/2022
*/
#include<stdio.h> //link section
#define PI 3.14 //definition section
float area; //global declaration section
void message(); //Function Declaration Section
void main()
{
float r; //declaration part
printf("Enter the radius of the circle\n"); //executable part
scanf("%f",&r);
area=PI*r*r;
printf("Area of the circle=%f \n",area);
message();
}

void message() // Sub Program Section


{
printf("This Sub Function \n");
printf("we can take more Sub Function \n");
}
Compilation phase of C program

Executable Code

Loader

Memory

Executed
• Preprocessor
• The source code is the code which is written in a text editor and the
source code file is given an extension ".c". This source code is first
passed to the preprocessor, and then the preprocessor expands this
code. After expanding the code, the expanded code is passed to the
compiler.
• Compiler
• The code which is expanded by the preprocessor is passed to the
compiler. The compiler converts this code into assembly code. Or we
can say that the C compiler converts the pre-processed code into
assembly code.
• Assembler
• The assembly code is converted into object code by using an
assembler. The name of the object file generated by the assembler is
the same as the source file. The extension of the object file in DOS is
'.obj,' and in UNIX, the extension is 'o'. If the name of the source file
is 'hello.c', then the name of the object file would be 'hello.obj'.
• SOURCE CODE
• Source code is generated by human or programmer.
• Source code is high level code.
• Source code is written in plain text by using some high level programming
language.
• Source code is human understandable.
• Source code is not directly understandable by machine.
• It is written in a high-level language like C, C++, Java, Python, etc., or assembly
language.
• It can be easily modified.
• It contains comments for better understanding by programmer.
• Source code is input to compiler or any other translator.
• Source code is not system specific.
• It can be changed over time.
• Language translators like compiler, assembler, interpreter are used to translate
source code to object code.
• The source lines of code gives the readability and understandability to the user.
Use of fewer lines of code gives better performance by giving same results in most
cases.
• OBJECT CODE
• Object code is generated by compiler or other translator.
• Object code is low level code.
• Object code is translated code of source code. It is in binary format.
• Object code is not human understandable.
• Object code is machine understandable and executable.
• It is written in machine language through compiler or assembler or other
translator.
• It can not be modified.
• Object code is output of compiler or any other translator.
• Object code is system specific.
• Source code needs to be compiled or translated by any other translator to get
modified object code.
• Object code is machine code so it does not require any translation.
Executable Code:
• The linker takes input of object code generated by compiler/assembler
and generates executable file.
• Executable code is a file or a program that indicates tasks according
to encoded instructions.
• The CPU can directly execute an executable file to defined tasks.
• In other words, it is machine code instructions for a physical CPU. As
a CPU can directly execute an object code, we can also consider the
object code as an executable code.
Algorithm

• For solving a program we implement algorithm.


• Algorithm: It is a sequence of steps that gives method of solving a
program/problem.
• The algorithm creates a logic of program.
• Algorithm is a step-by-step procedure, which defines a set of
instructions to be executed in a certain order to get the desired
output. Algorithms are generally created independent of underlying
languages, i.e. an algorithm can be implemented in more than one
programming language.
Characteristics of an Algorithm

• Unambiguous − Algorithm should be clear and unambiguous. Each of


its steps (or phases), and their inputs/outputs should be clear and
must lead to only one meaning.
• Input − An algorithm should have 0 or more well-defined inputs.
• Output − An algorithm should have 1 or more well-defined outputs,
and should match the desired output.
• Finiteness − Algorithms must terminate after a finite number of
steps.
• Feasibility − Should be feasible with the available resources.
• Independent − An algorithm should have step-by-step directions,
which should be independent of any programming code.
• Problem − Design an algorithm to add two numbers and display the
result.
• Step 1 − START
• Step 2 − declare three integers a, b & c
• Step 3 − define values of a & b
• Step 4 − add values of a & b
• Step 5 − store output of step 4 to c
• Step 6 − print c
• Step 7 − STOP
Flowcharts:Flowchart is a diagrammatic representation of
sequence of logical steps of a program.
Sequential flowchart
General Aspect of ‘C’
• C was originally developed in the 1970s, by Dennis Ritchie at
Bell Telephone Laboratories, Inc.
• C is a middle level , general –purpose structured programming
language. Instructions of C consists of terms that are very
closely same to algebraic expressions, consisting of certain
English keywords such as if, else, for ,do and while
• C contains certain additional features that allows it to be used
at a lower level , acting as bridge between machine language
and the high level languages.
• C is called a structured programming language because to
solve a large problem, C programming language divides the
problem into smaller modules called functions or procedures
each of which handles a particular responsibility.
Importance Of C
1.As a middle-level language, C combines the features of both high-level and low-
level languages. It can be used for low-level programming, such as scripting for
drivers and kernels and it also supports functions of high-level programming
languages, such as scripting for software applications etc.
2.C is a structured programming language which allows a complex program to be
broken into simpler programs called functions. It also allows free movement of
data across these functions.
3.Various features of C including direct access to machine level hardware APIs, the
presence of C compilers, deterministic resource use and dynamic memory
allocation make C language an optimum choice for scripting applications and
drivers of embedded systems.
4.C language is case-sensitive which means lowercase and uppercase letters are
treated differently.
5.C is a general-purpose programming language and can efficiently work on
enterprise applications, games, graphics, and applications requiring calculations,
etc.
6.C language has a rich library which provides a number of built-in functions. It
also offers dynamic memory allocation.
Formatted I/O Functions
• Formatted I/O functions are used to take various inputs from the user and
display multiple outputs to the user. These types of I/O functions can help
to display the output to the user in different formats using the format
specifiers. These I/O supports all data types like int, float, char, and many
more.
• Why they are called formatted I/O?
These functions are called formatted I/O functions because we can use
format specifiers in these functions and hence, we can format these
functions according to our needs.

The following formatted I/O functions will be discussed in this section-

• printf()

• scanf()

• sprintf()

• sscanf()
printf():
printf() function is used in a C program to display any value like float,
integer, character, string, etc on the console screen. It is a pre-defined
function that is already declared in the stdio.h(header file).
Syntax 1:
To display any variable value.
printf(“Format Specifier”, var1, var2, …., varn);

scanf():
scanf() function is used in the C program for reading or taking any value
from the keyboard by the user, these values can be of any data type like
integer, float, character, string, and many more. This function is declared in
stdio.h(header file), that’s why it is also a pre-defined function. In scanf()
function we use &(address-of operator) which is used to store the variable
value on the memory location of that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
DATA TYPES IN C:
• It specifies the type of data that the variable can store like integer, character,
floating, double, etc. The data type is a collection of data with values having
fixed values, meaning as well as its characteristics.

• Each data type requires different amounts of memory and has some specific
operations which can be performed over it.
Primary Integer, character, float, double , void

Derived Pointer, Array, functions

User Defined Enum, Structure, Union, Typedef


Different Data Type Size
The size of any given data type in a program depends a lot on the type of processor,
as well as the compiler. In simpler words, the size of the data type depends entirely
on the computer on which we run C language along with the version of the C
program compiler that we installed in the computer.

• The int data type can be 4 bytes/ 2 bytes.

• The integer data type in C is used to store the whole numbers without decimal
values. Int will be 2 bytes or 16 bits in the case of an environment that is 16-bit.
However, int will be 4 bytes or 32 bits in case of an environment that is 32-bit and
64 bit.
Range: -2,147,483,648 to 2,147,483,647

Size: 2 bytes or 4 bytes

Format Specifier: %d

• The char data type is 1 byte.


• Character data type allows its variable to store only a single character. The storage size of the character
is 1 byte. It stores a single character and requires a single byte of memory in almost all compilers.
• Range: (-128 to 127) or (0 to 255)
• Size: 1 byte
• Format Specifier: %c
• The double data type is 8 bytes.

• The size of the double data type is basically 64 bits or 8 bytes. It is capable of
storing values that are comparatively double the size of the bytes that the float
data type can store. This is the reason why it is known as the double. This data
type is capable of holding about 15-17 digits, both after and before the decimal of
the data type.
• Range: 1.7E-308 to 1.7E+308
• Size: 8 bytes
• Format Specifier: %lf

• The float data type is 4 bytes.

• The size of the float data type is basically 32 bits or 4 bytes. The float data type is
single-precision in nature, and we use it for holding the decimal values. It helps in
the storage of various large values, but the float is comparatively faster than
double. It is because double works with comparatively much larger data values.
Thus, it is slower comparatively.
• Range: 1.2E-38 to 3.4E+38
• Size: 4 bytes
• Format Specifier: %f

• The void data type is 0 bytes.

• Since the void data type has no meaning, it has no size at all.
Different data types also have different ranges up to which they can store numbers.
These ranges may vary from compiler to compiler.

Data Type Memory (bytes) Range Format Specifier


(32 Bit)
short int 2 -32,768 to 32,767 %hd

unsigned short int 2 0 to 65,535 %hu

unsigned int 4 0 to 4,294,967,295 %u

int 4 -2,147,483,648 to 2,147,483,647 %d

long int 4 -2,147,483,648 to 2,147,483,647 %ld

unsigned long int 4 0 to 4,294,967,295 %lu

long long int 8 -(2^63) to (2^63)-1 %lld

unsigned long long int 8 0 to %llu


18,446,744,073,709,551,615

signed char 1 -128 to 127 %c

unsigned char 1 0 to 255 %c

float 4 1.2E-38 to 3.4E+38 %f

double 8 1.7E-308 to 1.7E+308 %lf

long double 16 3.4E-4932 to 1.1E+4932 %Lf


• Modifiers in C

• These are keywords in C to modify the default properties of int and char data
types. There are 4 modifiers in C as follows.

1. short It limits user to store small integer values from -32768 to 32767. It can be
used only on int data type.

2. long It allows user to stores very large number (something like 9 Million
Trillion) from -9223372036854775808 to 9223372036854775807.

3. signed It is default modifier of int and char data type if no modifier is specified.
It says that user can store negative and positive values.

4. unsigned When user intends to store only positive values in the given data type
(int and char).

You might also like