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.