0% found this document useful (0 votes)
22 views

Lecture 1 - Introduction

Uploaded by

zaidkhattab1012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lecture 1 - Introduction

Uploaded by

zaidkhattab1012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Computing Fundamentals

(CS116)
Introduction
Programming: Instructing the Computer
• Computers are dumb devices that perform only the tasks applied by
the user
The C Programming Language
• The most influential programming language in the short history of computing!
• Compact, fast, and powerful
• Closer to the hardware than other high-level languages, introducing what is
known as “mid-level” language
• Very popular (top 5 – 10 world-wide)
• Backward compatible with the highly-popular C++
• Supports modular programming style (dividing your program into independent
units, each performing a specific task)
• Useful for a very wide spectrum of applications (operating systems development, embedded
systems, system software, game development, compilers and interpreters, database systems, network programming, real-
time systems, scientific and mathematical computing, graphics programming, security software, utilities and tools, artificial
intelligence, web development)
• C is the native language of UNIX and its popular variants (Linux, BSD)
The C Programming Language - History
• (1972) Developed by Dennis Ritchie
• (1983) Standardized by The American National Standards Institute (ANSI), producing the known as
ANSI C or C83
• (1990) Adopted by the International Standardization Organization (ISO), producing what is known as
ANSI/ISO C or C90
• (1999) Last major changes adopted
Levels of Programming Languages What software do we need to run C
programs on our machines?

For MS Windows users any of these


options will do the job:
Scripting language (R, • MS Visual Studio
Python) • MS Visual Studio Code
Mid/High level languages (C, • Code::Blocks
C++) • NetBeans
• Notepad++
Assembly language (MOV EAX, 5) • Eclipse

For Apple MacOS users any of these


Machine language (Binary 0110010)
options will do the job:
• MS Visual Studio for Mac
Hardware (CPU, Memory) • MS Visual Studio Code for Mac
• Code::Blocks
• Xcode
• CLion
• Eclipse
• Vim (for the super geeky ones)
Our First Program! (Known as Hello World!)
#include <stdio.h>
int main(void)
{
/* My first program */
printf("Assalamu Aleikom\n");
return 0;
}
• Case sensitive!
• End each program statement ends with a semicolon (;) except comments
• The program execution starts from the beginning of main() and ends by
the end of main()
• Curly brackets { } signify the begin and end of the program
Understanding Our First Program
• #include <stdio.h>
• Indicates that, in our program, we will be using functions (commands) taken from a library of
commands called stdio.h which was pre-written by professional programmers
• In our program, the function printf(…) was taken from the library stdio.h
• int main (void)
• This line defines a very special function called main(void). As the name implies, it’s the main
function of our program, where the beginning and end of program execution are defined.
• It returns an integer value “int” (to be discussed later)
• The keyword void enclosed in the parentheses specifies that the function main(void) takes
no inputs (we also call the function inputs arguments)
• return 0;
• Indicates the successful completion of the execution of main(void)
• Returns to the system a status value of 0 (returning a none-zero value indicates an error or
abnormal program termination)
Functions (a Quick Visit)
• Will be discussed later in detail (sometime after the midterm)
• Example: Variable

Input
Output (return) Function

Return
printf Options (known as: escape sequences)
• printf(“Alsalamo Alikom \n");
• A function in the C library stdio.h that simply prints or displays its argument
(input) on screen
• \n means newline. After printing Assalamu Aleikom the typing cursor goes to
next line
A Program to Multiply Two Numbers
#include <stdio.h> Output:
The product of 7 and 3 is: 21
int main(void)
{
int x;
int y;
int z;
x = 7;
%d or %i int
y = 3;
%c char
z = x * y; %f float
printf(“The product of 7 and 3 is: %d\n“, z); %s string
return 0;
}
Declarations
• Declare an integer  int x;
• Declare a character  char x;
• Declare a float number (with decimal places)  float x;
• Multiple declarations are allowed  int x, y, z;

• Restrictions on variable names


• Names are case sensitive ex. int x; int X;
• No dollar sign ex. int comp$fund;
• Underscores are allowed ex. int comp_fund;
• Names can contain numbers but not at the beginning ex int comp3; int 3comp;
• Names can not be keywords of C language ex. int int; int char; int long;
Initializations
• int x;
• x = 5;
• int x = 5;
• float pi = 3.14;
• char x = ‘A’;
• int x = 5, y = 3, z = 2;
What is wrong here?
• int x
• int X$E;
• Int X=‘A’;
• Int X=3.14;
• Char X=3;
• char X=‘3’; /* Nothing wrong, it means the ASCII value of 3, which is 51*/
• Char ABC_DEF=‘A’
Arithmetic Operators in C language
Operator Description Example
+ Adds two operands. A + B = 30
Subtracts second operand from the
− A − B = -10
first.
A = 10 * Multiplies both operands. A * B = 200
B = 20
/ Divides numerator by de-numerator. B/A=2

Modulus Operator and remainder of


% B%A=0
after an integer division.
Increment operator increases the
++ A++ = 11
integer value by one.
Decrement operator decreases the
-- A-- = 9
integer value by one.

Decimal points will be lost if a variable is declared as int instead of float


Area of a Circle (1)
• How to calculate the area of a circle?
• Area= pi * r * r

What is pi?
How can we represent pi?
How to compute a power
of 2?
Which values are integers
and which values are
floating-point numbers?
Area of a Circle (2)
#include<stdio.h>
int main()
{
float radius, area;
radius=3.1;
area = 3.14 * radius * radius;
printf("Area of Circle : %f", area);
return 0;
}
Let’s Test What We Learned
#include <stdio.h>
int main (void)
6 + a / 5 * b = 16
{ a / b * b = 24
int a = 25; c / d * d = 25.000000
int b = 2; -a = -25
float c = 25.0;
float d = 2.0;
printf ("6 + a / 5 * b = %i\n", 6 + a / 5 * b); /* */
printf ("a / b * b = %i\n", a / b * b);
printf ("c / d * d = %f\n", c / d * d);
printf ("-a = %i\n", -a);
return 0;
}

You might also like