INS2020, Lecture 3
Welcome to Programming 1!
Dr. Pham Ngoc Thanh
Faculty of Applied Sciences
International School – Vietnam National University, Hanoi
This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Marty Stepp, Cynthia Lee, Chris Gregg, Jerry Cain, Lisa Yan and others.
1
Identifiers
• Identifiers (i.e., variable names, function names, etc) are made up of letters and
digits, and underscore (_).
• Identifiers are case-sensitive. E.g: int m; int M declares two different variables.
• The first character of an identifier must be a letter or underscore (_)
• Keywords (e.g., for, int, while etc.) cannot be used as identifiers.
• Avoid redefining identifiers used in the C standard library (e.g., standard function
names, etc.).
• Use lowercase for variable names & uppercase for symbolic constants.
• Example (correct/incorrect):
int money$owed; /*incorrect: cannot contain $*/
int total_count; /*correct*/
int _2ndscore; /*incorrect: must start with a letter*/
int long; /*incorrect: cannot use keyword*/
2
Data Types
• The Data type is a set of value with predefined characteristics.
• Data types are used to declare variable, constants, arrays, pointers, and functions.
3
Primary data types
• The primary data types are already defined in the C language (Built-In data types)
int a = 10; /*stores integer values*/
char b = 'S’; /*stores a single character*/
float c = 2.88; /*Stores decimal values with single-precision*/
double d = 28.888; /*stores decimal values with double-precision*/
4
Primary data types
• Primary data types
5
Size of data types
#include <stdio.h>
int main (void) // void = “no type”, “no value” or “no parameters”
/* Print the size of various types in “number-of-chars” */
{
printf("char\tshort\tint\tlong\tfloat\tdouble\n");
printf("%3d\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\n",
sizeof(char), sizeof(short), sizeof(int),
sizeof(long), sizeof(float), sizeof(double));
}
• The size of a type (memory size) in number of characters
(number of bytes) can be found using the sizeof operator.
• %3d means prints a integer at least 3 character wide
6
Range of Data Types
#include <stdio.h>
#include <limits.h> /* integer specifications */
#include <float.h> /* floating-point specifications */
/* Look at range limits of certain types */
int main (void)
{
printf("Integer range:\t%d\t%d\n", INT_MIN, INT_MAX);
printf("Long range:\t%ld\t%ld\n", LONG_MIN, LONG_MAX);
printf("Float range:\t%e\t%e\n", FLT_MIN, FLT_MAX);
printf("Double range:\t%e\t%e\n", DBL_MIN, DBL_MAX);
printf("Long double range:\t%e\t%e\n", LDBL_MIN, LDBL_MAX);
printf("Float-Double epsilon:\t%e\t%e\n", FLT_EPSILON, DBL_EPSILON);
}
• Use %e format to print numbers in scientific notation.
24327 = 2.4327 x 104 = 2.4327e+4
• The parameters such as INT_MIN, FLT_MIN can be found
in standard headers limits.h and float.h
7
Type Specifier
#include <stdio.h>
int main() {
// datatypes
int a = 10;
char b = 'S';
float c = 2.88;
double d = 28.888;
printf("Integer is %d\n",a);
printf("Character is %c with ASCII is %d\n",b,b);
printf("Float is: %f\n",c);
printf("Double Float is: %lf\n",d);
return 0;
} • The char datatype refers to character values, enclosed in single
quotes, with a range of -127 to 127.
• The characters have a value equivalent to its integer code (ASCII)
8
Type Modifier
#include <stdio.h>
#include <limits.h>
int main() {
// datatypes
short a1 = -100; /* =short int a1; 2 byte */
long a2 = -100; /* =long int a1; 4 byte */
signed int a3 = -100; /* = int a1; */
unsigned a4 = -100; /* = int a1; 4294967196*/
double b1 = 28.888; //memory size is 8 byte
long double b2 = 28.888; //memory size is 16 byte
printf("Short integer: %d, memory size: %d byte\n",a1, sizeof(short int));
printf("Long integer: %d, memory size: %d byte\n",a2, sizeof(long int));
printf("Signed integer: %d, range: %d\t%d\n",a3,INT_MIN,INT_MAX);
printf("Unsigned integer: %u, range: %u\t%u\n",a2,0.000000,UINT_MAX);
printf("Double: %lf, memory size: %d byte\n",b1, sizeof(double));
printf("Long double: %Lf, memory size: %d byte\n",b2, sizeof(double long));
} 9
Type Modifier
#include <stdio.h> • The signed or unsigned may be applied to char or any integer.
#include <limits.h> • A signed type may represent negative values; the most-significant-bit (MSB) of the
number is its sign-bit. An unsigned type is always non-negative, and the MSB is
int main() {
part of the numerical value. Integer types are signed by default.[convert]
// datatypes
short a1 = -100; /*=short int a1; 2 byte*/
long a2 = -100; /* =long int a1; 4 byte */
signed a3 = -100; /* = signed int a3; 1111111110011100 */
unsigned a4 = -100; /* = unsigned int a4; a4>=0; 4294967196*/
double b1 = 28.888; //memory size is 8 byte
long double b2 = 28.888; //memory size is 16 byte
printf("Short integer: %d, memory size: %d byte\n",a1, sizeof(short int));
printf("Long integer: %d, memory size: %d byte\n",a2, sizeof(long int));
printf("Signed integer: %d, range: %d\t%d\n",a3,INT_MIN,INT_MAX);
printf("Unsigned integer: %u, range: %u\t%u\n",a2,0.000000,UINT_MAX);
printf("Double: %lf, memory size: %d byte\n",b1, sizeof(double));
printf("Long double: %Lf, memory size: %d byte\n",b2, sizeof(double long));
} 10
Type Modifier
#include <stdio.h>
#include <limits.h>
int main() {
// datatypes
short a1 = -100; /* =short int a1; 2 byte */
long a2 = -100; /* =long int a1; 4 byte */
signed a3 = -100; /* = unsigned int a3; 1111111110011100 */
unsigned a4 = -100; /* = unsigned int a4; a4>=0; 4294967196*/
double b1 = 28.888; //memory size is 8 byte
long double b2 = 28.888; //memory size is 16 byte
printf("Short integer: %d, memory size: %d byte\n",a1, sizeof(short int));
printf("Long integer: %d, memory size: %d byte\n",a2, sizeof(long int));
printf("Signed integer: %d, range: %d\t%d\n",a3,INT_MIN,INT_MAX); //-2147483648 to 2147483647.
printf("Unsigned integer: %u, range: %u\t%u\n",a2,0.000000,UINT_MAX); //0 to 4294967295
printf("Double: %lf, memory size: %d byte\n",b1, sizeof(double));
printf("Long double: %Lf, memory size: %d byte\n",b2, sizeof(double long));
}
11
Type Qualifier
#include <stdio.h>
int main() {
// datatypes
int Change = 5;
Change = 6; ///* Can change*/
const int DoesNotChange = 5;
/* DoesNotChange = 6; /* Error: will not compile */
const char DoesNotChangeChar = 'A';
printf("Change: %d\n",Change);
printf("DoesNotChange: %d\n",DoesNotChange);
printf("DoesNotChangeChar: %d\n",DoesNotChangeChar);
}
• The qualifier const means that the variable to
which it refers cannot be changed.
12
Void Type
#include <stdio.h>
void printCompanyInfo() /* A void function does not return a value */
{
printf("Company **************\n");
printf("address *********************\n");
printf("Phone ****************** \n");
printf("Email ****************** \n");
}
int sumFirst10Numbers(void) /*does not accept parameters*/
{
int sum = 0; int i;
for(i = 1; i <= 10; ++i) sum += i;
return sum;
} • There is a void type, which specifies a “no value” type.
int main() • It is used as an argument for functions with no arguments
{ and a return type for functions that return no value.
printCompanyInfo();
printf("sumFirst10Numbers: %d\n",sumFirst10Numbers());
return 0;
} 13
Constants
• Constants are fixed values that do not change during a program's execution.
• Numeric Constants
• Integer Constant
• Decimal integer constant: 0,..9,10,11,12…
• Octal integer constant: 0,1..7,10,..17,20
• Hexadecimal integer constant: 0,1..9,A,B..F
• Real Constant
• Character Constants
• Single character constant
• String character constant
• Backslash Character constants
• Symbolic constants
14
Decimal constant
#include <stdio.h>
int main()
{ Valid: 0 32767 -9999
int a = 10; /*Decimal constants*/ Invalid:
int b = 012;/*Octal constants*/ 12,245 - Illegal character (,)
int c = 0xA;/*Hexadecimal constants*/ 10 20 - Illegal character (blank space)
/* %d specify the number as decimal integer
%o specify the number as octal integer
%x specify the number as hexadecimal integer
*/
printf(" In decimal = %d, In octal = %o, In Hexadecimal = %x ", a,b,c);
return 0;
} • Combinations of digits from 0 to 9 may be used, preceded by a – or + sign.
• The first digit must be other than 0.
• Spaces, commas, and non-digit characters are not permitted between digits.
• Type specifier for the decimal numbers is %d.
15
Octal Constant (base 8)
#include <stdio.h>
int main()
{ Valid: 037 0 0435
int a = 10; /*Decimal constants*/ Invalid:
int b = 012;/*Octal constants*/ 0786 - Illegal digit 8
int c = 0xA;/*Hexadecimal constants*/ 123 - Does not begin with zero
/* %d specify the number as decimal integer 01.2 - Illegal character (.)
%o specify the number as octal integer
%x specify the number as hexadecimal integer
*/
printf(" In decimal = %d, In octal = %o, In Hexadecimal = %x ", a,b,c);
return 0;
}
• This constant is a combination of any digits from 0 to 7.
• The first digit must be 0.
• Type specifier for an octal number is %o.
16
Hexadecimal constant
#include <stdio.h>
int main()
{ Valid: 0x 0XDF1 0x7F
int a = 10; /*Decimal constants*/ Invalid:
int b = 012;/*Octal constants*/ 0xefg - Illegal character g
int c = 0xA;/*Hexadecimal constants*/ 123 - Does not begin with 0x
/* %d specify the number as decimal integer
%o specify the number as octal integer
%x specify the number as hexadecimal integer
*/
printf(" In decimal = %d, In octal = %o, In Hexadecimal = %x ", a,b,c);
return 0;
}
• It consists of any combinations of digits from 0-9 & a-f (uppercase or lowercase).
• From a-f (or A-F) correspond to the decimal quantities 10-15.
• This constant must begin with either 0x or 0X.
• Type specifier for hexadecimal is %x.
17
Floating-point constants
Valid: -0.1 +123.456 .2 2.
#include <stdio.h> Invalid:
int main()
1 - a decimal point is missing
{
float a = 10.56f; // or 10.56F 12,3 - Illegal character (,)
double b = 10.56; // capable of storing more precision value
long double c = 10.56L; // capable of storing more and more accurate precision
/*
%f is for float
%lf is for double
%Lf is for long double,but MINGW is broken regarding long double.
*/
printf(" In float= %f, In double = %lf, In long double = %Lf ", a,b,c);
return 0;
}
• A decimal point (.) in a numeric value represents a floating-point constant.
• A floating-point constant is represented by suffixing it with ‘f’ or ‘F’.
• It will be considered a double constant in the absence of a suffix with 'f' or 'F’.
• If the value exceeds the range of 'float' or 'double', we need to suffix it with l or
L to represent it as ‘long double’. 18
Floating-point constants
0.000342 = 3.42e-4= 3.42E-4
#include <stdio.h>
Mantissa = 3.42
int main()
{
Exponent = -4
float a = 10.56f; // or 10.56F
double b = 10.56; // capable of storing more precision value
long double c = 10.56L; // capable of storing more and more accurate precision
/*
%f is for float
%lf is for double
%Lf is for long double,but MINGW is broken regarding long double.
*/
printf(" In float= %.3e, In double = %.2e, In long double = %.3e ", a,b,c);
return 0;
• In scientific notation: the real constant is represented in two parts
}
• (Mantissa)±E(exponent) = Mantissa × 10±exponent
• A real constant is usually expressed in scientific notation if the value is either
too small or too large.
19
Character constants
#include <stdio.h> Valid: ‘m’ ‘=’ ‘A’.
int main()
Invalid:
{
ۥ123ۥ - Length should be 1
“123” - Enclosed in single quotes
char variable1 = 'a'; //character 'a’.
char variable2 = 97; //its ASCII value = 97
printf("Variable initialized with character a= %d \n", variable1);
printf("Variable initialized with its ASCII value a= %c", variable2);
return 0;
}
• A single character enclosed in a single quote is said to be a character constant.
• Each character in C will have an ASCII representation.
• Type specifier for character is %c.
20
String Constants
Valid: "W" "100" "24, Kaja Street"
#include <stdio.h> Invalid:
int main() "W - closing double quotes missing
{ Raja" - beginning double quotes missing
char *string= "Hello World\0";
/* %s is the type specifier used to specify the string */
printf ("The string is %s ", string);
printf ("Size of A %d ", sizeof("A"));
return 0;
}
• A string constant contains one or more than one character in it (Including the
NULL i.e. character ' \0' ).
• In string constant, characters are enclosed within double quotes.
• "A" consists of character ‘A’ and \0 (NULL) → sizeof("A")=2
21
Backslash constants
#include <stdio.h>
int main()
{
printf ("Hello world!\n\n");
printf ("Thank you\v\v");
printf ("\"See you again\"");
return 0;
}
22
How to use Constant in C
#include <stdio.h>
void main()
{
const int height = 100; /*int constant*/
const float number = 3.14; /*Real constant*/
const char letter = 'A'; /*char constant*/ 1
const char letter_sequence[10] = "ABC"; /*string constant*/
const char backslash_char = '\?'; /*special char cnst*/
printf("value of height :%d \n", height );
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );
printf("value of letter_sequence : %s \n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char);
}
• Using const keyword: const data_type var_name = value;
• Changing constants after defining in C program will result in an error.
23
How to use Constant in C
#include <stdio.h>
#define height 100
#define number 3.14
#define letter 'A' 2
#define letter_sequence "ABC"
#define backslash_char '\?'
void main ()
{
printf ("value of height : %d \n", height);
printf ("value of number : %f \n", number);
printf ("value of letter : %c \n", letter);
printf ("value of letter_sequence : %s \n", letter_sequence);
printf ("value of backslash_char : %c \n", backslash_char);
}
• Using #define preprocessor: #define identifier value
• Symbolic Constants.
24