Professional Documents
Culture Documents
Lectuskje - Constant and Data
Lectuskje - Constant and Data
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
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.
Department of C programming
Mechanical Engineering
Keywords
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
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
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
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
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.
• 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
Department of C programming
Mechanical Engineering
sizeof operator
• It returns operand’s memory size
#include <stdio.h>
int main(void)
{
int val = 10;
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;
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;
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
Department of C programming
Mechanical Engineering
ASCII code
• Range of ASCII code: 0 – 127
• Use quotation marks ('A’) to
express the mapped number
(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;
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.
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값을 출력한다.
출력 결과를 제출하고, 왜 이런 결과가 나왔는지에 대해 설명 하시오
1. 실행결과 창 캡쳐
2. Source code
3. 간단한 code 해설
Department of C programming
Mechanical Engineering