SlideShare a Scribd company logo
1 of 33
CSE240 – Introduction to
Programming Languages
Lecture 07:
Programming with C
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Imperative Paradigm
Fully specified and fully controlled
manipulation of named data in a stepwise fashion.
which means that:
Programs are algorithmic in nature:
do this, then that, then repeat this ten times
Focuses on how rather than what.
variables
functions (methods in Java)
Loop statements
Conditional statements
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Paradigms
variables
functions (methods in Java)
Loop statements
Conditional statements
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Paradigms
variables
functions (methods in Java)
Loop statements
Conditional statements
// Different from
// Java
Arrays
Structs
Pointers
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Paradigms
variables
functions (methods in Java)
Loop statements
Conditional statements
/*There are NOT
classes
Classes and
Objects will
appear in C++
*/
// Different from
// Java
Array
Structures
Pointers
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Outline
1. Getting started with Language C
2. Primitive data types, arrays (and strings)
3. Pointers
4. typedef, enum and struct type
5. Functions calls and parameter passing
1. Getting Started
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
We Need a Compiler
• Microsoft Visual Studio for C/C++
View instructions on Blackboard
• Online Compiler for C/C++
https://www.onlinegdb.com/online_c_compiler
• Dev-C++ 5
http://www.bloodshed.net/devcpp.html
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Reference
• Textbook
Section 2.1 to 2.5 and 2.7
Optional 2.6 (files), 2.7 (recursion). – not included in the exam
• The Programming Language C by Kernighan and Ritchie
https://archive.org/details/TheCProgrammingLanguageFirstEdition
• Learning C programming
https://www.tutorialspoint.com/cprogramming/cprogramming_tutorial.pdf
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Anatomy of a Program in C
• The parts (components) of a C program are called functions.
• There are built-in functions that exist in libraries and user defined
functions that are written by the programmers.
• You can declare variables inside and outside functions. They are
called local variables and global variables, respectively.
• And, a program in C must contain exactly one main( ) function.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Getting Started
1. Comments are identical in Java
2. #include is equivalent to import
3. Libraries are *.h files
4. There are not classes
5. Methods are called functions.
6. Global variables, local
variables, and parameters.
7. Most of the lexical, syntactical,
and semantical rules are as you
know from your Java courses
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12
main()
8. Like Java, main( ) is the entry point
for program execution.
But, (1) C allows void or int as
type for main; (2) C allows empty
parameters or two parameters for
main. This are correct:
• void main () {
}
• void main (int argc, char *argv [ ]) { }
• int main() {return 0;}
• main() {return 0;} // if there is not a
type, C apply as default int
• int main (int argc, char *argv [ ]) { }
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
main()
8. Like Java, main( ) is the entry point
for program execution.
But, (1) C allows void or int as
type for main; (2) C allows empty
parameters or two parameters for
main. This are correct:
• void main () {
}
• void main (int argc, char *argv [ ]) { }
• int main() {return 0;}
• main() {return 0;} // if there is not a
type, C apply as default int
• int main (int argc, char *argv [ ]) { }
public static void main(String [] args) {
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14
Output
printf (control sequence, expressions);
• The control sequence includes a
constant string to be printed, e.g.,
"Result: ", and control symbols to be
used to convert variables from their
numeric values that are stored in the
computer to printing format.
• The expressions is the list of
expressions whose values are to be
printed out. Each expression is
separated by a comma. This is
optional.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15
Output
printf (control sequence, expressions);
• The control sequence includes a
constant string to be printed, e.g.,
"Result: ", and control symbols to be
used to convert variables from their
numeric values that are stored in the
computer to printing format.
• The expressions is the list of
expressions whose values are to be
printed out. Each expression is
separated by a comma.
%d for integer
%f for floating point
%c for character
%s for string of characters
%p address
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16
Output
printf (control sequence, expressions);
• The control sequence includes a
constant string to be printed, e.g.,
"Result: ", and control symbols to be
used to convert variables from their
numeric values that are stored in the
computer to printing format.
• The expressions is the list of
expressions whose values are to be
printed out. Each expression is
separated by a comma. This is
optional.
// Java
int x = 5;
float y = 10.3f;
System.out.println("hello " + x + " bye " + y);
// C
int x = 5;
float y = 10.3;
printf("hello %d bye %f", x, y);
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17
#include <stdio.h>
void main () {
int i, n = 5;
printf("Hi, please enter an integer: ");
// input: scanf (control sequence, &variable1, ... &variablen);
// &variable: address of the variable.
scanf("%d", &i); // input function
if (i > n)
n = n + i;
else
n = n - i;
printf("i = %d, n = %dn", i, n); //output function
}
Input
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18
Control Statements
These that you know, work in the same way
• for, while, do/while
• if/else, switch
But, there are not boolean values. Zero is false and any other number is true.
The following program is correct and print “Hello”
Primitive Data Types
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 20
Primitive Data Types and Modifiers
C defines five basic types
• int - integer, a whole number.
• float - floating point value i.e., a number with a fractional part.
• double - a double-precision floating point value.
• char - a single character.
• void - valueless special purpose type which we will examine closely in later sections.
C++ will add
• bool – boolean values
Primitive types can be modified using one or more of these modifiers
• signed,
• unsigned,
• short,
• long
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 21
Type Guaranteed minimum range ³ bits
char -127 to 127 or 0 to 255 8
signed char -127 to 127 8
unsigned char 0 to 255 8
int -32,768 to 32,767 16
signed int same as int 16
unsigned int 0 to 65,535 16
short int -32,768 to 32,767 16
signed short int same as short int 16
unsigned short int unsigned int 16
long int ±2,147,483,647 32
signed long int same as long int 32
unsigned long int 0 to 4,294,967,295 32
float 6 decimal digits of precision 32
double 10 decimal digits of precision 64
long double 10 decimal digits of precision 64
bool (C++ only) true/false 1 (8)
Basic Data Types and Ranges in C
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 22
Type Guaranteed minimum range ³ bits
char -127 to 127 or 0 to 255 8
signed char -127 to 127 8
unsigned char 0 to 255 8
int -32,768 to 32,767 16
signed int same as int 16
unsigned int 0 to 65,535 16
short int -32,768 to 32,767 16
signed short int same as short int 16
unsigned short int unsigned int 16
long int ±2,147,483,647 32
signed long int same as long int 32
unsigned long int 0 to 4,294,967,295 32
float 6 decimal digits of precision 32
double 10 decimal digits of precision 64
long double 10 decimal digits of precision 64
bool (C++ only) true/false 1 (8)
Basic Data Types and Ranges in C
unsigned int x = 65000;
int x = 32767;
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 23
Type Guaranteed minimum range ³ bits
char -127 to 127 or 0 to 255 8
signed char -127 to 127 8
unsigned char 0 to 255 8
int -32,768 to 32,767 16
signed int same as int 16
unsigned int 0 to 65,535 16
short int -32,768 to 32,767 16
signed short int same as short int 16
unsigned short int unsigned int 16
long int ±2,147,483,647 32
signed long int same as long int 32
unsigned long int 0 to 4,294,967,295 32
float 6 decimal digits of precision 32
double 10 decimal digits of precision 64
long double 10 decimal digits of precision 64
bool (C++ only) true/false 1 (8)
Basic Data Types and Ranges in C
Arrays
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 25
Arrays
Homogeneous collection of data elements (all have the same type). Individual
elements are accessed by an index.
int a[3], i, j, k; // a is not initialized
int ma[2][3] = {{4, 2, 3}, {7, 8, 9}};
int b[4] = {2, 3, 9, 4}; // b is initialized
a[0] = 2; // index starts from 0
a[1] = 3;
a[2] = 9;
i = sizeof a; // # of bytes used by a is 12
j = sizeof b; // # of bytes used by b is 16
k = sizeof ma; // # of bytes used by ma is 24
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 26
Arrays and Strings
A string is an array of characters.
There is not a class String (like in Java) or a primitive data type for strings
In other words, an array of characters can be seen as:
• An array of characters
• An string
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 27
• There are two ways of initializing an array of characters in
declaration:
char s1[] ={'a', 'l', 'p', 'h', 'a'}; // as an array of char
char s2[] ="alpha"; // as a string
• These two initializations have different results in memory:
• where '0' is the null terminator (null character), marking the end of a
string. It is automatically added to the end of a string.
a l p h as1 size = 5 a l p h a 0s2 size = 6
Array and String
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 28
• To have the same result, we could do:
// char s1[] = "alpha";
char s1[]={'a', 'l', 'p', 'h', 'a', '0'};
• If the size of the array is specified and
there is not enough space, the null
character will not be attached to the
string.
char s2[5] = "alpha";
char s3[4] = "alpha";
a l p h a 0s1
a l p h as2 size = 5
a l p hs3 size = 4
Array and String
size = 5
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 29
#include <stdio.h>
main() {
char s1[ ] = "hello";
printf("%s n", s1);
for (int i = 0; i < 5; i++)
printf("%c", s1[ i ]);
printf("n");
return 0;
}
Arrays and Strings
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 30
Array and Strings
#include <stdio.h>
#include <string.h>
main() {
int i;
char s1[ ] = "hello", s2[] = "world";
for (i = 0; i < 5; i++)
printf("%c", s1[ i ]);
printf("n");
printf("s1 = %s, size = %dn", s1, sizeof s1);
for (i = 0; i < 5; i++)
printf("%c", s2[ i ]);
printf("n");
}
h e l l o 0
w o r l d 0
Constants
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 32
Constants
C allows two ways to define a constant:
• const It is equivalent to final in Java. This way is slower since it have
to read memory.
const int i = 5;
i = i + 2; // this line will cause a compilation error
• #define It substitute values for constant definitions in compilation time.
Provide a faster execution.
#define PI 3.14
int x = PI * 5;
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centrejatin batra
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CSyed Mustafa
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...vtunotesbysree
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
T03 b basicioscanf
T03 b basicioscanfT03 b basicioscanf
T03 b basicioscanfteach4uin
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programmingNico Ludwig
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 
JavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By SatyenJavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By SatyenSatyen Pandya
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna universitysangeethajames07
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

What's hot (20)

C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centre
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
T03 b basicioscanf
T03 b basicioscanfT03 b basicioscanf
T03 b basicioscanf
 
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
 
Basics of c++
Basics of c++Basics of c++
Basics of c++
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
C Programming
C ProgrammingC Programming
C Programming
 
What is c
What is cWhat is c
What is c
 
Unit ii ppt
Unit ii pptUnit ii ppt
Unit ii ppt
 
JavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By SatyenJavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By Satyen
 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
 
C++ for beginners
C++ for beginnersC++ for beginners
C++ for beginners
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
C++
C++C++
C++
 
C Programming
C ProgrammingC Programming
C Programming
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

Similar to 201801 CSE240 Lecture 07

C programming language
C programming languageC programming language
C programming languageAbin Rimal
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#ANURAG SINGH
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing TutorialMahira Banu
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfjanakim15
 
introductory concepts
introductory conceptsintroductory concepts
introductory conceptsWalepak Ubi
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubeykiranrajat
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001Ralph Weber
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxMamataAnilgod
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 developmentFisnik Doko
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.pptFatimaZafar68
 
Presentation on 21.11.2022.pptx
Presentation on 21.11.2022.pptxPresentation on 21.11.2022.pptx
Presentation on 21.11.2022.pptxPriyajit Sen
 

Similar to 201801 CSE240 Lecture 07 (20)

201801 CSE240 Lecture 06
201801 CSE240 Lecture 06201801 CSE240 Lecture 06
201801 CSE240 Lecture 06
 
C programming language
C programming languageC programming language
C programming language
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Lec-1c.pdf
Lec-1c.pdfLec-1c.pdf
Lec-1c.pdf
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
C programming
C programmingC programming
C programming
 
C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
C-PPT.pdf
C-PPT.pdfC-PPT.pdf
C-PPT.pdf
 
introductory concepts
introductory conceptsintroductory concepts
introductory concepts
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
Pc module1
Pc module1Pc module1
Pc module1
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
 
201801 CSE240 Lecture 08
201801 CSE240 Lecture 08201801 CSE240 Lecture 08
201801 CSE240 Lecture 08
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
programming week 2.ppt
programming week 2.pptprogramming week 2.ppt
programming week 2.ppt
 
C PROGRAMING.pptx
C PROGRAMING.pptxC PROGRAMING.pptx
C PROGRAMING.pptx
 
Presentation on 21.11.2022.pptx
Presentation on 21.11.2022.pptxPresentation on 21.11.2022.pptx
Presentation on 21.11.2022.pptx
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Recently uploaded (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

201801 CSE240 Lecture 07

  • 1. CSE240 – Introduction to Programming Languages Lecture 07: Programming with C Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2 Imperative Paradigm Fully specified and fully controlled manipulation of named data in a stepwise fashion. which means that: Programs are algorithmic in nature: do this, then that, then repeat this ten times Focuses on how rather than what. variables functions (methods in Java) Loop statements Conditional statements
  • 3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3 Paradigms variables functions (methods in Java) Loop statements Conditional statements
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 Paradigms variables functions (methods in Java) Loop statements Conditional statements // Different from // Java Arrays Structs Pointers
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Paradigms variables functions (methods in Java) Loop statements Conditional statements /*There are NOT classes Classes and Objects will appear in C++ */ // Different from // Java Array Structures Pointers
  • 6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6 Outline 1. Getting started with Language C 2. Primitive data types, arrays (and strings) 3. Pointers 4. typedef, enum and struct type 5. Functions calls and parameter passing
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 We Need a Compiler • Microsoft Visual Studio for C/C++ View instructions on Blackboard • Online Compiler for C/C++ https://www.onlinegdb.com/online_c_compiler • Dev-C++ 5 http://www.bloodshed.net/devcpp.html
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 Reference • Textbook Section 2.1 to 2.5 and 2.7 Optional 2.6 (files), 2.7 (recursion). – not included in the exam • The Programming Language C by Kernighan and Ritchie https://archive.org/details/TheCProgrammingLanguageFirstEdition • Learning C programming https://www.tutorialspoint.com/cprogramming/cprogramming_tutorial.pdf
  • 10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10 Anatomy of a Program in C • The parts (components) of a C program are called functions. • There are built-in functions that exist in libraries and user defined functions that are written by the programmers. • You can declare variables inside and outside functions. They are called local variables and global variables, respectively. • And, a program in C must contain exactly one main( ) function.
  • 11. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11 Getting Started 1. Comments are identical in Java 2. #include is equivalent to import 3. Libraries are *.h files 4. There are not classes 5. Methods are called functions. 6. Global variables, local variables, and parameters. 7. Most of the lexical, syntactical, and semantical rules are as you know from your Java courses
  • 12. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12 main() 8. Like Java, main( ) is the entry point for program execution. But, (1) C allows void or int as type for main; (2) C allows empty parameters or two parameters for main. This are correct: • void main () { } • void main (int argc, char *argv [ ]) { } • int main() {return 0;} • main() {return 0;} // if there is not a type, C apply as default int • int main (int argc, char *argv [ ]) { }
  • 13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13 main() 8. Like Java, main( ) is the entry point for program execution. But, (1) C allows void or int as type for main; (2) C allows empty parameters or two parameters for main. This are correct: • void main () { } • void main (int argc, char *argv [ ]) { } • int main() {return 0;} • main() {return 0;} // if there is not a type, C apply as default int • int main (int argc, char *argv [ ]) { } public static void main(String [] args) { }
  • 14. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14 Output printf (control sequence, expressions); • The control sequence includes a constant string to be printed, e.g., "Result: ", and control symbols to be used to convert variables from their numeric values that are stored in the computer to printing format. • The expressions is the list of expressions whose values are to be printed out. Each expression is separated by a comma. This is optional.
  • 15. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15 Output printf (control sequence, expressions); • The control sequence includes a constant string to be printed, e.g., "Result: ", and control symbols to be used to convert variables from their numeric values that are stored in the computer to printing format. • The expressions is the list of expressions whose values are to be printed out. Each expression is separated by a comma. %d for integer %f for floating point %c for character %s for string of characters %p address
  • 16. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16 Output printf (control sequence, expressions); • The control sequence includes a constant string to be printed, e.g., "Result: ", and control symbols to be used to convert variables from their numeric values that are stored in the computer to printing format. • The expressions is the list of expressions whose values are to be printed out. Each expression is separated by a comma. This is optional. // Java int x = 5; float y = 10.3f; System.out.println("hello " + x + " bye " + y); // C int x = 5; float y = 10.3; printf("hello %d bye %f", x, y);
  • 17. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17 #include <stdio.h> void main () { int i, n = 5; printf("Hi, please enter an integer: "); // input: scanf (control sequence, &variable1, ... &variablen); // &variable: address of the variable. scanf("%d", &i); // input function if (i > n) n = n + i; else n = n - i; printf("i = %d, n = %dn", i, n); //output function } Input
  • 18. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18 Control Statements These that you know, work in the same way • for, while, do/while • if/else, switch But, there are not boolean values. Zero is false and any other number is true. The following program is correct and print “Hello”
  • 20. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 20 Primitive Data Types and Modifiers C defines five basic types • int - integer, a whole number. • float - floating point value i.e., a number with a fractional part. • double - a double-precision floating point value. • char - a single character. • void - valueless special purpose type which we will examine closely in later sections. C++ will add • bool – boolean values Primitive types can be modified using one or more of these modifiers • signed, • unsigned, • short, • long
  • 21. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 21 Type Guaranteed minimum range ³ bits char -127 to 127 or 0 to 255 8 signed char -127 to 127 8 unsigned char 0 to 255 8 int -32,768 to 32,767 16 signed int same as int 16 unsigned int 0 to 65,535 16 short int -32,768 to 32,767 16 signed short int same as short int 16 unsigned short int unsigned int 16 long int ±2,147,483,647 32 signed long int same as long int 32 unsigned long int 0 to 4,294,967,295 32 float 6 decimal digits of precision 32 double 10 decimal digits of precision 64 long double 10 decimal digits of precision 64 bool (C++ only) true/false 1 (8) Basic Data Types and Ranges in C
  • 22. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 22 Type Guaranteed minimum range ³ bits char -127 to 127 or 0 to 255 8 signed char -127 to 127 8 unsigned char 0 to 255 8 int -32,768 to 32,767 16 signed int same as int 16 unsigned int 0 to 65,535 16 short int -32,768 to 32,767 16 signed short int same as short int 16 unsigned short int unsigned int 16 long int ±2,147,483,647 32 signed long int same as long int 32 unsigned long int 0 to 4,294,967,295 32 float 6 decimal digits of precision 32 double 10 decimal digits of precision 64 long double 10 decimal digits of precision 64 bool (C++ only) true/false 1 (8) Basic Data Types and Ranges in C unsigned int x = 65000; int x = 32767;
  • 23. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 23 Type Guaranteed minimum range ³ bits char -127 to 127 or 0 to 255 8 signed char -127 to 127 8 unsigned char 0 to 255 8 int -32,768 to 32,767 16 signed int same as int 16 unsigned int 0 to 65,535 16 short int -32,768 to 32,767 16 signed short int same as short int 16 unsigned short int unsigned int 16 long int ±2,147,483,647 32 signed long int same as long int 32 unsigned long int 0 to 4,294,967,295 32 float 6 decimal digits of precision 32 double 10 decimal digits of precision 64 long double 10 decimal digits of precision 64 bool (C++ only) true/false 1 (8) Basic Data Types and Ranges in C
  • 25. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 25 Arrays Homogeneous collection of data elements (all have the same type). Individual elements are accessed by an index. int a[3], i, j, k; // a is not initialized int ma[2][3] = {{4, 2, 3}, {7, 8, 9}}; int b[4] = {2, 3, 9, 4}; // b is initialized a[0] = 2; // index starts from 0 a[1] = 3; a[2] = 9; i = sizeof a; // # of bytes used by a is 12 j = sizeof b; // # of bytes used by b is 16 k = sizeof ma; // # of bytes used by ma is 24
  • 26. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 26 Arrays and Strings A string is an array of characters. There is not a class String (like in Java) or a primitive data type for strings In other words, an array of characters can be seen as: • An array of characters • An string
  • 27. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 27 • There are two ways of initializing an array of characters in declaration: char s1[] ={'a', 'l', 'p', 'h', 'a'}; // as an array of char char s2[] ="alpha"; // as a string • These two initializations have different results in memory: • where '0' is the null terminator (null character), marking the end of a string. It is automatically added to the end of a string. a l p h as1 size = 5 a l p h a 0s2 size = 6 Array and String
  • 28. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 28 • To have the same result, we could do: // char s1[] = "alpha"; char s1[]={'a', 'l', 'p', 'h', 'a', '0'}; • If the size of the array is specified and there is not enough space, the null character will not be attached to the string. char s2[5] = "alpha"; char s3[4] = "alpha"; a l p h a 0s1 a l p h as2 size = 5 a l p hs3 size = 4 Array and String size = 5
  • 29. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 29 #include <stdio.h> main() { char s1[ ] = "hello"; printf("%s n", s1); for (int i = 0; i < 5; i++) printf("%c", s1[ i ]); printf("n"); return 0; } Arrays and Strings
  • 30. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 30 Array and Strings #include <stdio.h> #include <string.h> main() { int i; char s1[ ] = "hello", s2[] = "world"; for (i = 0; i < 5; i++) printf("%c", s1[ i ]); printf("n"); printf("s1 = %s, size = %dn", s1, sizeof s1); for (i = 0; i < 5; i++) printf("%c", s2[ i ]); printf("n"); } h e l l o 0 w o r l d 0
  • 32. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 32 Constants C allows two ways to define a constant: • const It is equivalent to final in Java. This way is slower since it have to read memory. const int i = 5; i = i + 2; // this line will cause a compilation error • #define It substitute values for constant definitions in compilation time. Provide a faster execution. #define PI 3.14 int x = PI * 5;
  • 33. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.