Вы находитесь на странице: 1из 40

Programming

Fundamentals

Slide 1

Course Outline
Definition of Programming
History of Programming
Languages
Compiler & Interpreter
Definition of C language
Basic structure of C language
Header files
Void Functions
Data Types
Operators
Loop Structures

For Loop
While Loop
Do While Loop
Nested Loops
If Else Conditions
Nested If Conditions
Functions or Procedures
Switch-Case Statement
Local & Global Variables
Arrays

Course Outline
Multi-Dimensional Arrays
Strings
Functions of Strings

Pointers
Bitwise Operators
Structures
Introduction to OOPS

Definition of Programming
A precise sequence of steps to solve a
particular problem
OR
A vocabulary and set of grammatical rules
for instructing a computer to perform
specific tasks.

Introduction
-These instruction can be high level languages or low level
languages.

-But Now these terms programming languages usually


refers to high-level languages, such as BASIC, C, C++,
COBOL, FORTRAN, and Pascal. Each language has a unique
set of keywords (words that it understands) and a special
syntax for organizing program instructions.
- High-level programming languages, while simple
compared to human languages, are more complex than the
languages the computer actually understands,
called machine languages.

Introduction
Ca powerful computer programming language
thats appropriate for technically oriented people
with little or no programming experience, and for
experienced programmers to use in building
substantial information systems.
Youll write instructions commanding computers to
perform those kinds of tasks.

Software (i.e., the instructions you write) controls


hardware (i.e., computers).

Hardware & Software


Computers can perform calculations and make logical
decisions phenomenally faster than human beings can.
Todays personal computers can perform billions of
calculations in one secondmore than a human can
perform in a lifetime.

Supercomputers are already performing thousands of


trillions (quadrillions) of instructions per second!
Computers process data under the control of sequences of
instructions called computer programs.
These programs guide the computer through ordered
actions specified by people called computer programmers.

Hardware & Software


The programs that run on a computer are referred to as
software.
Youll learn a key programming methodology thats
enhancing programmer productivity, thereby reducing
software-development costs.

A computer consists of various devices referred to as


hardware
(e.g., the keyboard, screen, mouse, hard disks, memory, DVD
drives and processing units).

Computing costs are dropping dramatically, owing to


rapid developments in hardware and software
technologies.

Moores Law
For many decades, hardware costs have fallen rapidly.
Every year or two, the capacities of computers have
approximately doubled inexpensively.
This trend often is called Moores Law, named for the
person who identified it in the 1960s, Gordon Moore, cofounder of Intel.
Moores Law and related observations apply especially to
the amount of memory that computers have for programs,
the amount of secondary storage (such as disk storage)
they have to hold programs and data over longer periods of
time, and their processor speedsthe speeds at which
computers execute their programs (i.e., do their work).
Similar growth has occurred in the communications field.

How Do We Write a Program?


-A computer is not intelligent. It cannot analyze a
problem and come up with a solution.

-A human (the programmer) must analyze the


problem, develop the instructions for solving the
problem, and then have the computer carry out
the instructions.
- To write a program for a computer to follow, we
must go through a two-phase process: problem
solving and implementation.

How Do We Write a Program?

How Do We Write a Program?


Problem-Solving Phase:
1. Analysis and Specification. Understand
(define) the problem and what the solution
must do.

A. Defining/Specifying the problem:


What the computer program do?
What tasks will it perform?
What kind of data will it use, and where will get its data
from?
What will be the output of the program?
How will the program interact with the computer user?

How Do We Write a Program?


B. Analyzing the problem
It involves identifying the problem
Inputs that is, the data you have to work with
Outputs, the desired results
Additional requirements or constraints on the solution.

2. General Solution (Algorithm). Specify the required


data types and the logical sequences of steps that
solve the problem.
3. Verify. Follow the steps exactly to see if the solution
really does solve the problem.

How Do We Write a Program?


Implementation Phase:
1. Concrete Solution (Program).
Translate the algorithm (the general solution) into a
programming language.
2. Test.
Have the computer follow the instructions. Then
manually check the results. If you find errors, analyze
the program and the algorithm to determine the source
of the errors, and then make corrections.

How Do We Write a Program?


Maintenance Phase:
1. Use: Use the program.
2. Maintain: Modify the program to meet changing
requirements or to correct any errors that show up
while using it.

History of Programming
History:
In Programming, here we have four Generations
1st Generation: (Machine Dependent)
Machine languages (first-generation languages) are
the most basic type of computer languages,
consisting of strings of numbers the computer's
hardware can use.

History of Programming
2nd Generation: (Low Level Languages)
Assembly languages are only somewhat easier to
work with than machine languages.
developers use cryptic English-like phrases to
represent strings of numbers.
Assembler is used to translate the code into
object code.

History of Programming
3rd Generation: (Middle Level Languages)
These languages are machine independent.
Examples of 3rd Generation are:
FORTRON, COBOL, PASCAL, C, BASIC, etc.

4th Generation: (High Level Languages)


This generation may use a text-based environment (like a
3rd Generation) or may allow the programmer to work in a
visual environment, using graphical tools.
Examples of 3rd Generation are:
VB, VISUAL AGE, JAVA, C++, C# etc

Machine Languages, Assembly


Languages, High level Languages
Programmers write instructions in various
programming languages, some directly
understandable by computers and others requiring
intermediate translation steps.
These may be divided into three general types:
Machine languages
Assembly languages
High-level languages

