Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Chapter 3

Constant and Data Type

Department of C programming
Mechanical Engineering
Character Set
• The characters in C are grouped into the following categories:
– Letters
– Digits
– Special characters
– White spaces

Department of C programming
Mechanical Engineering
Character Set

Table 3.1 C Character Set

Department of C programming
Mechanical Engineering
C token
• In a passage of text, individual words and punctuation marks are
called tokens.
• Similarly, in a C program the smallest individual units are known as
C tokens.

Fig. 3.1 C tokens and examples

Department of C programming
Mechanical Engineering
Keywords

Table 3.3 ANSI C Keyword

Department of C programming
Mechanical Engineering
Constants

• Integer Constants
- An integer constant refers to a sequence of digits.

Department of C programming
Mechanical Engineering
Constants
• Real Constants
– Examples of real (or floating point) constants are:
• 0.083, -0.75, 435.36, +247.0, -.71, 215.
– A real number may also be expressed in exponential (or scientific)
notation.
• 0.65e4, 12e-2, 1.5e+5, 3.18E3, -1.2E-1
• Single Character Constants
– Example of character constants is as follows:
• ‘5‘, ‘X’, ‘;’, ‘ ‘
• String Constants
– Examples are:
• “Hello!”, “1987”, “WELL DONE“, “?...!”, “5+3”, “X”

Department of C programming
Mechanical Engineering
Constants
• Backslash Character Constants

Table 3.5 Backslash Character Constants

Department of C programming
Mechanical Engineering
Variables
• A variable is a data name that may be used to store a data value.
• A variable name can be chosen by the programmer in a meaningful
way so as to reflect its function or nature in the program.
• Some examples of such names are:
– Average, height, Total, Counter_1, class_strength
• Variable names may consist of letters, digits, and the underscore(_)
character, subject to the flowing conditions:
– They must begin with a letter. Some systems permit underscore as the
first character.
– ANSI standard recognizes a length of 31 characters.
– Uppercase and lowercase are significant. That is, the variable Total is
not the same as total or TOTAL.
– It should not be a keyword.
– White space is not allowed.

Department of C programming
Mechanical Engineering
Variables

Table 3.6 Examples of Variable Names

Department of C programming
Mechanical Engineering
Data type
• What is Data type?
- Attribute of data which tells the compiler or interpreter
how the programmer intends to use the data

• Primary types

Fig. 3.4 Primary data types in C

Department of C programming
Mechanical Engineering
Data type
• Integer Types
– Int: Integers are whole numbers with a range of values supported by a
particular machine. (16bit/32bit machines → 16bit/32bit word size)
– For 16bit machine,
– (signed) int : -32,768 ~ 32,767
– unsigned int: 0 ~ 65,535

Fig. 3.5 Integer types

Department of C programming
Mechanical Engineering
Data type
• Example of Arduino IDE

