C Prog Handouts
C Prog Handouts
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
VII. Pointers
A. Pointers and function
B. Pointers and arrays
1
Data Structures and Algorithm
1972 – Dennis Rithcie and Ken Thompson combine to augment B’s power
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
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;
}
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
Variable Declaration
Used to communicate to the compiler the names of all variables used in program.
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
Accepting Input:
scanf() – analogous to the function printf and used for input operation.
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.
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
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;
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
#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:
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();
}
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();
}
syntax:
#define NAME value/text
ex.
#define COW 35
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();
}
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
13th Program:
#include<stdio.h>
#include<conio.h>
void mess(); Function prototype
void main()
{
clrscr();
mess(); Function invocation
getch();
}
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 /_\\”);
}
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
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”);
}
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;
}
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
a. for loop
b. while loop
c. do/while
For Loop:
general form:
13
for (initialization; condition; updating)
{
statement;
}
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.
#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.
1+2+3+4+5=15
16