Basics of C Unit 1
Basics of C Unit 1
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
•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
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.
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.
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
• 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
• 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
Format Specifier: %d
• 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 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
• 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.
• 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).