(https://www.arduino.cc/reference/en/language/variables/data-types/int/)

Department of C programming
Mechanical Engineering
Data type
• Floating Point Types
– Float: Floating point (or real) numbers are stored in 32 bits (on all 16
bit and 32 bit machines), with 6 digits of precision.

• Void Types Fig. 3.6 Floating-point types

– The void type has no value.


– This is usually used to specify the type of functions.

• Character Types
– A single character can be as a character(char) type data.
– 8 bit: -128 ~ 127

Department of C programming
Mechanical Engineering
Data type
Size and Range of Data type in 32-bit machine
Allocated
Data type memory Range of data size
size
Character char -128 (-27) ~ 127 (27-1)
Type
1 Byte
unsigned char 0 ~ 255 (28-1)
short int -32768 (-215) ~ +32767 (215-1)
2 Byte
unsigned short int 0 ~ 65535 (216-1)
Integral int -2147483648 (-231) ~ 2147483647 (231-1)
Type 4 Byte
Integer unsigned int 0 ~ 4294967295 (232-1)
Type
long int -2147483648 (-231) ~ 2147483647 (231-1)
4 Byte
unsinged long int 0 ~ +4294967295 (232-1)
long long int –9,223,372,036,854,775,808 (-263) ~ 9,223,372,036,854,775,807 (263-1)
8 Byte 0 ~ 18,446,744,073,709,551,615 (264-1)
unsigned long long int

float 4 Byte 3.4*10-37 ~ 3.4*10+38

double 8 Byte 1.7*10-307 ~ 1.7*10+308


Floating point Type
Equal or
larger
long double Depending on compliers
than
8 Byte

Department of C programming
Mechanical Engineering
sizeof operator
• It returns operand’s memory size
#include <stdio.h>

int main(void)
{
int val = 10;

printf(" %d\n", sizeof val);// Memory size of the variable 'val'


printf(" %d\n", sizeof(int));// Memory size of the data type 'int'

return 0;
}

Department of C programming
Mechanical Engineering
sizeof operator
#include <stdio.h>

int main(void)
{
char ch = 9;
short st = 1;
int val = 10;
long int val_long = 100;
float real = 3.141592653589793238462643383;
double real2 = 3.141592653589793238462643383;
long double real_long = 3.141592653589793238462643383;

printf("Datasize of (char): %d\n", sizeof(ch));


printf("Datasize of (short): %d\n", sizeof(st));
printf("Datasize of (int): %d\n", sizeof(val));
printf("Datasize of (long int): %d\n", sizeof(val_long));
printf("Datasize of (float): %d\n", sizeof(real));
printf("Datasize of (double): %d\n", sizeof(real2));
printf("Datasize of (long double): %d\n\n", sizeof(real_long));

printf("float: %.25f\n", real);


printf("double: %.25lf\n", real2);
printf("long double: %.25lf\n", real_long);

return 0;
}

Department of C programming
Mechanical Engineering
sizeof operator
#include <stdio.h>

int main(void)
{
char num1 = 1, num2 = 2, result1 = 0;
short num3 = 300, num4 = 400, result2 = 0;

printf("size of num1 & num2: %d, %d \n", sizeof(num1), sizeof(num2));


printf("size of num3 & num4: %d, %d \n", sizeof(num3), sizeof(num4));
printf("size of num3 & num4: %d \n", sizeof(num1+num2));
printf("size of num3 & num4: %d \n", sizeof(num3+num4));

result1 = num1 + num2;


result2 = num3 + num4;

printf("size of result1 & result2: %d, %d \n", sizeof(result1),


sizeof(result2));

return 0;
}

Department of C programming
Mechanical Engineering
ASCII code
• ASCII code for Single Character Constants
– Defined by the American Standards Institute (ANSI)
– Standard for expressing characters through a computer
• Computer only uses numbers, not characters

– It maps characters and numbers


• ‘A’ → 65, ‘B’ → 66

Department of C programming
Mechanical Engineering
ASCII code
• Range of ASCII code: 0 – 127
• Use quotation marks ('A’) to
express the mapped number

First, refer to ASCII code table


‘A’ is mapped to 65

First, refer to ASCII code table


‘A’ is mapped to 65

(https://theasciicode.com.ar/)

Department of C programming
Mechanical Engineering
ASCII code
#include <stdio.h>

int main(void)
{
char ch1 = 'A', ch2 = 65;
int ch3 = 'Z', ch4 = 90;

printf("%c %d \n", ch1, ch1);


printf("%c %d \n", ch2, ch2);
printf("%c %d \n", ch3, ch3);
printf("%c %d \n", ch4, ch4);

return 0;
}

Department of C programming
Mechanical Engineering
Type Conversions in Expressions
• Implicit Type Conversion
– C permits mixing of constants and variables of different types in an
expression.
– If the operands are of different types, the ‘lower’ type is automatically
converted to the ‘higher’ type before the operation proceeds.

• Explicit Type Conversion


– It is a type conversion which is explicitly defined within a program
(instead of being done by a compiler for implicit type conversion).
– It is defined by the user in the program.

Department of C programming
Mechanical Engineering
Type Conversions in Expressions
• Implicit Type Conversion – Case 1
– Assignment operator

int main(void)
{
int n=5.25;
double d=3;
char c=129;

Department of C programming
Mechanical Engineering
Type Conversions in Expressions
• Implicit Type Conversion – Case 2
– Integral promotion

int main(void)
{
char c1=10, c2=20;
char c3=c1+c2;
.....

Department of C programming
Mechanical Engineering
Type Conversions in Expressions
• Implicit Type Conversion – Case 3
– Inconsistency of operands’ datatype

int main(void)
{
double e1 = 5.5 + 7; // double + int
double e2 = 3.14f+5.25; // float + double
.....

Department of C programming
Mechanical Engineering
Type Conversions in Expressions
• Implicit Type Conversion – Case 3

Department of C programming
Mechanical Engineering
Type Conversions in Expressions
• Explicit Type Conversion – Case 1
#include <stdio.h>

int main(void)
{
int num1 = 3, num2 = 4;
double divResult;
divResult = num1 / num2;
printf("result of division: %f \n", divResult);

return 0;
}

#include <stdio.h>

int main(void)
{
int num1 = 3, num2 = 4;
double divResult;
divResult = (double)num1 / num2;
printf("result of division: %f \n", divResult);

return 0;
}

Department of C programming
Mechanical Engineering
Homework #2
1. 다음을 실행하는 프로그램 작성
(a) x와 y를 int 형 변수, z를 short int 형 변수로 선언.
(b) x에 500000을, y에 600000을 대입. (x=500000, y=600000)
(c) x와 y의 합을 z에 대입. (z=x+y)
(d) x, y, z값을 출력한다.
출력 결과를 제출하고, 왜 이런 결과가 나왔는지에 대해 설명 하시오

2. 정수 125에 해당하는 ASCII 코드 문자를 출력하는 프로그램을 작성

3. 일(day) 수를 변수에 대입하고, 이를 년(year), 주(week)로 나타내는 프로그램을 작성


(나머지는 버림)
• Ex) 1000일은 각각 2년, 142주

4. 자동차로 여행한 거리와 소모된 연료 값을 임의로 선택하여 자동차의 연비를 계산하는


프로그램을 작성

1. 실행결과 창 캡쳐
2. Source code
3. 간단한 code 해설

Department of C programming
Mechanical Engineering

You might also like