Machine Languages, Assembly


Languages, High level Languages
Machine Languages
Any computer can directly understand only its own
machine language (also called machine code),
defined by its hardware architecture.
Machine languages generally consist of numbers
(ultimately reduced to 1s and 0s). Such languages
are cumbersome for humans.

Machine Languages, Assembly


Languages, High level Languages
Assembly Languages
English-like abbreviations to represent elementary
operations. These abbreviations formed the basis
of assembly languages.
Translator programs called assemblers were
developed to convert early assembly-language
programs to machine language.

Machine Languages, Assembly


Languages, High level Languages
High-Level Languages
To speed up the programming process further,
high-level languages were developed in which
single statements could be written to accomplish
substantial tasks.
Translator programs called compilers convert highlevel language programs into machine language.
Allow you to write instructions that look more like
everyday English and contain commonly used
mathematical expressions.

Compiler & Interpreter


COMPILER
Compiler translate the whole program into machine
language.
It makes a file called Object File (Machine oriented File)
It shows the error list at the end of compilation.
INTERPRETER
Interpreter reads the instruction line by line.
It convert the code into machine after execute.
If any error occurs then interpreter stop and show the
errors.

Programming Languages

Programming Languages

Programming Languages

Programming Languages

Programming Languages

Programming Languages

Elements of a C Program
A C development environment includes
System libraries and headers: a set of standard libraries and their
header files. For example see /usr/include and glibc.
Application Source: application source and header files
Compiler: converts source to object code for a specific platform
Linker: resolves external references and produces the executable
module

User program structure


there must be one main function where execution begins when the
program is run. This function is called main
int main (void) { ... },
int main (int argc, char *argv[]) { ... }

Exercise# 01
#include <conio.h>
#include<stdio.h>
void main ( )
{
printf( Welcome to DIHE);
}
Comments
Text surrounded by /* and */ is ignored by computer
Used to describe program
#include <stdio.h>
Preprocessor directive
Tells computer to load contents of a certain file
<stdio.h> allows standard input/output operations

Example of C Program
/usr/include/stdio.h
/* comments */
#ifndef _STDIO_H
#define _STDIO_H
... definitions and protoypes
#endif

/usr/include/stdlib.h
/* prevents including file
* contents multiple
* times */
#ifndef _STDLIB_H
#define _STDLIB_H
... definitions and protoypes
#endif

#include directs the preprocessor


to include the contents of the file
at this point in the source file.
#define directs preprocessor to
define macros.
example.c
/* this is a C-style comment
* You generally want to palce
* all file includes at start of file
* */
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char **argv)
{
// this is a C++-style comment
// printf prototype in stdio.h
printf(Hello, Prog name = %s\n,
argv[0]);
exit(0);
}

C Standards Header Files


Standard Headers you should know about:
stdio.h file and console (also a file) IO: perror, printf,
open, close, read, write, scanf, etc.
stdlib.h - common utility functions: malloc, calloc, strtol,
atoi, etc
string.h - string and byte manipulation: strlen, strcpy,
strcat, memcpy, memset, etc.
ctype.h character types: isalnum, isprint, isupport,
tolower, etc.
errno.h defines errno used for reporting system errors
math.h math functions: ceil, exp, floor, sqrt, etc.
signal.h signal handling facility: raise, signal, etc
stdint.h standard integer: intN_t, uintN_t, etc
time.h time related facility: asctime, clock, time_t, etc.

The Preprocessors
The C preprocessor permits you to define simple
macros that are evaluated and expanded prior to
compilation.
Commands begin with a #. Abbreviated list:
#define : defines a macro
#undef : removes a macro definition
#include : insert text from file
#if : conditional based on value of expression

Variable & Data types


Variables are memory location in computer's
memory to store data. To indicate the memory
location, each variable should be given a unique
name called identifier.
Variable names are just the symbolic
representation of a memory location. Examples of
variable name: a, b, c, sum, count, etc.
Example:
Int a;
Int sum;
C language has three basic data types:

Data types

Variables
#include<conio.h>
main ( )
{
int x ;
int y ;
int z ;
x = 10 ;
y = 20 ;
z=x+y;
}

Scanf
Printf type

Input

Type signed int represented in base 10. Digits 0 through 9 and the sign (+ or -).

Type signed int. The base (radix) is dependent on the first two characters. If the
first character is a digit from 1 to 9, then it is base 10. If the first digit is a zero and
the second digit is a digit from 1 to 7, then it is base 8 (octal). If the first digit is a
zero and the second character is an x or X, then it is base 16 (hexadecimal).

Type unsigned int. The input must be in base 8 (octal). Digits 0 through 7 only.

Type unsigned int. The input must be in base 10 (decimal). Digits 0 through 9 only.

x, X

Type unsigned int. The input must be in base 16 (hexadecimal). Digits 0 through 9
or A through Z or a through z. The characters 0x or 0X may be optionally prefixed to
the value.

e, E, f, g,
G

Type float. Begins with an optional sign. Then one or more digits, followed by an
optional decimal-point and decimal value. Finally ended with an optional signed
exponent value designated with an e or E.

Type character array. Inputs a sequence of non-whitespace characters (space, tab,


carriage return, new line, vertical tab, or formfeed). The array must be large enough
to hold the sequence plus a null character appended to the end.

scanf(%d,&x)

printf(%d,x)

THANK YOU

Вам также может понравиться