Sie sind auf Seite 1von 32

Programming Language 1

CSC 1102

Akinul Islam Jony


Faculty Member
Department of Computer Science, FSIT
American International University - Bangladesh (AIUB)

akinul@aiub.edu
C Basic

>> C Character Set


>> ASCII Character Set
>> Tokens in C
>> Identifiers
>> Keywords
>> Data Types
>> Variables
>> Constants
>> Literals
>> The % Format Specifiers
>> Flowchart to C Code
>> Pseudocode to C Code
C Character Set
>> The character set is the fundamental raw material of any language and they are
used to represent information.

>> A character denotes any alphabet, digit or special symbol used to represent
information.

Programming Language 1 3
ASCII Character Set
>> The other form of alphabet which the program executes, also known as Execution
Character set.

>> Computers can manage internally only 0s (zeros) and 1s (ones) and can express any
numerical value to the sequence of 0s (zeros) and 1s (ones) by its binary translation.

>> Therefore, computers use ASCII tables, which are tables or lists that contain all the
letters plus some additional characters. In these tables each character is always
represented by the same order number.

>> For example, the ASCII code for the capital letter "A" is always represented by the
order number 65, which is easily representable using 0s and 1s in binary: 65 expressed
as a binary number is 1000001.

>> The standard ASCII table defines 128 character codes (from 0 to 127), of which, the
first 32 are control codes (non-printable), and the remaining 96 character codes are
representable characters.

Programming Language 1 4
ASCII Character Set

dec oct hex ch


21 25 15 NAK (negative acknowledge) 42 52 2a *
0 0 00 NUL (null)
22 26 16 SYN (synchronous idle) 43 53 2b +
1 1 01 SOH (start of header)
23 27 17 ETB (end of transmission block) 44 54 2c ,
2 2 02 STX (start of text)
24 30 18 CAN (cancel) 45 55 2d -
3 3 03 ETX (end of text)
25 31 19 EM (end of medium) 46 56 2e .
4 4 04 EOT (end of transmission)
47 57 2f /
26 32 1a SUB (substitute)
5 5 05 ENQ (enquiry) 48 60 30 0
27 33 1b ESC (escape)
6 6 06 ACK (acknowledge) 49 61 31 1
28 34 1c FS (file separator)
7 7 07 BEL (bell) 50 62 32 2
8 10 08 BS (backspace) 29 35 1d GS (group separator)
51 63 33 3
9 11 09 HT (horizontal tab) 30 36 1e RS (record separator)
52 64 34 4
10 12 0a LF (line feed - new line) 31 37 1f US (unit separator)
53 65 35 5
32 40 20 (space)
11 13 0b VT (vertical tab) 54 66 36 6
33 41 21 !
12 14 0c FF (form feed - new page) 55 67 37 7
13 15 0d CR (carriage return) 34 42 22 "
56 70 38 8
14 16 0e SO (shift out) 35 43 23 #
57 71 39 9
15 17 0f SI (shift in) 36 44 24 $
58 72 3a :
16 20 10 DLE (data link escape) 37 45 25 % 59 73 3b ;
17 21 11 DC1 (device control 1) 38 46 26 & 60 74 3c <
18 22 12 DC2 (device control 2) 39 47 27 ' 61 75 3d =
19 23 13 DC3 (device control 3) 40 50 28 ( 62 76 3e >
20 24 14 DC4 (device control 4) 41 51 29 ) 63 77 3f ?

