Uploaded by Sokha Heng

Basic C

advertisement
Basic of C Programming
Heng sokha
A Simple C Program
/* A Simple C Program */
#include <stdio.h>
int main(void)
{
printf(“C is Sea\n”);
return 0;
}
Structure of C Program
Structure of C Program
Backslash Codes
Header Files
The following list contains the set of standard headers (24 Header files):
Concept of Variable
• Programs operate on data.
• The instruc?ons that make up the program and the data that it acts upon have to be
stored somewhere while the computer is execu?ng that program.
• a computer provides a random access memory (RAM) for storing the executable
•
•
•
program code and the data
A computer memory is made up of registers and cells which are capable of
holding informa?on in the form of binary digits 0 and 1 (bits). It accesses data as a
collec?on of bits, typically 8 bits, 16 bits, 32 bits or, 64 bits.
Data is stored in the memory at physical memory loca?ons. These loca?ons are
known as the memory address. Therefore, each byte can be uniquely iden?fied by
its address
Concept of Variable (2)
• A variable is an identifier for a memory location in which data can be stored and
subsequently recalled
Concept of Variable (2)
Syntax of Variable Declaration:Syntax !ន#រ%ប#ស អ)*ត,
[DataType] Variable_Name; or
[DataType] Variable_Name= initialized Value;
Example: int salary = 65000;
Data type
•
character—Keyword used is char
•
integer—Keyword used is int
•
floating-point—Keyword used is float
•
•
double precision floating point—Keyword used
is double
valueless—Keyword used is void
Data type
Data type
Token
• Tokens are one or more symbols understood by the compiler that help it interpret the program code.
• There are five classes of tokens: identifiers, reserved words, operators, separators, and constants.
• Identifier It is a sequence of characters invented by the programmer to identify or name a specific
object, and the name is formed by a sequence of letters, digits, and underscores.
• Keywords These are explicitly reserved words that have a strict meaning as individual tokens to the
compiler. They cannot be redefined or used in other contexts. Use of variable names with the same name
as any of the keywords will cause a compiler error.
• Operators These are tokens used to indicate an action to be taken (usually arithmetic operations, logical
operations, bit operations, and assignment operations).
• Separators These are tokens used to separate other tokens. Two common kinds of separators are
indicators of an end of an instruction and separators used for grouping.
• Constant It is an entity (value) that does not change.
Identifier
• Identifier is a sequence of characters invented by the programmer to identify or name a
specific object, and the name is formed by a sequence of letters, digits, and underscores.
• In C, variables, arrays, functions, and labels are named. the names of those variables, arrays,
functions and labels are identifiers
•Some rules must be kept in mind when naming identifiers
1. The first character must be an alphabetic character (lower-case or capital letters) or an
underscore ‘_’.
2. All characters must be alphabetic characters, digits, or underscores.
3. A keyword cannot be duplicated by an identifier. A keyword is word which has special
meaning in C.
Ex. int score_1=80; //correct
int main=50; // error because main is a keyword in C
int math score=80 // error because there is space
Keywords
• Keywords are the vocabulary of C
Constants
• A constant is an explicit data value written by the programmer. Thus, it is a value known to the compiler
•
•
•
•
•
at compiling time.
Example: 20, 45.3, ‘A’, “Hello” are constants
a literal integer (e.g. 20, 134…) is considered to be int
If suffix is added to a literal integer, it will be specified to be other type of data
• suffix L or l : ex. 134L or 134l is long
• suffix U or u :ex. 134U or 134u is unsigned integer
• suffix 0: ex. 0134 is octal
• suffix 0x or 0X: : ex. 0x25, 0x5C is hexadecimal
A floating-point constant consists of an integer part, a decimal point, a fractional part
• Ex. 156.67 ( 156 is integer, “.” is decimal point, and 67 is fractional part)
An exponent field containing an e or E followed by an integer. ( e/E stands for exponent)
• Ex. 2.164E-3 = 2.164 × 10-3
Operators and Expressions
An operator is a symbol that specifies the mathematical, logical, or relational operation to be performed
Binary Operators
• Arithmetic binary operators
Operation
12 + 4.9
Operand
• Priority: 1. ()
2. *, /, %
3. +, -
Operand
Operator
Download