0% found this document useful (0 votes)
20 views36 pages

1 Programming Fundamentals

The document discusses fundamentals of computer programming including what programming is, programming languages, the programming process, common errors, C history, C structure, variables, data types, and more. It provides information about programming concepts and the C programming language.

Uploaded by

Code Juice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views36 pages

1 Programming Fundamentals

The document discusses fundamentals of computer programming including what programming is, programming languages, the programming process, common errors, C history, C structure, variables, data types, and more. It provides information about programming concepts and the C programming language.

Uploaded by

Code Juice
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Programming

Fundamentals

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
C Programming Basic

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
What is Computer Programming
• Computer programming is creating a sequence of instructions
to enable the computer to do something.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
What is Programming Language
A programming language is an artificial language that can be used to
control the behavior of a machine, particularly a computer.
For this subject, the programming language used is C.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Stages in the Applications Programming
Process
1. Problem statement. The programming process begins with a clear, written statement of the problem
to be solved by the computer.
2. Algorithm development: Once the problem has been clearly stated and all the requirements have
been understood, the next step is to develop the program logic necessary for accomplishing the task.
An algorithm is defined as a logical sequence of steps that must be performed in order to accomplish a
given task.
Sample Tool: Flowchart
3. Program coding: When the programmer is satisfied with the efficacy of the logic developed in the
preceding step, it is time to convert that logic (in either flowchart or pseudocode form) to the
specific syntax of the programming language that will be used.
4. Program testing: The coded program is next checked for errors.
5. Program documentation: The programming process is complete when the program has been fully
documented.
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming
Common Programming Errors
1. Syntax error - occurs when your code violates one or more grammar
rules of C and is detected by the compiler as it attempts to translate
your program.
• Note: If a statement has a syntax error, it cannot be translated and
your program will not be executed.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Common Programming Errors
2. Run-time Errors
• are detected errors and displayed by the compiler during the execution of the
program.
• occurs when the program directs the computer to perform illegal operation,
such as dividing a number by zero.
• an attempt to perform an invalid operation, detected during program
execution.
Note: When a run-time error occurs, the computer will stop executing
your program and will display a diagnostic message that indicates the
line where the error was detected.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Common Programming Errors
3. Logic Errors
• occur when a program follows a faulty algorithm.
• do not cause a run-time error and do not display error messages, so
are very difficult to detect.

Note: The only sign of a logic error may be incorrect program output.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
C History
• Dennis Ritchie – developed C in 1972 at AT&T Bell Laboratories.
• C was first developed for system programming.
• Initially, C language was developed to be used in UNIX operating
system.
• It was developed to overcome the problems of previous languages
such as B, BCPL, etc.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
C General Structure

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
C General Structure
Preprocessor Directives

int main(void){
local declarations
statements
}

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Sample C Program
#include <stdio.h>

