Week2 3 VariablesDataTypes Friday
Week2 3 VariablesDataTypes Friday
P
Bahçeşehir Üniversitesi
C M
Week 2: Data Types and Variables
Oct. 13, 2023
In this lecture...
0 1
4
An overview of the C programming language
M
Which operations can we perform on data in C?
C
Bahcesehir University Week 2 – Oct. 13, 2023 2 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
Computer program
set of instructions that tell a computer exactly what to do
0
e.g. add up a set of numbers, compare two numbers and make a 1
decision based on the result, etc.
Programming language
14
P
for a computer to recognize the instructions they need to be written in a
programming language
e.g. C, C++, Java, Python, Javascript, PHP, Perl, etc.
Compiler
C M
Translates a program written in computer language into a form that a
computer can execute
e.g. .exe files
0
e.g. Python code at an ATM machine for a withdrawal of $100:
x = 100 1
4
if balance x:
print ’Insufficient balance’
1
else:
print ’Please take your money’
Assembly
P
Makes machine language more readable by humans
A translator (“assembler”) is needed to translate to machine code
e.g.
C M
Each processor family has its own assembly language
MOVE, ADD, SUB, END
Machine language (machine code)
Directly understood by the computer, doesn’t need to be translated
Lowest level represented by an assembled or compiled program
e.g. 10010101100101001111101010011011100101
Compilers
It is a computer program
0 1
4
Translates a program (“source code”) written in high-level language
to machine language
executed
P 1
Entire source code needs to be compiled before the program can be
M
A compiled code can be executed over and over again
C
Compilation does not process input or produce output
Input/output used when the compiled code is executed (“i.e., the
program is running”)
Programming Jargon
0 1
“transverse compound fracture of the tibia” instead of “broken leg”
14
In programming, too, we use terminology to communicate accurately
P
I’ll explain programming terminology and highlight them in slide
M
Please interrupt and ask if I am using a term you haven’t heard before
C
Visit this blog for the a fun video and for some programming terminology
C
The most widely used programming language of all time
Due to its speed, stability, and near-universal availability
14
Statically- and weak- typed (all data has a type, but implicit conversions may
P
be performed)
M
structures -- useful for computationally intense programs
GNU Scientific Library, Mathematica, MATLAB are written in C
C
Compilers, libraries and interpreters of other programming languages
are often implemented in C ( e.g. Python, Perl, PHP)
Portable and thus widely used for system programming (operating
systems, embedded system applications)
· Bahcesehir University Week 2 – Oct. 13, 2023 7 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
In C Programs...
0 1
14
A C program consists of functions and variables
A function contains statements that specify the computing operations
to be done
P
Variables store values used during the computation
C M
· Bahcesehir University Week 2 – Oct. 13, 2023 8 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
A Simple Program
0 1
14
#include <stdio.h> // allows program to output data to the screen
P
{
printf("Welcome to C!\n"); // display message
M
return 0; // indicate that program ended successfully
} // end function main
C
· Bahcesehir University Week 2 – Oct. 13, 2023 9 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
1
#include <stdio.h>
0
int main()
{
4
printf ( ”Welcome to C!\n”);
return 0;
1
}
#include
int main()
P
“syntax” of I/O in C
C M
the form, not the meaning
set of rules that define the correctly structured code in a programming
language
“Commenting out”/commenting in
int main()
Syntax:
P
int main ()
{
}
C M
/* body of your code */
Preprocessor Directives
Lines that begin with # are processed by the preprocessor before the
program is compiled
0 1
14
#include <stdio.h> is a preprocessor directive
Notifies the preprocessor to include the input/output header file in the
program
Variables
1
A variable is represented by:
Storage -- where is it stored, for example data, stack, heap
Scope -- who can see us, for example global, local...
4
Type -- what is our type, for example int, int*...
Address -- where are we located 0
Value -- what is our value
C
Variables are given names -- called an identifier
The name should indicate the purpose (THIS IS IMPORTANT.)
Name must begin with a letter
Name cannot be a reserved word in the C language
0 1
At initialization -- e.g. int y = 45;
14
We can put data into variables in one of three ways;
e.g.x = 1.2;
P
Using an assignment (variable = expression;)
e.g.
C M
area = (base * height) / 2;
Upon taking an input -- scanf(”%s”, &variable);
float height; scanf("%s", &height);
C Data Types
P
void type describes an empty set of values
C
different amounts of memoryM
Different data types have different ranges of values and require
1
bool (1 byte = 8 bits)
Has a boolean value (true / false)
char (1 byte)
4
contains members of the basic execution character set (ASCII) 0
short int (2 bytes = 16 bits)
P 1
Has an integer value between −(2 ) and +(215 ) − 1
C
Has an integer value between −(231 ) and +(231 ) − 1
unsigned int has integer value between 0 and 232 − 1
14
Double-precision floating-point number
long double
80-bit extended precision
P
C M
If precision is more of a concern for you than storage, use double
More on char
P
M
Each character has a numeric value from the ASCII table
e.g. ‘A’: 65; ‘a’ : 97
C
ASCII code is the numerical representation of a character
Type Casting
0 1
4
Implicit
Occurs when operands are of different types
e.g. short a=2000;
int b;
b=a;
P 1
Explicit
C M
When the developer wants to change the variable type
e.g. int sum = 17, count = 5;
double mean;
mean = (double) sum / count;
<stdio.h>
Is a C library
0 1
Allows the program to:
output text to the screen
get input from the user
14
P
This file must be included for any C program that inputs/outputs data
C M
Input/Output in C are accomplished with streams of characters
0 1
4
printf is used to display information on screen (the standard output)
#include <stdio.h>
main()
{
P 1
M
printf("Welcome to C!\n");
}
C
· Bahcesehir University Week 2 – Oct. 13, 2023 21 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
In a program:
printf("Welcome to C!\n");
0 1
4
The characters \n are not printed on the screen
\n is an escape sequence
P 1
The backslash (\) is called an escape character
indicates that a special character is to be output
C M
\n: newline → the cursor (current screen-position indicator) moves to
the beginning of the next line
\t: tab → a tab is printed
\”: double quotes → double quotes are printed
1
(Example from https://en.cppreference.com/w/cpp/language/escape)
#include <stdio.h>
int main()
{
4 0
}
return 0;
P 1
printf("This\nis\na\ntest\n\nShe said, \"Hello!\" ");
Output:
This
a
C
is
test
M
She said, “Hello!”
How do we display the sum of 20 and 15? printf() only prints strings!
14
%d literally “holds the place” for the actual number that is the result of
P
adding 20 to 15
Other format specifiers that work with printf():
%f – float
%lf – double
C M
%c – char (single character)
%d (same as %i) – int
Examples
#include <stdio.h>
0 1
main()
{
14
printf("Welcome to CMP %d, Introduction to Programming in C!\n",
1401);
P
M
int i = 60;
printf("There are %d students in this class \n", i );
C
}
main()
P
M
{
int i;
printf("Please print a number: ");
}
scanf("%d", &i);
C
printf("you entered: %d \n", i);
return 0;
Assignment Operators
0 1
4
“=” is the assignment operator
Syntax: Variable = Expression ;
e.g.
P 1
total pairs = total count / 2;
The variable to be changed is always on the left of “=”
On the right of “=” must be an expression, examples:
Constants: age = 18;
C M
Variables: new budget = money earned;
Arithmetic expressions: circumference = diameter *
3.14159;
Data types on both sides of “=” must be the same
#include <stdio.h>
int main ()
0 1
{
int a, b; // a:?, b:?
a = 10; // a:10, b:?
14
P
b = 4; // a:10, b:4
a = b; // a:4, b:4
b = 7; // a:4, b:7
printf("a: ");
printf(a);
printf(" b: %d", b);
return 0; C M
}
Arithmetic Operators
1
Addition: The + operator -- adds its operands
E.g. 3+4 evaluates to 7
E.g. 12 − 3 evaluates to 9
4 0
Subtraction: The − operator -- subtracts the second operand from the first
E.g. 8 ∗ 4 evaluates to 32
P 1
Multiplication: The ∗ operator -- multiplies its operands
C M
E.g. 100 / 5 evaluates to 20
If both operands are integers, the result is the integer portion of the quotient.
For example, 17 / 3 is 5, with the fractional part discarded.
Modulus: The % operator -- finds the modulus of its first operand with respect
to the second (remainder of integer division)
E.g. 19 % 6 evaluates to 1
int main()
{
0 1
4
int number1, number2;
1
printf("Enter a number: ");
scanf("%d", &number1);
P
printf("Enter another number: ");
scanf("%d", &number2);
printf("number1
printf("number1
printf("number1
printf("number1
printf("number1
=
+
-
*
/
M
%d; number2=%d\n", number1, number2);
number2 = %d\n", number1 + number2);
C
number2 = %d\n", number1 - number2);
number2 = %d\n", number1 * number2);
number2 = %f\n", number1 / number2);
printf("number1 % number2 = %d\n", number1 % number2);
return 0;
}
· Bahcesehir University Week 2 – Oct. 13, 2023 30 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
0 1
Assignment
Operator
Example
14
Explanation Assigns
+=
-=
c += 7
d -= 4
c=c+7
d=d-4
P c = 10
d=1
*=
/=
%=
e *= 5
f /= 3
g %= 9
C M e=e*5
f=f/3
g=g%9
e = 20
f=2
g=3
#include <stdio.h>
0 1
int main()
{
int number1 = 15;
14
printf("number1 = %d \n", number1);
number1 +=5;
P
M
printf("number1 = %d \n", number1);
return 0;
}
C
· Bahcesehir University Week 2 – Oct. 13, 2023 32 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
Increment/Decrement Operators
a++ : Post-increment
0 1
4
Use the current value of a in the expression, then increment a by 1
++a : Pre-increment
1
Increment a by 1 then use the new value of a
a -- : Post-decrement
P
--a: Pre-decrement
C M
Use the current value of a in the expression, then decrement a by 1
#include <stdio.h>
0 1
int main()
{
int number1 = 5;
14
printf("number1 is %d \n", number1);
printf("number1++ is %d \n", number1++);
P
M
printf("++number1 is %d \n", ++number1);
printf("finally, number1 is now %d \n", number1);
return 0;
}
C
· Bahcesehir University Week 2 – Oct. 13, 2023 34 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
14
Don’t assume the user knows what you’re asking for
P
Echo the input by displaying what was read (gives user a chance to
verify the data)
C M
Use variables. Never hard code values.
Use parentheses to change the order of arithmetic operations
Exercise - 1
0 1
14
Write a C program that inputs three integers from the keyboard and prints
P
the sum, average, and product.
C M
· Bahcesehir University Week 2 – Oct. 13, 2023 36 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up
Exercise - 2
0 1
14
Write a program that gets the radius of a circle and prints the diameter,
P
area and circumference. Use the constant value 3.14159 for pi value.
C M
· Bahcesehir University Week 2 – Oct. 13, 2023 37 / 37