Professional Documents
Culture Documents
CSC 1102
akinul@aiub.edu
C Basic
>> 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
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
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).
null
\0 0 \n line feed - new line 10 \" double quote 22
character
Programming Language 1 7
Tokens in C
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
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++.
Programming Language 1 14
What is Signed & Unsigned
>> 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:
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.
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
Programming Language 1 20
Constants
Programming Language 1 21
Constants
output output
Programming Language 1 22
Literals
>> 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.
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
>> & is an Address of operator which gives the location number used by the variable
in memory
Programming Language 1 28
The % Format Specifiers
Programming Language 1 29
Flowchart to C Code
Programming Language 1 30
Flowchart to C Code
Programming Language 1 31
Pseudocode to C Code
1. Start
2. Get three grades
3. Average them
4. Print Average
5. End