Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Qualification
• Postdoc.
Department of Electrical Engineering, University of Minnesota, Duluth, USA.
• Ph.D.
Lahore University of Management Sciences (LUMS), Lahore, Pakistan.
Carried one-year PhD research at University of Ottawa, Canada.
• MS (Computer Science)
Lahore University of Management Sciences (LUMS), Lahore, Pakistan.
(With Honors, Merit certificate was awarded)
• M.Phil. (Electronics)
Quaid-i-Azam University, Islamabad, Pakistan. (Merit Certificate was awarded)
• Post Graduate Course
Post-Graduate Course in Lasers and Optics, PIEAS, Nilore, Islamabad, Pakistan.
• B.Sc. Electrical Engineering
University of Engineering and Technology (UET), Lahore, Pakistan.
• F.Sc. Government College, Lahore.
Dr. Muhammad Yousaf Hamza
Course Contents
• Basic components of a computer
• Programming languages
• Introduction to operating system
• Programming in C with examples and
applications

Dr. Muhammad Yousaf Hamza


Text Books:

1) P. K. Sinha, Computer Fundamentals, BPB


Publications, 2004
2) Kernighan and Ritchie, The C Programming
Language
3) Yashwant Kanitkar, Let Us C
4) Bradley L. Jones and Peter Aitken , Teach
Yourself C in 21 Days, 6th Edition
Objective of the Course
• To learn computer fundamentals
• To learn C language.
Why Learn C?
• C is the base language of most of the other
programming languages.
• To be a Good Programmer, one must know
fundamentals of C programming language.
• Simulations ----- lets us apply maths to the
real world.

Dr. Muhammad Yousaf Hamza


Information
• Course related stuff is available on data server
of PIEAS
\\zambeel.pieas.edu.pk>FacultyShare>Muhammad
Yousaf Hamza Dr>Public> 1_CF Zero Semester

• We will use Borland C++ Version 5.02


• Lab. Venue: Computer Centre (I –Block)

Dr. Muhammad Yousaf Hamza


Exams Schedule
• Mid Term Exam
December 16–20, 2019

• Final Exam
January 18–25, 2020

Dr. Muhammad Yousaf Hamza


Let us Write First C Program

Dr. Muhammad Yousaf Hamza


Approach To Our First C Program
x = 5;
y = 7;
z = x + y;

x, y and z are integer variables

Dr. Muhammad Yousaf Hamza


Data Types
• Integer variables (int)
For example:
– Number of students
– Number of tables
• Floating point variables (float)
For quantities which may contain decimal point
such as distance, area, and temperature.
• Character variables (char)

At this stage, we will focus only on integers.

Dr. Muhammad Yousaf Hamza


Approach To Our First C Program
x = 5;
y = 7;
z = x + y;

x, y and z are integer variables

Dr. Muhammad Yousaf Hamza


#include <stdio.h> The header file "std" - "io". h stands for Standard Input Output. It has the
information related to input/output functions

int main( )
{ //Our First C Program
int x, y, z;
x = 5;
y = 7; Function declaration in C
z = x + y;

printf(“%d", z);

getchar(); The program will run and close unless we add this statement i.e. program waits
for an input from user before proceeding any further
return 0; Note that we're writing a function so we have to return a value hence "return 0"

Dr. Muhammad Yousaf Hamza


#include <stdio.h>
int main( )
{ //Our First C Program
int x, y, z;
x = 5;
y = 7;
z = x + y;

printf(“%d", z);
%d is used for digits
%c is used to display character
%f for float variable
%s for string variable

getchar();
return 0;
}
12
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z;
x = 5;
y = 7;
z = x + y;

printf(“Sum is %d", z); // Compare it with printf(“%d", z);


getchar();
return 0;
}
Sum is 12
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d", z);


printf("Difference is %d", m);

getchar();
return 0;
}
Sum is 12Difference is -2
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\n", z);


printf("Difference is %d", m);
getchar();
return 0;
}
Sum is 12
Difference is -2
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m); // two outputs in


