Unit-1 Overview of C 2
Unit-1 Overview of C 2
OVERVIEW OF C
TARUNJIT YUMNAM
ASSISTANT PROFESSOR
COMPUTER ENGINEERING DEPARTMENT
UNITEDWORLD INSTITUTE OF TECHNOLOGY
UNIX incorporated many special features that were either new or uncommon to
operating systems of the time. UNIX revolutionized operating system design, and many of
it’s principles are used in modern operating systems, such as Windows, OS X, and Linux.
FLOW CHART SYMBOLS
ALGORITHMS
• Algorithm is a step – by – step procedure which is helpful in solving a problem. If, it is written in English like
sentences then, it is called as ‘PSEUDO CODE’.
• Properties of an Algorithm
• An algorithm must possess the following five properties −
• Input, Output, Finiteness, Definiteness, Effectiveness
• Example
• Algorithm for finding the average of three numbers is as follows −
• Start
• Read 3 numbers a,b,c
• Compute sum = a+b+c
• Compute average = sum/3
• Print average value
• Stop
FLOWCHARTS
• To write the first c program, open the C console and write the following code:
1. #include <stdio.h>
2. int main(){
3. printf("Hello C Language");
4. return 0;
5. }
• #include <stdio.h> includes the standard input output library functions.
• The printf() function is defined in stdio.h .
• int main() The main() function is the entry point of every program in c language.
• printf() The printf() function is used to print data on the console.
• return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for successful
execution and 1 for unsuccessful execution.
HOW TO COMPILE AND RUN THE C PROGRAM
• There are 2 ways to compile and run the c program, by menu and by shortcut.
• By menu
• Now click on the compile menu then compile sub menu to compile the c program.
• Then click on the run menu then run sub menu to run the c program.
• By shortcut
• Or, press ctrl+f9 keys compile and run the program directly.
• You will see the following output on user screen.
• You can view the user screen any time by pressing the alt+f5 keys.
• Now press Esc to return to the turbo c++ console.
TYPES OF PROGRAMMING LANGUAGES
1. Machine language:
• It is easier for the computer to understand but harder for the programmer to understand because machine language is
simply the language of machines—bits.
• Sometimes, programmers will develop programs directly with machine code, but because this is difficult to understand and
tedious to type, it is more common to program using assembly or a high-level language.
2. Assembly language :
• It is slightly easier to understand because the bits of machine language are replaced by numbers and English commands.
• Before assembly code is run by the computer, it is assembled by an assembler which will converts the code back into the 1s and
0s of machine language that the computer can understand.
3. High-Level languages:
• It uses more English commands and are significantly more readable than assembly or machine language.
• Many high-level languages have built-in commands that help the programmer write loops, create variables of different data
types, and manipulate strings.
• Some newer high-level languages are scripting languages. This means that they are not compiled, or translated into
machine language, until just before the code is executed at runtime For example, Python, Javascript, PHP etc, are all
scripting languages.
COMPILATION PROCESS IN C
• The compilation is a process of converting the source code into object code which is done with the help of the compiler.
• The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code.
• The c compilation process converts the source code taken as input into the object code or machine code. The compilation process can be divided
into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking. The c compilation process converts the source code taken as input
into the object code or machine code. The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and
Linking.
• The preprocessor takes the source code as an input, and it removes all the comments from the source code. The preprocessor takes the
preprocessor directive and interprets it. For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets the
directive and replace this directive with the content of the 'stdio.h' file.
A C program passes through below phases before being transformed into an executable form:
CONT..
• 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'.
• Linker
• The library functions written in C program are pre-compiled, and the object code of these library files is stored with '.lib' (or '.a') extension.
• The main working of the linker is to combine the object code of library files with the object code of our program.
• When a program refers to the functions defined in other files, then linker links the object code of these files to our program.
• Therefore, we conclude that the job of the linker is to link the object code of our program with the object code of the library files and other files.
• The output of the linker is the executable file where The name of the executable file is the same as the source file but differs only in their extensions.
• In DOS, the extension of the executable file is '.exe', and in UNIX, the executable file can be named as 'a.out'.
TESTING AND DEBUGGING A PROGRAM
• Program testing and debugging refer to the tasks of detecting and removing errors in a
program, so that the program produces the desired results.
• It is necessary to make efforts to detect, isolate and correct any errors that are likely to
be present in the program because in real time rarely a program run perfectly for the
first time.
• Program Testing : Testing is the process of reviewing and executing a program with the
intent of detecting errors.
• Program Debugging : Debugging is the process of finding errors and removing them from a
computer program, otherwise they will lead to failure of the program.
PROGRAM TESTING
• Debugging is the process of finding errors and removing them from a computer
program, otherwise they will lead to failure of the program.
• Even after taking full care during program design and coding, some errors may
remain in the program and these errors appear during compilation or linking or
execution.
• Debugging is generally done by program developer.
DEBUGGING VS TESTING
TESTING DEBUGGING
• Technically Testing is a process to check if the application • Debugging is the activity performed by developers to fix
is working same as it was supposed to do, and not working the bug found in the system.
as it was not supposed to do.
• Main objective of Debugging is to find the exact root cause
• Main objective of Testing is to find bugs and errors in an at code level to fix the errors and bugs found during the
application which get missed during the development testing.
phase by the developer.
• Debugging is to find the missing or de-faulty code in an
• As Testing is mainly to find out the errors and bugs is application hence major performed by the developers only.
mainly performed by the testers.
• Debugging is used to find the error at code level so
• As Testing covers the functional and behavioral flow of an technical and code level knowledge is required for the
application so only functional knowledge is required for developer to perform debugging.
the tester to perform the testing.
• Debugging can't be get automated it is always be the
• Testing can be manual or made automated with the help of manual.
different tools.
HISTORY OF C
• The C Language is developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices
such as drivers, kernels, etc.
• It was named ‘C’ because it is an offspring of BCPL (Basic Combined Programming Language) which was popularly called ‘B’
language.
• C programming is considered as the base for other programming languages, that is why it is known as mother language.
• It can be defined by the following ways:
1. Mother language
2. System programming language
3. Procedure-oriented programming language
4. Structured programming language
5. Mid-level programming language
CONT..
1. Mother language
• C language is considered as the mother language of all the modern programming
languages because most of the compilers, JVMs, Kernels, etc. are written in C language,
and most of the programming languages follow C syntax, for example, C++, Java, C#, etc.
• It provides the core concepts like the array, strings, functions, file handling, etc. that are
being used in many languages like C++, Java, C#, etc.
CONT..
3. C as a procedural language
• A procedure is known as a function, method, routine, subroutine, etc. A procedural
language specifies a series of steps for the program to solve the problem.
• A procedural language breaks the program into functions, data structures, etc.
• C is a procedural language. In C, variables and function prototypes must be declared
before being used.
CONT..
• C is called as a robust language, which has so many built-in functions and operations, which can be used to write
any complex program.
• Generally, we use to call C as a middle level language. Because, the ‘C’ compiler combines the capabilities of an
assembly language with the features of a high-level language. Therefore, it is best for writing both system software
and business packages.
• ‘C’ Programs are efficient and fast.
• C is highly portable, that is, ‘C’ programs written on one computer can be run on another with little (or) no
modification.
• ‘C’ language is best for structured programming, where the user can think of a problem in terms of function modules
(or) blocks.
• It has the ability to extend itself.
FUNCTION IN C PROGRAMMING
• Types of main function/ predefined • User-defined function in c programming
function:
• int main(){ • return_type function_name( parameter_list)
printf(“Hello World”); {
} //Return value
}
• void main(){
printf(“Hello World”);
}
BASIC STRUCTURE OF C PROGRAMS
• /* documentation section */
• preprocessor directives
• global declaration
• main ( ){
• local declaration
• executable statements
• }
• returntype function name (argument list){
• local declaration
• executable statements
• }
EXAMPLE
• #include<stdio.h>
• void main()
• { //Syntax for addition (function has int because we are returning values for function//
• int sum(); int add;
• add = sum();
• printf("Addition of two numbers is : %d",add); }
• int sum()
• { //Declaring actual parameters//
• int a,b,add;
• //Reading User I/p//
• printf("Enter a,b :");
• scanf("%d,%d",&a,&b);
• //Addition operation// OUTPUT
• add=a+b;
Enter a,b : 4,6
• //Returning value//
• return add; } Addition of two numbers is : 10
CONSTANTS
• A constant is a value or variable that can't be changed in the program, for example: 10,
20, 'a', 3.4, "c programming" etc.
INTEGER CONSTANTS
• An integer constant is a sequence of digits from 0 to 9 without decimal points or fractional part or
any other symbols. There are 3 types of integers namely decimal integer, octal integers and
hexadecimal integer.
• Decimal Integers consists of a set of digits 0 to 9 preceded by an optional + or –sign. Spaces,
commas and non digit characters are not permitted between digits. Example for valid decimal
integer constants are : int y = 123; //here 123 is a decimal integer constant
• Octal Integers constant consists of any combination of digits from 0 through 7 with a O at the
beginning. Some examples of octal integers are : int x = 0123; // here 0123 is a octal integer
constant .
• Hexadecimal integer constant is preceded by OX or Ox, they may contain alphabets from A to F
or a to f. The alphabets A to F refers to 10 to 15 in decimal digits. Example of valid hexadecimal
integers are : int x = 0x12 // here Ox12 is a Hexa-Decimal integer constant
REAL CONSTANT
• All character constants have an equivalent integer value which are called ASCII Values.
STRING CONSTANTS
• A string constant is a set of characters enclosed in double quotation marks. The characters in a
string constant sequence may be a alphabet, number, special character and blank space.
• Each string literal constant is implicitly terminated by a null character(\0)
• Number of bytes occupied is one more than the number of characters present in the string, hence
empty string(“”) occupies one byte
• Examples:
• “Computer Science and Engineering”
• “Knowledge Institute of Technology”
• "VISHAL" "1234" "God Bless""!.....?“
QUALIFIED & SYMBOLIC CONSTANT
QUALIFIED CONSTANT
• Created by using const qualifier
• Example: const char a = ‘A’;
• Qualifier places a lock on the memory box after placing the value, so it is not possible to modify
the value
SYMBOLIC CONSTANT
• Created with the help of the define preprocessor directive
• Example: #define PI 3.14124
Function
Definition of
#include<stdio.h>
Function
Definition of
#include<conio.h>
COMPILER LINKER
SHARING FUNCTIONS BETWEEN FILES IN C
BACKSLASH CHARACTER CONSTANTS
[ESCAPE SEQUENCES]
VARIABLES
• A variable in simple terms is a storage place which has some memory allocated to it.
Basically, a variable used to store some form of data.
• Different types of variables require different amounts of memory, and have some
specific set of operations which can be applied on them.
• A variable name can consist of alphabets (both upper and lower case), numbers and the
underscore ‘_’ character. However, the name must not start with a number.
• Variable Declaration:
• A typical variable declaration is of the form:
DIFFERENCE B/W VARIABLE DECLARATION
AND DEFINITION
• Variable declaration refers to the part where variable is first introduced before its first use.
• Variable definition is the part where the variable is assigned a memory location and a
value. Most of the time, variable declaration and definition are done together.
RULES FOR DEFINING VARIABLES
• A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
CONT..
• TOKEN is the smallest unit in a ‘C’ program. It is each and every word and
punctuation that you come across in your C program.
• The compiler breaks a program into the smallest possible units (Tokens) and
proceeds to the various stages of the compilation.
• C Token is divided into six different types, viz, Keywords, Operators, Strings,
Constants, Special Characters, and Identifiers
CONT..
KEYWORDS IN C
• Keywords are specific reserved words in C each of which has a specific feature associated
with it.
• Almost all of the words which help us use the functionality of the C language are included
in the list of keywords
IDENTIFIERS IN C
• C identifiers represents the name in the C program, for example, variables, functions,
arrays, structures, unions, labels, etc.
• An identifier can be composed of letters such as uppercase, lowercase letters, underscore,
digits, but the starting letter should be either an alphabet or an underscore.
• If the identifier is not used in the external linkage, then it is called as an internal identifier. If
the identifier is used in the external linkage, then it is called as an external identifier.
RULES FOR CONSTRUCTING C IDENTIFIERS