100% found this document useful (1 vote)
42 views

C Prog Handouts

This document outlines the course content for ICT 103 Fundamentals of Programming. The course covers C basics including program structure, variables, input/output, arithmetic, conditionals, functions, loops, arrays, strings, pointers, and data structures. It will teach the history of C programming, how to construct, compile, and execute C programs, and cover key C language elements like headers, functions, comments, and variable declaration. Students will learn about accepting input using scanf(), different data types in C like integers and floats, and arithmetic operators. The assigned textbook is Problem Solving and Program Design in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
42 views

C Prog Handouts

This document outlines the course content for ICT 103 Fundamentals of Programming. The course covers C basics including program structure, variables, input/output, arithmetic, conditionals, functions, loops, arrays, strings, pointers, and data structures. It will teach the history of C programming, how to construct, compile, and execute C programs, and cover key C language elements like headers, functions, comments, and variable declaration. Students will learn about accepting input using scanf(), different data types in C like integers and floats, and arithmetic operators. The assigned textbook is Problem Solving and Program Design in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Instructor: Ronald T.

Soriano
Subject: ICT 103
Descriptive Title: Fundamentals of Programming
Course Outline

I. Introduction
A. History of C

II. C Basics
A. C Program Structure
B. Variables
C. Printing Out and inputting variables
D. Arithmetic Operations
E. Comparison Operators
F. Preprocessor Directives

III. Conditionals
A. The if statement, if else and relational operators
B. Switch / case statement

IV. Function
A. Functions
B. Function prototyping
V. Looping and Iterations
A. The for loop statement
B. The while loop statement
C. The do/while statement
D. Break and Continue

VI. Arrays and Strings


A. Single and Multidimensional Arrays
B. Strings
C. Function and Arrays

VII. Pointers
A. Pointers and function
B. Pointers and arrays

Text: Problem Solving and Program Design in C (2nd Ed.)


By: Hanly and Koffman
Reference:
1. Using Turbo C (2nd Ed.)
By: Herbert Schildt
2. C by Dissection (3rd Ed.)
By: Ira Pohl
3. Data Structures and Algorithm Analysis in C
By: Mark Allen Weiss

1
Data Structures and Algorithm

C – is the successor to the language called B


- is often called sa middle level computer language because it combines
the elements of high – level language wit functionalism of assembly
language that allows the manipulation of bits,bytes and addresses w/c are
the basic elements of computer functions.
- considered as structured language because it allows to create the
- program into blocks(ex: pascal begin-end,C { })

Ken Thompson – inventor of B language

Dennis Ritchie - 1st implemented the C programming language on


DEC -PDP -11 that used the UNIX operating system.
DEC – PDP - Digital Equipment Corporation – Programmed Data Processors

1972 – Dennis Rithcie and Ken Thompson combine to augment B’s power

Type of Programming Language :

1. Machine Language(low level) – the native tongue of the computer


2. High Level Language – a programming language whose instructions resemble every language
and using English terminologies as its instructions.
Ex : pascal, C

First Program:
#include<stdio.h>
#include<conio.h>

main( )
{
clrscr( );
printf(“The Joy of C”);
printf(“\nThe Joy of C”);
getch( );
return 0;
}

Output:
The Joy of C
The Joy of C

Process of Constructing, Compiling and Executing a C program


Library
Routines
Source Object Linker
Text editor Compiler
File Module

Exe File

Compiler - program to translate the source program into a machine language or instructions.
Object Code – is the translation of the program source code into a machine code
Source Code – content of the program and it is the input to the compiler.
Linker – responsible for producing executable program.
- combines the object program with additional file needed to execute the program.
Library Files – a file containing the standard functions that was used by your program.

1st Program:
#include<stdio.h>
2
#include<conio.h>

main()
{
clrscr(); /* clears the screen */
printf(“The Joy of C”);
getch();
return 0;
}

To save the file:


 Click file then select save as…
 Save as dialog box will appear. Specify the path and the file name of your program. Ex. C:\
cuz\exer1 then press enter.
To Compile:
 Press alt + F9
To Run:
 Press ctrl + F9
To open the file:
 Press F3 or Click File then select Open.
 Open dialog box will appear. Specify the path and the file name of your program. Ex. C:\
