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

Week2 3 VariablesDataTypes Friday

The document is a lecture slide presentation on data types and variables in the C programming language. It introduces different data types in C and how they are used to store and represent data. It also covers input/output stream operations and common operators that can be performed on different data types. The presentation provides an overview of key concepts for working with data in the C language.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Week2 3 VariablesDataTypes Friday

The document is a lecture slide presentation on data types and variables in the C programming language. It introduces different data types in C and how they are used to store and represent data. It also covers input/output stream operations and common operators that can be performed on different data types. The presentation provides an overview of key concepts for working with data in the C language.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

CMP1401: Introduction to Programming with C


0 1
14
Hassan Imani

P
Bahçeşehir Üniversitesi

C M
Week 2: Data Types and Variables
Oct. 13, 2023

Bahcesehir University Week 2 – Oct. 13, 2023 1 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

In this lecture...

0 1
4
An overview of the C programming language

How do we take user input in C?


P
How do we show the user a data using C?
1
In which format do we maintain data in C?

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

Programming – Some Definitions

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

· Bahcesehir University Week 2 – Oct. 13, 2023 3 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Types of Programming Languages


High-level programming languages
Examples are: C++, Fortran, Java, Python

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

· Bahcesehir University Week 2 – Oct. 13, 2023 4 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

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

Compiled code is “executable’ -- suitable for running on a computer

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”)

· Bahcesehir University Week 2 – Oct. 13, 2023 5 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Programming Jargon

Doctors do not discuss diagnoses with each other in simple terms

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

· Bahcesehir University Week 2 – Oct. 13, 2023 6 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

C
The most widely used programming language of all time
Due to its speed, stability, and near-universal availability

Developed by Dennis Ritchie 1969-1973


0 1
Is a compiled language

14
Statically- and weak- typed (all data has a type, but implicit conversions may

P
be performed)

Enables programmers efficiently implement algorithms and data

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

int main() // function main begins program execution

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

We’ll break down the simple program:

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

· Bahcesehir University Week 2 – Oct. 13, 2023 10 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

int main()

Designated start of a program


Exactly one function in every program must be main
0 1
14
Main will usually call other functions to help perform its job

int main() → main function “returns” an integer value

Syntax:
P
int main ()
{

}
C M
/* body of your code */

· Bahcesehir University Week 2 – Oct. 13, 2023 11 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

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

#define, #if, #endif are other examples P


C M
Preprocessor directives (like #include) do not end with a semicolon
(Whereas every C statement must end with a semicolon)

Here is a detailed definition with examples

· Bahcesehir University Week 2 – Oct. 13, 2023 12 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

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

With a variable, you can:


Read the value in it
P 1
M
Write a value to it
Change the value of it

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

· Bahcesehir University Week 2 – Oct. 13, 2023 13 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Putting Data in Variables

*** ( All variables must be declared before use ) ***

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);

· Bahcesehir University Week 2 – Oct. 13, 2023 14 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

C Data Types

Built-in types (primitive data types)


Defined in the C compiler
0 1
14
Can be integral, floating point, or void:
Integral types are capable of handling whole numbers
Floating point types are capable of specifying values that may have
fractional parts

P
void type describes an empty set of values

C
different amounts of memoryM
Different data types have different ranges of values and require

· Bahcesehir University Week 2 – Oct. 13, 2023 15 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Built-in Data Types - 1: Integer Types

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

unsigned short int (2 bytes)


15

int (4 bytes = 32 bits) M


Has an integer value between 0 and +(216 ) − 1

C
Has an integer value between −(231 ) and +(231 ) − 1
unsigned int has integer value between 0 and 232 − 1

long long (8 bytes)


· Bahcesehir University Week 2 – Oct. 13, 2023 16 / 37
Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Built-in Data Types - 2: Floating Point Types

float (4 bytes = 32 bits)


Floating-point number
Can take positive or negative value
0 1
double (8 bytes = 64 bits)

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

If precision is not crucial, then use float

Bahcesehir University Week 2 – Oct. 13, 2023 17 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

More on char

A variable of type char holds a single character


0 1
1
Represented within single quotation marks ‘’
4
“a” is a string of characters containing one character
‘a’ is a single char constant

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

· Bahcesehir University Week 2 – Oct. 13, 2023 18 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Type Casting

Changing an variable of one data type into another

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;

· Bahcesehir University Week 2 – Oct. 13, 2023 19 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

<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

#include<stdio.h> tells the compiler to include information about the


standard input/output library

· Bahcesehir University Week 2 – Oct. 13, 2023 20 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

The printf Function

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

Escape Sequence (1/2)

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

Some escape sequences:

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

· Bahcesehir University Week 2 – Oct. 13, 2023 22 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Escape Sequence (2/2)

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!”

· Bahcesehir University Week 2 – Oct. 13, 2023 23 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Placeholders (Format Specifiers)

How do we display the sum of 20 and 15? printf() only prints strings!

We put a placeholder format code in the string:


0 1
printf("20+15 is %d ", 20+15);

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

%s – string (char sequence of characters)


%x – int – unsigned hex value

· Bahcesehir University Week 2 – Oct. 13, 2023 24 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

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
}

· Bahcesehir University Week 2 – Oct. 13, 2023 25 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

The scanf Function

scanf is used to read information from a standard input device


(keyboard)
& is used to indicate the memory address of the variable
0 1
#include <stdio.h>
14
We use it so the input stream is written in that memory address

main()
P
M
{
int i;
printf("Please print a number: ");

}
scanf("%d", &i);

C
printf("you entered: %d \n", i);
return 0;

· Bahcesehir University Week 2 – Oct. 13, 2023 26 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Assignment Operators

Assigns a value to a variable


When new value is assigned, old one is overwritten (LOST)

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

· Bahcesehir University Week 2 – Oct. 13, 2023 27 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Example: Assignment Operators

#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
}

· Bahcesehir University Week 2 – Oct. 13, 2023 28 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

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

Division: The / operator -- divides its first operand by the second

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

· Bahcesehir University Week 2 – Oct. 13, 2023 29 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Example: Arithmetic Operators


#include <stdio.h>

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

Arithmetic Assignment Operators

Assume: int c = 3, d = 5, e = 4, f = 6, g = 12:

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

· Bahcesehir University Week 2 – Oct. 13, 2023 31 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Example: Arithmetic Assignment Operators

#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

Decrement a by 1 then use the new value of a

· Bahcesehir University Week 2 – Oct. 13, 2023 33 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

Example: Increment Operators

#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

Summary & Tips

Storing a variable (allocate memory)


0 1
Proper I/O design
Properly prompt for input

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

· Bahcesehir University Week 2 – Oct. 13, 2023 35 / 37


Programming C Fundamentals Data Types Stream I/O Operators Sum-Up

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

You might also like