Chapter_04
Programming in C
What is Program ?
A setof instructions in sequential order is called program.
Programming Language:
The function of a computer is controlled by a set of instructions called programming language .
What is Computer Programming:
Computer programming is the process of developing computer programs.
Computer programs are a medium to instruct the computer.
They tell the computer what to do.
Define a Computer Programmer:
A Computer programmer is a person who develops computer programs.
Commonly,
a computer programmer has a bachelor or master level degree in Computer Science.
What is a programming Language
A programming language is a set of words, symbols and rules to write computer programs.
There are many programming languages like COBOL, Pascal, Java, Visual Basic, C language and
C++ etc.
Characteristics of a Good Programming Language :
Till now there are many high level languages which are very popular, and there are others, which
could not become so popular in-spite of being very powerful. There might be many reasons for the
success of a language, but one obvious reason is the characteristics of the language. Several
characteristics believed to be important with respect to making a programming language good are
briefly discussed below.
Simplicity
A good programming language must be simple and easy to learn and use. For example, BASIC is liked
by many programmers only because of its simplicity. Thus, a good programming language should
provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It
is also easy to develop and implement a compiler or an interpreter for a programming language that is
simple. However, the power needed for the language should not be sacrificed for simplicity.
Naturalness
A good language should be natural for the application area it has been designed. That is, it should
provide appropriate operators, data structures, control structures, and a natural syntax in order to
facilitate the users to code their problem easily and efficiently. FORTRAN and COBOL are good
examples of scientific and business languages respectively that posses high degree of naturalness.
Abstraction
Abstraction means the ability to define and then use complicated structures or operations in ways that
allow many of the details to be ignored. The degree of abstraction allowed by a programming
language directly affects its writability. For example, object-oriented languages support high degree of
abstraction. Hence writing programs in object-oriented languages is much easier. Object-oriented
languages also support reusability of program segments due to its feature.
Efficiency
The program written in good programming language are efficiently translated into machine code, are
efficiently executed, and acquires as little space in the memory as possible. That is, a good
programming language is supported with a good language translator (a compiler or an interpreter)
that gives due consideration to space and time efficiency.
Structuredness
Structuredness means that the language should have necessary features to allow its users to write
their programs based on the concepts of structured programming. This property of a language greatly
affects the ease with which a program may be written, tested, and maintained. Moreover, it forces a
programmer to look at a problem in a logical way so that fewer errors are created while writing a
program for the problem.
Compactness
In a good programming language, programmers should be able to express intended operation
concisely. A verbose language can tax the programmer’s sheer writing stamina and thus reduce its
usefulness. COBOL is generally not liked by many programmers because it is verbose in nature and
compactness.
Locality
A good programming language should be such that while writing a program, a programmer need not
jump around visually as the text of the program is prepared. This allows the programmer to
concentrate almost solely on the part of the program around the statements currently being worked
with. COBOL lacks locality because data definitions are separated from processing statements,
perhaps by many pages of code.
Extensibility
A good programming language should allow extension through simple, natural, and elegant
mechanisms. Almost all languages provide subprogram definition mechanisms for this purpose, but
there are some languages that are rather weak in this aspect.
Suitability to its Environment
Depending upon the type of application for which a programming language has been designed, the
language must also be made suitable to its environment. For example, a language designed for real
time applications must be interactive in nature. On the other hand, languages used for data processing
jobs like pay-roll, stores accounting, etc., may be designed to be operative in batch mode.
Types of Programming Languages
There are mainly 5 types of programming languages:
● Machine Language
● Assembly Language
● Low-level Language
● High-level Language
● Fourth-generation Language (4GL)
Machine Language
What is Machine Language?
Machine Languages are machine-specific instructions, executed by CPU.
A Machine language program consists of a sequence of zeros and ones.
Each kind of CPU has its own machine language.
Advantages
● Fast and efficient
● Machine oriented
● No translation required
Disadvantages
● Not portable
● Not programmer friendly
Assembly Language
What is Assembly Language?
Assembly language use English-like abbreviations or Mnemonics to represent machine
[Link] statement in assembly language corresponds to one statement in machine
language.
Advantages and Disadvantages
Assembly language programs have the same advantages and disadvantages as machine
language programs.
Translated via assemblers.
Example
8086 Assembly Language,
Low-level Language
What is Low-level Language?
Low-level Language is the language whose instructions are directly related to the
instruction set architecture of a specific CPU and it is possible to execute those
instructions with little or no translation.
Machine Language and Assembly Language are called Low-level Languages.
Example
8086 Machine Language program for 8086 Assembly Language progra
var1 = var1 + var2 ; var1 = var1 + var2
1010 0001 0000 0000 0000 0000 MOV AX , var1
0000 0011 0000 0110 0000 0000 0000 0010
ADD AX , var2
1010 0011 0000 0000 0000 0000
MOV var1 , AX
High-level Language
What is High-level Language?
A HLL language consists of English-like statements that are governed by a strict syntax.
A high-level language (HLL) has 2 primary components:
● A set of built-in language primitives and grammatical rules
● A translator
Advantages
● Portable or machine independent
● Readable and Programmer-friendly
Disadvantages
● Not as quick as low-level languages
● Need to be translated via Compilers or Interpreters
Examples
C, C++, C#, Java, FORTRAN, Visual Basic, Delphi and so on.
Fourth-generation Language (4GL)
What is Fourth Generation Language?
Fourth Generation Languages are those languages where users and programmers
specify the results they want, where the computer determines the sequence of
instructions that will accomplish those results.
Languages for accessing databases are often described as 4GLs.
Advantages
● Uses nonprocedural programming
● Simplified the programming process
● Do not impose rigid grammatical rules
Disadvantages
● Less flexible that other languages
● Programs written in 4GLs are generally far less efficient than that of the programs
written in high-level languages. Therefore, their use is limited to projects that do not
need such efficiency.
Example
SQL
Language Translator (1)
What is Language Translator?
A Language Translator is a program that translates a high-level language program or an
assembly language program into a machine language program.
Types of Language Translators
There are 3 types of Language Translators:
● Assembler
● Compiler
● Interpreter
Assembler
An assembler is a program that translates an assembly language program, written in a
particular assembly language, into a particular machine language.
Compiler
A compiler is a program that translates a high-level language program, written in a
particular high-level language, into a particular machine language.
Interpreter
An interpreter is a program that translates a high-level language program, one
instruction at a time, into machine language.
As each instruction is translated it is immediately executed.
NOTE
● Interpreted programs are generally slower than compiled programs because
compiled programs can be optimized to get faster execution.
● Some high-level languages are compiled while others are interpreted.
● There are languages which are first complied and then interpreted. Example, Java.
What is a difference between a high level programming language and a low level
programming language?
Low level programming language High level programming language
1. the statements in a low level language can 1. But a single statement in a high level
be directly mapped to processor instructions language may execute dozens of instructions.
2. Low level language is in binary form. 2. A HLL language consists of English
[Link] is fast and efficient 3. Not as quick as low-level languages
4. Low level language is machine dependent 4. High level language is machine independent
5. It is not portable 5. It is portable
6. No translation required 6. Translation is required via compiler or
interpreter.
7. Example: 8086 Assembler language. 7. Example: C, C++, C#, Java, FORTRAN,
RPG,COBOL,Visual Basic, Delphi and so on.
Generation of programming languages:
Answer:
1GL or first-generation language was (and still is) machine language or the level of instructions and data
that the processor is actually given to work on (which in conventional computers is a string of 0s and 1s).
2GL or second-generation language is assembler (sometimes called "assembly") language. A typical 2GL
instruction looks like this:
ADD 12,8
An assembler converts the assembler language statements into machine language.
3GL or third-generation language is a "high-level" programming language, such as PL/I, C, or Java. Java
language statements look like this:
Main()
{
Printf(“Welcome to IT world”);
}
A compiler converts the statements of a specific high-level programming language into machine
language.
4GL or fourth-generation language is designed to be closer to natural language than a 3GL language.
Languages for accessing databases are often described as 4GLs. A 4GL language statement might look
like this:
EXTRACT ALL CUSTOMERS WHERE "PREVIOUS PURCHASES" TOTAL MORE THAN $1000
5GL or fifth-generation language is programming that uses a visual or graphical development interface
to create source language that is usually compiled with a 3GL or 4GL language compiler. Microsoft,
Borland, IBM, and other companies make 5GL visual programming products for developing applications
in Java, for example. Visual programming allows you to easily envision object-oriented
programmingclass hierarchies and drag icons to assemble program components.
function
In programming, a named section of a program that performs a specific task. In this sense, a
function is a type of procedure or routine. Some programming languages make a distinction
between a function, which returns a value, and a procedure, which performs some operation but
does not return a value.
Constant :
A constant is an entity that doesn't change during the execution of a program.
Followings are the different types of constants :
1. Real Constant :
● It must have at least one digit.
● It must have a decimal point which may be positive or negative.
● Use of blank space and comma is not allowed between real constants.
● Example:
+194.143, -416.41
2. Integer Constant :
● It must have at least one digit.
● It should not contain a decimal place.
● It can be positive or negative.
● Use of blank space and comma is not allowed between real constants.
● Example:
1990, 194, -394
3. Character Constant :
● It is a single alphabet or a digit or a special symbol enclosed in a single quote.
● Maximum length of a character constant is 1.
● Example:
'T', '9', '$'
4. String Constant :
● It is collection of characters enclosed in double quotes.
● It may contain letters, digits, special characters and blank space.
● Example:
"Technowell Web Solutions, Dhaka"
Expression:
In programming, an expression is any legal combination of symbols that represents a value. Each
programming language and application has its own rules for what is legal and illegal. For
example, in the C languagex+5 is an expression.
Every expression consists of at least one operand and can have one or more operators. Operands
are values, whereas operators are symbols that represent particular actions. In the expression
x+5
x and 5 are operands, and + is an operator.
Variable:
In computer programming, a variable is a storage location and an associated symbolic name which
contains some known or unknown quantity or information, a value. The variable name is the usual way
to reference the stored value;
a = 5;
b = 2;
a = a + 1;
result = a -
b;
Local Variable:
alocal variable is a variable that is given local scope. Such a variable is accessible only from the function
or block in which it is declared.
In programming languages with only two levels of visibility, local variables are contrasted with global
variables. On the other hand, many ALGOL-derived languages allow any number of levels of nested
functions with private variables, functions, constants and types hidden within them.
Global variable:
aglobal variable is a variable that is accessible in every scope (unless shadowed). Interaction
mechanisms with global variables are called global environment mechanisms.
The global environment paradigm is contrasted with the local environment paradigm, where all
variables are local with no shared memory (and therefore all interactions can be reconducted to
message passing).
What is the difference between local and global variable?
Local variable
⚫ Local Variable is a variable which is declared inside the main().
⚫ For example
main()
{
int a=5;
........
.......
}
here a=5 is a local variable.
Global variable:
⚫ A local variable can only be accessed inside of main().
⚫ Global Variable is a variable which is declared before main ().
⚫ For example
int a=10;
main()
{}
here a is a global variable. Where ever u r using this variable ... we'll get the value as 10.
A global variable can be accessed by all functions
Fundamental data types:
The memory in our computers is organized in bytes. A byte is the minimum amount of memory .
Summary of the basic fundamental data types are given below:
Nam Size
Description Range*
e *
1byt signed: -128 to 127
char Character or small integer.
e unsigned: 0 to 255
2byt
int Short Integer. signed: -32768 to 32767
es
unsigned: 0 to 65535
floa 4byt
t Floating point number. +/- 3.4e +/- 38 (~7 digits)
es
doub Double precision floating point 8byt +/- 1.7e +/- 308 (~15
le number. es digits)
* The values of the columns Size and Range depend on the system the program is compiled for.
• Scope of variable:
– Portion of program where identifier can be used
– File scope
– Defined outside a function, known in all functions
– Global variables, function definitions and prototypes
– Function scope
– Can only be referenced inside defining function
• Block scope
– Begins at declaration, ends at right brace }
• Can only be referenced in this range
– Local variables, function parameters
– static variables still have block scope
• Storage class separate from scope
• Function-prototype scope
– Parameter list of prototype
– Names in prototype optional
• Compiler ignores parameter names
/* Write a C program to convert Selcius temperature to Ferhenheight*/
#include<stdio.h>
#include<conio.h>
void main()
{
int c, f;
printf("Enter celcious temperature :");
scanf("%d",&c);
f=9*c/5+32;
printf("Ferhenheight temperature=%d\n",f);
getch();
}
Write a C program to calculate the area of a circle
#include<stdio.h>
#include<conio.h>
#define PI 3.14
main ( )
{
Int ra;
float area;
printf ("Enter integer value for radius:");
scanf ("%d", &ra) ;
area = pi*(ra*ra);
printf("\n Area of circle =%f", area);
getch();
}
Write a C program to calculate the sum of two numbers
#include<stdio.h>
#include<conio.h>
main()
{
int a, b, sum;
a = 25;
b = 5;
sum = a+b;
clrscr() ;
printf("1st Number = %d\n", a);
printf("2nd number = %d\n", b);
printf("summation = %d\n", sum);
getch();
}
Write a C program to calculate the sum of series from 1 to 10.
#include<stdio.h>
#include<conio.h>
main()
{
int a, s;
s=0;
for(a=1; a<=10; a++)
{
s=s+a;
}
printf("Sum=%d", s);
getch();
}