cuz\exer1 then press enter.

C Language Elements

1. #include – directive that gives the program access to a library and it must be enclosed by
angle brackets.
2. Angle brackets (<>) – informs the compiler to search the include directory.
3. Standard Header Files (ex. stdio.h, conio.h) – contains all the predefined functions.
 stdio.h – prototypes for all input and output function.(ex. printf())
 conio.h – prototypes for text handling functions.(ex. clrscr())
4. main() – the label that tags the beginning of the program. When C starts-up the first thing to
call is the main function.
5. Predefined functions (clrscr(), printf())
- predefined means it was written, compiled and linked together with the program after
compiling.
6. Comments (/* */) – describes how the function or code works.
7. Semi-colon(;) – statement terminator.
8. Curly Brace({ }) – it signifies the start and end of the body of the program.
9. Return 0 – return the integer value 0 to fulfill the main function requirements.

Types of Error in C

1. Fatal Error – indicate a serious problem and no object module is created.


Ex. syntax error
2. Warning – a potential problem but not serious enough to avoid creating an object module.

Variable Declaration

Used to communicate to the compiler the names of all variables used in program.

Types of Declaring Variables

1. Local Declaration- are declared inside the main function.


2. Global Declaration- are variables known throughout the entire program and declared outside
the main function.

Data Type in C
A set of values to which a variable can hold.
1. Integer – to hold exact or whole numbers.
2. Character – used to store a single letter, number, and text symbols.
3. Float – numbers with decimal or fractional parts. It can represent the number into 6 significant
digits.
4. Double – similar to float but it can represent the decimal number up to 15 significant digits.
5. Void – valueless
3
Data Type Bit Width Range

Int 16 -32767 to 32767


Char 8 0 – 255
Float 32 3.4E -37 to 3.4E +38
Double 64 1.7E-307 to 1.7E +308
Void 0 valueless

Accepting Input:

scanf() – analogous to the function printf and used for input operation.

syntax: scanf(“formatting codes”,& variable’s name);

where:
formatting codes or specifier – provide the format in which printf and scanf writes a set of value
- also referred for the placeholder for actual values.
& - ampersand or address operator
- used to help the compiler to recall where the variable is located.

Placeholder Variable Type Function Use


%c char printf/scanf
%d or %i int printf/scanf
%f float printf/scanf
%lf double scanf
%s string printf/scanf

2nd Program:
#include<stdio.h>
#include<conio.h>

main()
{
int x;
clrscr(); /* clears the screen */
printf(“Enter any number: ”);
scanf(“%i”,&x);
printf(“The entered number is %d. ”,x);
getch();
return 0;
}

Variable- the memory cell used for storing a programs input data and its computational results. (ex. x)
- also referred to as name locations for storing values.
Identifier – names that are used to refer to variable, function and label. (ex. int)

Types of Identifier

1. Standard Identifier – reserve words that have special meaning in C.


Ex. printf(), clrscr(), getch()
2. User Defined Identifier – user’s identifier to hold value and program result.

3rd Program: Using printf and scanf


#include<stdio.h>
#include<conio.h>

void main()
{
char x,y[80];
int z;
double a;
clrscr();
printf(“Enter a character:”);
scanf(“%c”,&x); or x=getche();
printf(“Enter a string:”);
4
scanf(“%s”,y); or gets(y);
printf(“Enter a whole number:”);
scanf(“%i”,&z);
printf(“Enter a decimal no:”);
scanf(“%lf”,&a);
printf(“\n The character is %c”,x);
printf(“\n The string is %s”,y);
printf(“\n The number is %i”,z);
printf(“\n The decimal is %f”,a);
getch();
}

Arithmetic Operators in C
* - multiplication
- - subtraction
/ - division
+ - addition
% - modulo (returns the remainder)
4th Program: Using operators to convert the entered distance in km into miles.
#include <stdio.h>

int main() {
double distance_km, distance_miles;

// Input distance in kilometers


printf("Enter distance in kilometers: ");
scanf("%lf", &distance_km);

// Convert kilometers to miles (1 kilometer = 0.621371 miles)


distance_miles = distance_km * 0.621371;

// Display the result


printf("Distance in miles: %.2lf\n", distance_miles);

return 0;
}
Different Operators Use in C Language

