Csa 1500 - Module - 1
Csa 1500 - Module - 1
Introduction to C
Background:
C has now become a widely used professional language for various reasons.
Easy to learn
Structured language
It produces efficient programs.
It can handle low-level activities.
It can be compiled on a variety of computers.
Coding:
The process of converting the representation of a solution into set of
instruction in a programming language is known as coding.
Debugging:
The process of correcting error in a program is known as debugging.
Complier:
It is a software, which check the entire program created by the uses for
a certain type of errors called syntax error. If the program is error free the
complete program is translated into its equivalent machine language.
Source program/code:
The program created by the users is known as source program.
1
CSA 1500 Problem Solving Using C Module 1
Object program:
The machine language generated by the compiler is known as object
program.
Syntax:
It refers to set of rules which should be followed while creating every
statement in a program.
Syntax error:
The error which accrues due to the wrong use of syntax is known as syntax
error.
Semantic:
It refers to the logic which is followed for representing a solution.
Semantic Error:
An error which is accursed due to wrong use of logic.
Logical Error:
If the program produces wrong result such error are known as logical
error.
Runtime Error:
Errors which are deleted during run time or execution of a program is
known as run time error.
Testing:
It is the process of checking whether program work correctly according to
the requirement of the user.
2
CSA 1500 Problem Solving Using C Module 1
COMPUTER BASICS
A Computer is an electronic device that stores, manipulates and retrieves
the data.
Similarly a computer system can have objects and process.
1) Primary Memory
2) Secondary Memory
3
CSA 1500 Problem Solving Using C Module 1
Primary memory: The following are the types of memories which are treated
as primary.
ROM: It represents Read Only Memory that stores data and instructions even
when the computer is turned off. The Contents in the ROM can‘t be modified
once if they are written . It is used to store the BIOS information.
RAM: It represents Random Access Memory that stores data and instructions
when the computer is turned on. The contents in the RAM can be modified any
no. of times by instructions. It is used to store the programs under execution.
Cache memory: It is used to store the data and instructions referred by
processor.
Secondary Memory:
The following are the different kinds of memories
-Magnetic Storage: The Magnetic Storage devices store information that can
be read, erased and rewritten any number of times.
Example: Floppy Disks, Hard Disks, Magnetic Tapes
-Optical Storage: The optical storage devices that use laser beams to read and
write stored data. Example: CD(Compact Disk),DVD(Digital Versatile Disk)
COMPUTER SOFTWARE
Software of a computer system can be referred as anything which we can feel and
see. Example: Windows, icons
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.
System Software
4
CSA 1500 Problem Solving Using C Module 1
The operating system provides services such as a user interface, file and
database access, and interfaces to communication systems such as Internet
protocols. The primary purpose of this software is to keep the system operating
in an efficient manner while allowing the users access to the system.
System support software provides system utilities and other operating
services. Examples of system utilities are sort programs and disk format
programs. Operating services consists of programs that provide performance
statistics for the operational staff and security monitors to protect the system and
data.
The last system software category, system development software, includes
the language translators that convert programs into machine language for
execution, debugging tools to ensure that the programs are error free and
computer –assisted software engineering (CASE) systems.
Application software
5
CSA 1500 Problem Solving Using C Module 1
system software provides the direct interaction with the hard ware. The opening
at the bottom of the figure is the path followed by the user who interacts directly
with the operating system when necessary.
COMPUTER LANGUAGES
To write a program (tells what to do) for a computer, we must use a computer
language. Over the years computer languages have evolved from machine
languages to natural languages. The following is the summary of computer
languages
1940‘s Machine Languages
Machine Language:
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. The instructions in machine language must be in streams
of 0‘s and 1‘s. This is also referred as binary digits. These are so named as the
machine can directly understood the programs.
Advantages:
1) High speed execution
2) The computer can understand instructions immediately
3) No translation is needed.
Disadvantages:
1) Machine dependent
2) Programming is very difficult
3) Difficult to understand
4) Difficult to write bug free programs
5) Difficult to isolate an error.
6
CSA 1500 Problem Solving Using C Module 1
In the 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. These early programming languages simply
mirrored the machine languages using symbols or mnemonics to represent the
various language instructions. These languages were known as symbolic
languages. Because a computer does not understand symbolic language it must
be translated into the machine language. A special program called an Assembler
translates symbolic code into the machine language. Hence, they are called as
Assembly language.
Advantages:
1) Easy to understand and use
2) Easy to modify and isolate error
3) High efficiency
4) More control on hardware
Disadvantages:
High-Level Languages:
7
CSA 1500 Problem Solving Using C Module 1
Advantages:
1) Easy to write and understand
2) Easy to isolate an error
3) Machine independent language
4) Easy to maintain
5) Better readability
6) Low Development cost
7) Easier to document
8) Portable
Disadvantages:
1) Needs translator
2) Requires high execution time
3) Poor control on hardware
4) Less efficient
Example:C language
8
CSA 1500 Problem Solving Using C Module 1
Language Translators
These are the programs which are used for converting the programs in one
language into machine language instructions, so that they can be executed by
the computer.
COMPILER INTERPRETER
The compiled programs run faster The Interpreted programs run slower
Most of the Languages use compiler A very few languages use interpreters.
The procedure for turning a program written in C into machine Language. The
process is presented in a straightforward, linear fashion but you should recognize
that these steps are repeated many times during development to correct errors
9
CSA 1500 Problem Solving Using C Module 1
Name of File
Sl. No. Phase Code Tools Extension
C Compilers
1 TextEditor Source Code Edit, .C
Notepad Etc..,
2 Compiler Object Code C Compiler .OBJ
Executable
3 Linker C Compiler .EXE
Code
Executable
4 Runner C Compiler .EXE
Code
The software used to write programs is known as a text editor. A text editor helps
us enter, change and store character data. Once we write the program in the text
editor, we save it using a filename stored with an extension of C. This file is
referred as source code file.
Compiling Programs
10
CSA 1500 Problem Solving Using C Module 1
The code in a source file stored on the disk must be translated into machine
language. This is the job of the compiler. The Compiler is a computer program
that translates the source code written in a high-level language into the
corresponding object code of the low-level language. This translation
process is called compilation. The entire high-level program is converted into
the executable machine code file. The Compiler which executes C programs is
called as C Compiler. Example Turbo C, Borland C, GC etc.,
Linking Programs
The Linker assembles all functions, the program ‘s functions and system‘s
functions into one executable program.
Executing Programs
Sometimes it is not sufficient just to cope with problems. We have to solve those
problems. Most people are involving to solve the problem. These problems are
occurred while performing small task or making small decision.
List out all the solution that you find. Don’t focus on the quality of the
solution
11
CSA 1500 Problem Solving Using C Module 1
After filtering all the solution, you have the best solution only. Then choose on of
the best solution and make a decision to make it as a perfect solution.
After getting the best solution, Implement that solution to solve a problem.
After implementing a best solution, Evaluate how much you solution solve the
problem. If your solution will not solve the problem then you can again start
with Step 2.
ALGORITHM
12
CSA 1500 Problem Solving Using C Module 1
Disadvantage of Algorithms:
5) It is time consuming
6) Difficult to show branching and looping statement
7) Large problems are difficult to implement
Flowchart:
Flowcharts use special shapes to represent different types of actions or steps in a
process. Lines and arrows show the sequence of the steps, and the relationships
among them. These are known as flowchart symbols.
Common Flowchart Symbols
Rectangle Shape – Represents a process
Oval or Pill Shape – Represents the start or end
Diamond Shape – Represents a decision
Parallelogram – Represents input/output
13
CSA 1500 Problem Solving Using C Module 1
Advantages of flowchart:
Disadvantage of flowchart
14
CSA 1500 Problem Solving Using C Module 1
Draw flowchart to find the largest among three different numbers entered by user.
15
CSA 1500 Problem Solving Using C Module 1
4. Global declaration section: There are some variables that are used in
more than one function. Such variables are called global variables and are
declared in the global declaration section that is outside of all the functions. This
section also declares all the user-defined functions.
16
CSA 1500 Problem Solving Using C Module 1
Step 1: The program that is to be compiled is first typed into a file on the
computer system. Name should be provided with the last two characters as “.c”
or file with extension .c. So, the file name prog1.c might be a valid filename for
a C program.A text editor is usually used to enter the C program into a file. The
program that is entered into the file is known as the source program.
Step 2: Alt+F9
The compiler examines each program statement contained in the source program
and checks the syntax and semantics of the language. If any mistakes are
discovered, they are reported to the user. The errors have to be corrected in the
source program, and the compilation process must be restarted. Typical errors
reported during this phase of compilation might be due to an expression that has
unbalanced parentheses (syntactic error), or due to the use of a variable that is
not “defined” (semantic error).
Step 3: When all the syntactic and semantic errors have been removed from the
program, the compiler translate/Converts itinto a binary format known as object
code
17
CSA 1500 Problem Solving Using C Module 1
Step 4: After the program has been translated into object code, it is ready to be
linked. This process is once again performed automatically. The purpose of the
linking phase is to get the program into a final form for execution on the computer.
Tokens
In a paragraph of text document, individual words and punctuation marks are
called tokens. Similarly, the smallest individual unit in a c program which are
meaningful to the compiler is known as a token.
Different tokens in C can be classified as below:
1. Keyword (e.g. int, for, if)
2. Identifier (e.g. main, a, sum)
3. Constant (e.g. 10, 3.14)
4. string-literal (e.g. “hello”, “hello”)
5. operator (e.g. +, /, *)
6. Special symbols (e.g. (), {}, ;)
Keywords
Keywords are pre-defined words in a C compiler.
Each keyword convey special meaning and perform a specific function in
a C program.
Since keywords are referred names for compiler, they can’t be used as
variable name.
The C language have the following keywords:
18
CSA 1500 Problem Solving Using C Module 1
Identifier
General terminology used for the names of variables, functions and arrays.
These are user defined names consisting of arbitrarily long sequence of
letters and digits.
Identifiers may either a letter or the underscore(_) as a first character.
eg. x is a name given to integer variable.
int x=10;
Constant
Data items that remains same i.e. their value during do not changes
during the program
execution.
Several types of C constants that are allowed in C are; integer constant,
real constant,
character constant, string constant and other.
19
CSA 1500 Problem Solving Using C Module 1
String Literal
Sequence of characters enclosed within double quotes. For example, “hello”
, “abc”.
Every sting constant is automatically terminated with a special character ‘’
called the null character which represents the end of the string.
For example, “hello” will represent “hello” in the memory.
Thus, the size of the string is the total number of characters plus one for
the null character.
Operators
C operators are symbols that trigger an action and perform operation
when applied to C variables and other objects.
The data items on which operators act upon are called operands.
Operators can be classified as follows:
Unary Operators: Those operators that require only single operand to act
upon are known as unary operators.
Binary Operators: Those operators that require two operands to act upon
are called binary operators.
Ternary Operators: These operators require three operands to act upon.
Special Symbols
It has some special meaning
Thus, cannot be used for some other purpose.
[] () {} , ; : * … = #
Braces{}: These opening and ending curly braces marks the start and end
of a block of
code containing more than one executable statement.
Parentheses(): These special symbols are used to indicate function calls
and function
parameters.
Brackets[]: Opening and closing brackets are used as array element
reference. These
indicate single and multidimensional subscripts.
20
CSA 1500 Problem Solving Using C Module 1
VARIABLES
Type Description
A variable definition tells the compiler where and how much storage to
create for the variable. A variable definition specifies a data type and
contains a list of one or more variables of that type as follows −
type variable_list;
Here, type must be a valid C data type including char, int, float,
double, bool, or any user-defined object; and variable_list may
consist of one or more identifier names separated by commas. Some
valid declarations are shown here −
21
CSA 1500 Problem Solving Using C Module 1
int i, j, k;
char c, ch;
float f, salary;
double d;
22
CSA 1500 Problem Solving Using C Module 1
Examples:
While dealing with input-output operations in C, we use the following two streams:
#include <stdio.h>
23
CSA 1500 Problem Solving Using C Module 1
The printf() function is the most used function in the C language. This function is
defined in the stdio.h header file and is used to show output on the console
(standard output).
It writes the C string pointed by the format pointer to the standard output
(stdout). On success, the total number of characters written is returned.
24
CSA 1500 Problem Solving Using C Module 1
Scanf Statement:
Example 1:
#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an integer: ");
scanf("%d", &testInteger);
printf("Number = %d",testInteger);
return 0;
}
Output:
Enter an integer: 4
Number = 4
Example 2:
// C program to Read the two integer values using formatted input
scanf()
#include <stdio.h>
// Driver Code
int main()
{
int a, b, c;
c = scanf("%d %d", &a, &b);
printf("Number of successful "
"inputs read : %d",
c);
return 0;
25
CSA 1500 Problem Solving Using C Module 1
Syntax:
char getchar();
From the above syntax, the return type of getchar function is char. That means,
it can read only character value. For example, ch=getchar(); will read a single
character from a console and store that character in the character variable ch.
Syntax:
int putchar(char);
from the above syntax, you can see that return type of putchar function is int.
That means it returns the ASCII value of the variable char that we are displaying
on the console.
#include <stdio.h>
#include <stdlib.h>
main( )
{
char ch;
printf("Enter a character: ");
ch=getchar();
26
CSA 1500 Problem Solving Using C Module 1
The gets() and puts() are declared in the header file stdio.h. Both the functions
are involved in the input/output operations of the strings.
C gets() function
The gets() function enables the user to enter some characters followed by the
enter key.
Syntax
char[] gets(char[]);
#include<stdio.h>
void main ()
char s[30];
gets(s);
C puts() function
27
CSA 1500 Problem Solving Using C Module 1
The puts() function is very much similar to printf() function. The puts() function
is used to print the string on the console which is previously read by using gets()
or scanf() function.
Syntax
int puts(char[])
Let's see an example to read a string using gets() and print it on the console using
puts().
#include<stdio.h>
#include <string.h>
int main(){
char name[50];
return 0;
28
CSA 1500 Problem Solving Using C Module 1
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“Enter value of a:”);
scanf(“%d”, &a);
printf(“ a = %d”, a);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
char ch ;
29
CSA 1500 Problem Solving Using C Module 1
clrscr();
printf(“Press any character:”);
ch = getchar();
printf(“\nYou pressed :”)
putchar(ch);
getch();
}
30