int main(void) {
printf("Hello World!\n");
return 0;
}

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
C Directive
• DIRECTIVES – how C preprocessor commands are called, and begin
with a pound / hash symbol (#). No white space should appear before
the #, and a semi colon is NOT required at the end.
• The C Preprocessor - a program that is executed before the source
code is compiled.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Two Common Directives
1. include

2. define

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
#include Directive
• Gives program access to a library.
• Causes the preprocessor to insert definitions from a standard header
file into the program before compilation.
• Tells the preprocessor that some names used in the program are found
in the standard header file.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
main()
Every C program has a main function. This is where program execution
begins.
Body- the remaining line of the program in the body.
Braces {} – enclose the body of the function.
- indicates the beginning and end of the function main.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Two Parts of main
1. Declarations – the part of the program that tells the compiler the
names of memory cells in a program needed in the function,
commonly data requirements identified during problem analysis.
2. Executable statements – derived statements from the algorithm into
machine language and later executed.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Variable Declaration

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Variable Declaration
• A statement that communicates to the C compiler the names of all variables
used in the program and the kind of information stored in each variable. It
also tells how that information will be represented in memory.

Syntax:
data type variable_list;

Examples:
int x,age;
float sum,a,b;
char middle_intial;
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming
Data Type
• Defines the size and set of values and a set of operations that can be
performed on those values.

Standard Predefined Data Type in C:


 char
 double
 int

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Data Type
Seven Basic C Data Types:
1. Text (data type char) – made up of single characters (example x,#,9,E) and
strings (“Hello”), usually 8 bits, or 1 byte with the range of 0 to 255.
2. Integer values – those numbers you learned to count with.
3. Floating-point values – numbers that have fractional portions such as
12.345, and exponents 1.2e+22.
4. Double-floating point values – have extended range of 1.7e-308 to
1.7e+308.
5. Enumerated data types – allow for user-defined data types.
6. void – signifies values that occupy 0 bit and have no value. You can also use
this type to create generic pointers.
7. Pointer – does not hold information as do the other data types. Instead, each
pointer contains the address of the memory location.
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming
int
int - data type
int is used to define integer numbers.

Example:
int Count;
Count = 5;

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
float
float - data type
float is used to define floating point numbers.

Example:
float Miles;
Miles = 5.6;

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
double
double - data type
double is used to define BIG floating point numbers. It reserves twice
the storage for the number. On PCs this is likely to be 8 bytes.

Example:
double Atoms;
Atoms = 2500000.13;

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
char
char - data type
char defines characters.

Example:
char Letter;
Letter = 'x';

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Data Type Modifiers
• The three standard data types have the following modifiers:
 short
 long
 signed
 unsigned

• The modifiers define the amount of storage allocated to the variable.


ANSI has the following rules:
• short int <= int <= long int
• float <= double <= long double

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Data Type Size According to 32-bit Architecture
Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
long double 10 byte

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Variables
• Are like containers in your computer's memory - you can store values
in them and retrieve or modify them when necessary.
• Associated with a memory cell whose value can change as the
program executes.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Identifier
• An identifier is name that identifies or labels the identity of an object.
• In C, the following are the naming conventions for an identifier.
1. Names are made up of letters and digits.
2. The first character must be a letter.
3. C is case-sensitive, example ‘s’ is not the same with ‘S’.
4. The underscore symbol (_) is considered as a letter in C. It is not
recommended to be used, however, as the first character in a name.
5. At least the first 3 characters of a name are significant.
6. It should not be a reserved word/keyword in C.

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Examples
CANNOT start with a number 2i
CAN contain a number elsewhere h2o
CANNOT contain any arithmetic operators... r*s+t
CANNOT contain any other punctuation marks... #@x%£!!a
CAN contain or begin with an underscore _height_
CANNOT be a C keyword struct
CANNOT contain a space im stupid
CAN be of mixed cases XSquared

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Fundamental Operators and
Commands

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Assignment Operator (=)
• The most basic operator where the value on the right of the equal sign
is assigned to the variable on the left.
• Syntax:
lvalue = rvalue;
• Examples:
c = c + 1;
diameter = 2 * radius;
stat = getch();

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Binary/Arithmetic Operators
• Takes two operands and return a result.

Operator Use Result


+ op1 + op2 adds op1 to op2
- op1 - op2 subtracts op2 from op1
* op1 * op2 multiplies op1 by op2
/ op1 / op2 divides op1 by op2
% op1 % op2 computes the remainder
from dividing op1 by op2
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming
Basic Input Function for Numbers
scanf() – one of the C object stream that is used to accept data from the
standard input, usually the keyboard.
Syntax:
scanf(“format”, &var_name);
Example:
printf(“Input side of the square:”);
scanf(“%d”, &s);

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming
Conversion Specifications
Date Types printf scanf
long double %Lf %Lf
double %f %lf
float %f %f
unsigned long int %lu %lu
long int %ld %ld
unsigned int %u %u
int %d %d
short %hd %hd
char %c %c
String %s %s
Prepared by: Dr. Cheryl B. Pantaleon
Fundamentals of Programming
Basic Output Function
printf – writes formatted output to the standard output device such as
the monitor.

Syntax:
printf(“string expression”);
printf(“format code”,var_name);
Examples:
printf(“Hello world”);
printf(“%d”,x);

Prepared by: Dr. Cheryl B. Pantaleon


Fundamentals of Programming

You might also like