A. Relational Operators
 < - less than
 > - greater than
 <= - less than or equal to
 >= - greater than or equal to
B. Equality Operators
 == - equal to
 != - not equal to
C. Logical Operators
 ! – negation
 && - logical AND
 || - logical OR

If, if and else, nested if statement


A. if statement
syntax:
if (condition or expression)
{
Statement;
}
5th Program: User’s password program using if statement

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
5
printf(“Enter Password”);
scanf(“%i”,&x);
if (x==123)
{
printf(“That is my password”);
}
getch();
}

B. if and else
syntax:
if (condition or expression)
{
statement;
}
else
{
statement;
}
6th Program: User’s password program using if and else statement

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf(“Enter Password”);
scanf(“%i”,&x);
if (x==123)
{
printf(“That is my password”);
}
else
{
printf(“That is not my password”);
}
getch();
}
C. Multiple Alternative Decision Form or if-else-if statement
syntax:
if (condition1)
{
statement;
}
else if(condition2)
{
statement;
}
else if(condition3)
{
statement;
}
else
{
statement;
}
7th Program: If-else-if
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf(“\n 1.NBA”);
printf(“\n 1.PBA”);
6
printf(“\n 1.ABC”);
printf(“\n Enter Choice”);
scanf(“%i”,&x);
if (x==1)
{
printf(“I Love This Game”);
}
else if (x==2)
{
printf(“I Hate This Game”);
}
else
{
printf(“I Do Not Like the Game”);
}
getch();
}

Truth Table for Logical AND (&&) Truth Table for Logical OR (||)
X Y Z X Y Z
0 0 0 0 0 0
0 1 0 0 1 1
1 0 0 1 0 1
1 1 1 1 1 1
Application:

A. if statement using logical OR

8th Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char choice;
clrscr();
printf(“Enter choice Y/N?:”);
scanf(“%c”,&choice);
if (choice==’Y’ || choice == ‘y’)
{
printf(“You pressed letter %c”, choice);
}
if (choice==’N’ || choice == ‘n’)
{
printf(“You pressed letter %c”, choice);
}
getch();
}
B. if statement using logical AND

9th Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int noise;
clrscr();
printf(“Enter Noise Range:”);
scanf(“%i”,&noise);
if (noise>=10 && noise <= 20)
{
printf(“%i is with in the range”, noise);
}
else
{
printf(“%i is our of range”, noise);
}
7
getch();
}

Case Statement Switch

Switch statement – use to select one of several alternatives.


 useful when the selection is based on the value of a single variable or a simple expression
and the value of the expression maybe of type int or char, but not double.
 process keyboard command such as new selection.
general form:
switch (controlled variable){
case 1:
statements;
break;
case 2:
statements;
break;
default: (optional)
statements;
}
where:
break – the end of the switch statement.
default – statement is executed if no matches found.
getch() – a type of function which reads a character from the keyboard.
getche() – reads a character from keyboard and echo it to the screen.
10th Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf(“Enter Choice”);
ch=getche();
switch (ch){
case ‘a’:
printf(“\n Battleship”);
break;
case ‘b’:
printf(“\n Cruiser”);
break;
case ‘c’:
printf(“\n Destroyer”);
break;
default:
printf(“\n Unknown ship class”);
}
printf(“ Press any key to exit”);
getch();
}
Additional Function

toupper- a function that converts lowercase to uppercase letter.


tolower – the reverse of upper
 header file “ctype.h”