Programming Language 1 5
ASCII Character Set
dec oct hex ch
83 123 53 S 102 146 66 F
64 100 40 @ 122 172 7a z
84 124 54 T 103 147 67 G
65 101 41 A 123 173 7b {
85 125 55 U 104 150 68 H
66 102 42 B 124 174 7c |
86 126 56 V 105 151 69 I
67 103 43 C 125 175 7d }
87 127 57 W 106 152 6a J
68 104 44 D 126 176 7e ~
107 153 6b K
69 105 45 E 88 130 58 X
108 154 6c L 127 177 7f DEL (delete)
70 106 46 F 89 131 59 Y
109 155 6d M
71 107 47 G 90 132 5a Z
110 156 6e N
72 110 48 H 91 133 5b [
111 157 6f O
73 111 49 I 92 134 5c \
112 160 70 P
74 112 4a J 93 135 5d ] 113 161 71 Q
75 113 4b K 94 136 5e ^ 114 162 72 R
76 114 4c L 95 137 5f _ 115 163 73 S
77 115 4d M 96 140 60 ` 116 164 74 T
78 116 4e N 97 141 61 A 117 165 75 u
79 117 4f O 98 142 62 B 118 166 76 v

80 120 50 P 99 143 63 C 119 167 77 w

81 121 51 Q 100 144 64 D 120 170 78 x


121 171 79 y
82 122 52 R 101 145 65 E

Programming Language 1 6
Escape sequences

>> Escape sequences are used to define certain special characters within string
literals. These are special characters that are difficult or impossible to express
otherwise in the source code of a program, like newline (\n) or tab (\t).

>> All of them are preceded by a backslash (\).

Escape ASCII Escape ASCII Escape ASCII


Description Description Description
sequence value sequence value sequence value

null
\0 0 \n line feed - new line 10 \" double quote 22
character

\a audible bell 7 \v vertical tab 11 \' single quote 27

form feed - new question


\b backspace 8 \f 12 \? 63
page mark
horizontal
\t 9 \r carriage return 13 \\ backslash 92
tab

Programming Language 1 7
Tokens in C

>> A C program consists of various tokens and a token is either a keyword, an


identifier, a constant, a string literal, or a symbol.

printf
Individual C tokens (
printf("Hello, World! \n"); "Hello, World! \n"
)
;

Programming Language 1 8
Identifiers
>> A C identifier is a name used to identify a variable, function, or any other user-
defined item. An identifier starts with a letter A to Z or a to z or an underscore _
followed by zero or more letters, underscores, and digits (0 to 9).

>> C does not allow punctuation characters such as @, $, and % within identifiers. C is
a case sensitive programming language. Thus, Manpower and manpower are two
different identifiers in C.

>> Here are some examples of acceptable identifiers: age, name, a_123, myname,
_temp, retVal

Programming Language 1 9
Keywords

>> Keywords are the words whose meaning has already been explained to the C
compiler. C language have some reserved words which cannot be used as
constant or variable or any other identifier names.

Programming Language 1 10
Data Types
>> Executable program must be brought to the main memory.

>> Needed to specify how much space is required before storing the data.

>> This means, the size must be defined in bytes starting with the lowest possible
size 1 Bytes which defined by the data types.

>> In every programming language, some basic data types are already defined.

>> These data types vary in size and the meaning of the content in each bit.

Programming Language 1 11
Data Types: Number Types

Type Declaration Size Range


Integer int Signed: From 2,147,483,648 to 2,147,483,647
32 bits from (231) to 231 1
Unsigned: From 0 to 4,294,967,295 which equals 0 to 232 1
long Signed: From 9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
64 bits from (263) to 263 1
Unsigned: From 0 to 18,446,744,073,709,551,615 which equals 0
to 264 1
Real float 32 bits 3.4 (10-38) to 3.4 (10+38) with 7 digit precision
double 64 bits 1.7 (10-308) to 1.7 (10+308) with 15-16 digits precision
long double
80 bits 3.4 (10-4932) to 1.1 (10+4932)

Programming Language 1 12
Data Types: Number Types

>> To get the exact size of a type or a variable on a particular platform, you can use
sizeof.

>> The expressions sizeof(type) yields the storage size of the object or type in bytes.

>> Following is an example to get the size of int type on any machine:

#include <stdio.h>
#include <limits.h>
int main() output
{ Storage size for int : 4
printf("Storage size for int : %d \n", sizeof(int));
return 0;
}

Programming Language 1 13
Data Types: Symbol Type

>> As there are 128 symbols to represent the symbols from the ASCII character
set, we need at least 1 byte (8 bits) which can range from -128 to +127. But to
represent UNICODE we require 2 bytes (16 bits) which represent the all the
ASCII character symbols along with all other language symbols. Following is the
data type to represent UNICODE in C/C++.

Type Declaration Size Range

Character char 16 bits Signed: From 32,768 to 32,767, from (215) to


215 1
Unsigned: From 0 to 65,535 which equals 0 to
216 1

Programming Language 1 14
What is Signed & Unsigned

>> An integral type with n bits can encode 2n numbers.

>> An unsigned type typically represents the non-negative values 0 through 2n1.
For example, 8 bits (1 byte) can represent 28 (256) different numbers; 0 to 28-1 (0 to
255) unsigned (positive) numbers.

>> As there is no way to store the negative sign '' in bits, twos complement is used
to represent negative numbers. This enables an integral type with n bits to
represent values (-2n-1) through (2n-1-1). So 8 bits (1 byte) can represent signed
values from -27 to 27-1 (-128 to +127).

Programming Language 1 15
Variables

>> Variable is nothing but a name given to a storage area that a programs can
manipulate. Each variable in C has a specific type, which determines the size and
layout of the variable's memory.

>> Or, a variable is entity that may vary during program execution. Variable names
are names given to locations in memory.

>> The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and
lowercase letters are distinct because C is case-sensitive. No commas or blanks are
allowed within a variable name.

Programming Language 1 16
Variables
>> Variable Definition in C:

type variable_list; // Variables declaration


without initialization.

type variable_name = value; // Variables declaration


with initialization.

where variable_list may consist of one or more identifier names separated by


commas.

>> For example:


int i, j, k, age;
char c, ch;
float f, salary;
double d;
int d = 3, f = 5;
char x = 'x';

Programming Language 1 17
Memory Management

>> A variable is an area of memory that has been given a name. For example: the
declaration int x; acquires four byte memory area and this area of memory that
has been given the name x.

>> But the computer access its own memory not by using variable names but by
using a memory map with each location of memory uniquely defined by a
number, called the address of that memory location. To access the memory of a
variable the program uses the & operator. For Example: &x returns the address of
the variable x.

&x represents the memory area of variable named x.

x represents the value stored inside the area of variable


int x;
named x.

Programming Language 1 18
Constants

>> A constant is an entity that doesnt change whereas a variable is an entity that
may change.

>> A constants refer to fixed values that the program cannot alter during its
execution. These fixed values are also called literals.

>> Constants can be of any of the data types like an integer constant, a floating
(real) constant, or a character constant.

>> The constants are treated just like regular variables except that their values
cannot be modified after their definition.

Programming Language 1 19
Constants

>> There are basically two types of constant:


Primary Constants
Secondary Constants

Programming Language 1 20
Constants

>> There are two simple ways in C to define constants:

1. Using #define preprocessor.

#define identifier value

2. Using const keyword

const type variable = value;

Programming Language 1 21
Constants

output output

value of area : 50 value of area : 50

Programming Language 1 22
Literals

>> Literals are the most obvious kind of constants.

>> They are used to express particular values within the source code of a program.

>> For example, to give concrete values to variables or to express messages to print
out. When we wrote: a = 5; the 5 in this piece of code is a literal.

>> Literal can be divided in Integer literals (numerals), Floating-Point literals


(numerals), Characters, and Strings literals.

Programming Language 1 23
Integer Literals

>> integer literals are of type int. However, we can force them to either be
unsigned by appending the u character to it, or long by appending l:

>> In both cases, the suffix can be specified using either upper or lowercase letters.

Programming Language 1 24
Floating-Point Literals
>> They express numbers with decimals and/or exponents. They can include either
a decimal point, an e character (that expresses "by ten at the Xth height", where X
is an integer value that follows the e character), or both a decimal point and
an e character:

>> The default type for floating point literals is double. If you explicitly want to
express a float or a long double numerical literal, you can use the f or l suffixes
respectively:

Programming Language 1 25
Character Literals
>> Character literals are enclosed in single quotes, e.g., 'x' and can be stored in a
simple variable of char type.

>> A character literal can be a plain character (e.g., 'x'), an escape sequence (e.g., '\t'),

Programming Language 1 26
String Literals

>> String literals or constants (which generally consists of more than one character)
are enclosed in double quotes "".

>> You can break a long line into multiple lines using string literals and separating
them using whitespaces. Here are some examples of string literals. All the three forms
are identical strings.

"hello, dear"
"hello, \
dear"
"hello, " "d" "ear

Programming Language 1 27
Input / Output Functions

>> Receiving Input: scanf()

>> Displaying Output: printf()

/* Calculation of simple interest */


main( )
{
int p, n ;
float r, si ;
printf ( "Enter values of p, n, r" ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "%f" , si ) ;
}

>> & is an Address of operator which gives the location number used by the variable
in memory

Programming Language 1 28
The % Format Specifiers

Specifier Usual variable type Display Others


%c char single character
%d int signed integer %ld long
%f float signed format %lf double
%p pointer address or location
%s array of char sequence of characters
%u unsigned int unsigned decimal

Programming Language 1 29
Flowchart to C Code

Flowchart for adding two numbers

Programming Language 1 30
Flowchart to C Code

C Code for adding two numbers

/* Adding two numbers*/


main( )
{
int num1, num2 ;
int sum ;
printf ( "Enter values of number 1 and number2 : " ) ;
scanf ( "%d %d", &num1, &num2) ;
sum = num1 + num2 ;
printf ( Sum : %d" , sum ) ;
}

Programming Language 1 31
Pseudocode to C Code

Pseudocode for finding average of three grades

1. Start
2. Get three grades
3. Average them
4. Print Average
5. End

C Code for finding average of three grades

/* Average three grades*/


main( )
{
int grade1, grade2, grade3;
float average;
printf ( "Enter values of grade1, grade2 and grade3 : " ) ;
scanf ( "%d %d %d ", &grade1, &grade2, &grade3) ;
average = (grade1 + grade2 + grade3)/3;
printf ( Average: %f", average) ;
}
Programming Language 1 32

Das könnte Ihnen auch gefallen