//one printf
getchar();
return 0;
}
What’s output?
S
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x, y, z, m;
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m); // two outputs in


//one printf
getchar();
return 0;
}
What’s output?
Sum is 12
Difference is -2 Dr. Muhammad Yousaf Hamza
C is highly case sensitive
#include <stdio.h>
int main( )
{
int x, y, z, Z;
x = 5;
y = 7;
z = x + y;
Z = x - y;
printf(“%d\n", z); // 12
printf(“%d", Z); // -2
getchar();
return 0;
}
//Note: Z is different from z.
Dr. Muhammad Yousaf Hamza
Escape Sequences
Sequence Meaning
\a Bell (alert)
\b Backspace
\n Newline
\t Horizontal tab
\\ Backslash
\' Single quote
\" Double quotation

Dr. Muhammad Yousaf Hamza


Escape Sequences
• The name reflects the fact that the backslash
causes an “escape” from the normal way
characters are interpreted.

• In this case, the n is interpreted not as the


character ‘n’ but as the new line character.

Dr. Muhammad Yousaf Hamza


#include <stdio.h>
int main( )
{
Exercise
Try out various escape
int x, y, z, m;
sequences in this program.
x = 5;
y = 7;
z = x + y;
m = x - y;

printf("Sum is %d\nDifference is %d", z, m);

getchar();
return 0;
}
Sum is 12
Difference is -2
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x = 5, y = 7, z; // initialization with declaration

z = x + y;

printf(“%d", z);

getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


“Hello World”
//Most of the people write this program first
#include <stdio.h>
int main ( )
{
printf ("Hello World");
getchar();
return 0;
}

Dr. Muhammad Yousaf Hamza


More About printf()

Dr. Muhammad Yousaf Hamza


More about printf()
• The printf function is used to output information
(both data from variables and text) to standard
output.
• It takes a format string and parameters for output.
a = 10;
b = 20;
• printf("The number is %d", a);

• printf("The numbers are %d and %d",a,b);

• printf("The numbers are %d and %d",a,b*3);

• printf("The numbers are %d and %d",a,b);

Dr. Muhammad Yousaf Hamza


More about printf()
• The printf function is used to output information
(both data from variables and text) to standard
output.
• It takes a format string and parameters for output.
a = 10;
b = 20;
• printf("The number is %d", a);
The number is 10
• printf("The numbers are %d and %d",a,b);
The numbers are 10 and 20
• printf("The numbers are %d and %d",a,b*3);
The numbers are 10 and 60
• printf("The numbers are %d and %d",a,b);
The numbers are 10 and 20
Dr. Muhammad Yousaf Hamza
More about printf()
The format string contains:
– Literal text: is printed as is without variation
– Escaped sequences: special characters preceeded
by \
– Conversion specifiers: % followed by a single
character
• Indicates (usually) that a variable is to be
printed at this location in the output stream.
• The variables to be printed must appear in the
parameters to printf following the format
string, in the order that they appear in the
format string.

Dr. Muhammad Yousaf Hamza


C doesn’t care about spaces
#include <stdio.h> #include <stdio.h>
Both of these
int main ( ) int programs are
{ main
printf ("Hello World”); (
the same as far as
) your compiler is
return 0; { concerned.
} printf
(
"Hello World" We SHOULD lay
) out our C program
;
return
to make them look
0 nice.
;
}

Dr. Muhammad Yousaf Hamza


C doesn’t care about spaces
In the most general sense, a statement is a part of
your program that can be executed.
a = 10;
An expression is a statement.
a = a+1;
A function call is also a statement.
printf("%d”, a);
Other statements ……
C is a free form language, so you may type the
statements in any style you feel comfortable:
a=
a+
1;
a = a + 1; a = 6;
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d\n", x);

x = x + 3;
printf(“%d\n", x);

printf(“%d\n", x*6);

printf(“%d\n", x);
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d\n", x); // 5

x = x + 3;
printf(“%d“\n, x); // 8

printf(“%d\n", x*6); // 48

printf(“%d\n", x); // 8
getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza

You might also like