syntax:
toupper(variable of the lowercase letter.

11th Program:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

8
void main()
{
char x,y;
clrscr();
printf(“Enter a lowercase letter:”);
scanf(“%c”,&x);
y=toupper(x);
printf(“\n %c in capital is %c”,x,y);
getch();
}

#define – define various symbolic constants and use in defining a macro.

syntax:
#define NAME value/text
ex.
#define COW 35

macro – a define name.


macro substitution – the process of substituting its replacement text.

12th Program:
#include<stdio.h>
#include<conio.h>
#define LIMP ‘D’
#define JAM “Eddie Vedder”
#define BIZKIT 20
void main()
{
clrscr();
printf(“%s meets kurt”,JAM);
printf(“\n %s initial is %c”,JAM, LIMP);
printf(“\n And %s age is %i”,JAM, BIZKIT);
getch();
}
Labels – is a valid identifier followed by a colon

syntax:
label:
statement;
.
.
goto label;

12th Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
korn:
gotoxy(2,5);
printf(“Enter any number between 1-10”);
scanf(“%i”,&x);
if (x>=1 && x<=10)
{
gotoxy(25,15);
printf(“Good selection”);
}
else
{
gotoxy(2,5);
printf(“ “);
goto korn;
9
}
getch();
}

TOP DOWN DESIGN

A process of decomposing a problem into subprogram.


Function – is a named collection of statements.

Parts of Function

1. Header – provide the functions name and followed by a pair of parameters. ex. main()
2. Body – content of the function (source code);

Types of Function

1. Function without return value and without parameter.


2. Function without return value but with parameters.
3. Function with return values and with parameters.
4. Function with return values and without parameter.

13th Program:

#include<stdio.h>
#include<conio.h>
void mess(); Function prototype
void main()
{
clrscr();
mess(); Function invocation
getch();
}

void mess() Function definition


{
printf(“Message”);
}
where:
1. Function prototype – tells the compiler the data type of the function, function name and
information about the arguments that the function expects.
syntax: type function name(arguments or parameters)
2. Function invocation – the subprogram
syntax: function name(arguments)
3. Function Definition – the c code that describes what the function does.
syntax: the same with function prototype but do not terminates by (;)

A. Function without return values and without parameter

14th Program:

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void draw_rectangle();
void draw_square();
void draw_triangle();

void main()
{
char ch;
clrscr();
printf(“Menu for the figure”);
printf(“\n [A].Rectangle”);
printf(“\n [B].Square”);
printf(“\n [C].Triangle”);
10
printf(“\n Enter choice:”);
ch=getch(),
switch(toupper(ch)){
case ‘A’:
draw_rectangle();
break;
case ‘B’:
draw_square();
break;
case ‘C’:
draw_triangle();
break;
default:
printf(“\n %c is out of selection”,ch);
}
getch();
}

void draw_rectangle()
{
printf(“\n * * * * *”);
printf(“\n * *”);
printf(“\n * * * * *”);
}
void draw_square()
{
printf(“\n * * *”);
printf(“\n * *”);
printf(“\n * * *”);
}
void draw_triangle()
{
printf(“\n /\\”);
printf(“\n /_\\”);
}

B. Function without return values and with parameter (arguments)

Arguments – used to carry information into the function subprogram from the
main function or from another function subprogram

1. Single parameter
15th Program

#include<stdio.h>
#include<conio.h>
#define pi 3.1416
void counter(double a);
void main()
{
double x
clrscr();
printf(“Enter radius:”);
scanf(“%lf”,&x);
counter(x);
getch();
}
void counter(double a)
{
double c;
c= pi*a*a
printf(“\n The area is %2f”,c);
}
2. Multiple Parameter
16th Program

11
#include<stdio.h>
#include<conio.h>
#define RATE 0.06

void total(double p, int n);


void display();
void main()
{
double price;
int number;
clrscr();
display();
printf(“Enter the price:”);
scanf(“%lf”,&price);
printf(“Enter the no. of items:”);
scanf(“%i”,&number);
total(price,number);
getch();
}
void total(double p, int n)
{
double x;
x=n*(p+RATE*p);
printf(“\n %i at %.2f pesos each”,n,p);
printf(“\n The total bill is %.2f pesos”,x);
}
void display()
{
printf(“\n This program will solve for the total price”);
}

C. Function with parameter and with return values:

17th Program

#include<stdio.h>
#include<conio.h>
void deftones();
double multiply(double a,double b);
void main()
{
double x,y,splonks;
clrscr();
deftones();
printf(“Enter 2 nos.:”);
scanf(“%lf”,&x,&y)
splonks=multiply(x,y);
printf(“\n The product is %.2f”,splonks);
getch();
}
double multiply(double a,double b)
{
double z;
z=a*b;
return z;
}
void deftones()
{
printf(“This program will multiply 2 entered numbers”);
}

D. Function with return values and without parameter.

18th Program

#include<stdio.h>
12
#include<conio.h>
int cube();
int square();
int x;

void main()
{
int i,v;
clrscr();
printf(“Enter an integer:”);
scanf(“%i”,&x);
i=cube();
printf(“\n The cube is %i”,i);
v=square();
printf(“\n The square is %i”,v);
getch();
}

int cube()
{
int a;
a= x*x*x;
return a;
}

int square()
{
int b;
b= x*x;
return b;
}

INCREMENT AND DECREMENT OPERATORS

x++ or x= x+1 or x+=1


x-- or x=x-1 or x-=1

19th Program:

#include<stdio.h>
#include<conio.h>

void main()
{
int x;
printf(“Enter a number:”);
scanf(“%i”,&x);
x=x+1;
printf(“\n The value of x after incremented by 1 is %i”,x);
getch();
}

LOOPING

Repeatition of steps in a program. It allows a set of instructions to be reapeated until a certain


condition is reached.

In C there are 3 loop control statements:

a. for loop
b. while loop
c. do/while

For Loop:
general form:
13
for (initialization; condition; updating)
{
statement;
}

note: do not terminate by semicolon(;)

where:
initialization – is an assignment statement that is use to set the loop control variable.
condition – is the relational expression that determines when the loop will exit.
updating – defines how the loop control variable will change each time the loop is repeated.
note: The for loop will continue to execute as long as the condition is true.

20th Program

#include<stdio.h>
#include<conio.h>
#define CONS 3
void main()
{
int z;
clrscr();
for(z=1; z<=CONS; Z++)
{
printf(“\n %i”,z);
}
getch();
}

21st Program

#include<stdio.h>
#include<conio.h>
void main()
{
int z, sq;
clrscr();
for(z=1; z<=5; Z++)
{
sq=z*z;
printf(“\n %i - %i”,z, sq);
}
getch();
}

Accumulator – a variable used to store a value being computed in increments/decrements during the
execution of the loops. Used to accumulate the sum and product.

ex. Make a program that will compute the sum from 1 to N.

#include<stdio.h>
#include<conio.h>
void main()
{
int x, z, sum=0;
clrscr();
printf(“Enter any number:”);
scanf(“%i”,&x);
for (z=1; z<=x; z++)
{
sum=sum+z;
}
printf(“The sum is %i”,sum);
getch();
}

14
1. #include<stdio.h>
#include<conio.h>
double solve(double x1,double x2,double grade);
void frint xy(int p1,int p2,char s[80]);

void main()
{
double a1,a2,a11,a22,m1,m2,ave1,ave2,final;
clrscr();
frintxy(4,3,”MIDTERM”);
frintxy(5,5,”ENTER Q1:”);
scanf(“%lf”,&q1);
frint(5,6,”ENTER Q2:”);
scanf(“%lf”,&q2);
frintxy(5,7,”ENTER MIDTERM:”);
scanf(“%lf”,&m1);
frintxy(5,9,”Your Average1 is:”);
scanf(“%.2f”,ave1);
frintxy(25,20,”…press any key to cont…”);
getch();
frintxy(40,3,”PRE-FINAL”);
frintxy40,5,”ENTER Q!:”);
scanf(“%lf”,&a11);
frintxy40,6,”ENTER Q2);
scanf(“%lf”,&q22);
frintxy(40,7,”ENTER PRE-FINAL:”);
scanf(“%lf”,&m2);
frintxy(40,9,”Your Average2 is:”);
scanf(“%.2f”,ave2);
final=(ave1+ave2)/2;
frintxy(25,17,”Your final grade is:”);
printf(“%.2f”,final);
getch();
}
double solve(double x1,double x2,double grade)
{
double av;
av=((x1+x2)/2+grade)/2;
return av;
}
void frintxy(int p1,p2,char s[80])
{
the same with ex.3 gotoxy(p1,p2);
printf(“%s”,s)
}

15
Create a program that will compute the sum of
numbers from 1 to N. Where N is the entered
number.

Enter any number: 5


The sum is 15

1+2+3+4+5=15

16

You might also like