Sie sind auf Seite 1von 31

Currently, the most commonly-used language for embedded systems High-level assembly Very portable: compilers exist for

virtually every processor Easy-to-understand compilation Produces efficient code Fairly concise

Developed between 1969 and 1973 along with Unix Due mostly to Dennis Ritchie Designed for systems programming

Operating systems Utility programs Compilers Filters

Evolved from B, which evolved from BCPL

Designed by Martin Richards (Cambridge) in 1967 Typeless Everything an n-bit integer (a machine word) Pointers (addresses) and integers identical Memory is an undifferentiated array of words Natural model for word-addressed machines Local variables depend on frame-pointer-relative addressing: dynamically-sized automatic objects not permitted Strings awkward Routines expand and pack bytes to/from word arrays

Original machine (DEC PDP11) was very small

24K bytes of memory, 12K used for operating system

Written when computers were big, capital equipment

Group would get one, develop new language, OS

Alphabets

Digits Special Symbol

A, B,..Y, Z a, b,y, z 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ~, , !, @, #, %, ^, &, P, *, (, ), _, -, +, =, |, \, {, }, [, ], :, ;, , <, >, ,, ., ?, /

#include <stdio.h> void main() { printf(Hello, world!\n); }

Preprocessor used to share information among source files - Clumsy + Cheaply implemented + Very flexible

#include <stdio.h>
void main() { printf(Hello, world!\n); }

Program mostly a collection of functions main function special: the entry point void qualifier indicates function does not return anything

I/O performed by a library function: not included in the language

Constant is an entity that does not change. Variable is an entity that may change. Variable name is a way to access the value stored in the memory cell. Int x=3; x 3

1. 2.

Primary Constant Secondary Constant C Constants

Primary Constants

Secondary Constants

Integer Constant Real Constant Character Constant

Array Pointer Structure Union Enum, etc.

1.

2.
3. 4.

5.

6.

7.

An integer constant must have at least one digit. It must not have a decimal point. It can be either positive or negative. If no sign precedes an integer constant, it is assumed to be positive. No commas or blanks are allowed within an integer constant. The allowable range for integer constants is -32768 to 32768. Ex. 425, +234, -6000, -4563.

Real Constants are often called Floating Point constants. 1. A real constant must have at least one digit. 2. It must have a decimal point. 3. It could be either positive or negative. 4. Default sign is positive. 5. No commas or blanks are allowed within a real constant. 6. Ex. +234.45, 345.0, -76.45, -23.4567

1.

2.

3.
4.

5.

The mantissa part and the exponential part should be separated by a letter e or E. The mantissa part may have a positive or negative sign. Default sign of mantissa part is positive. The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive. Range of real constant expressed in exponential form is +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5.

1.

2.

3.

A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas. Both the inverted comma should point to the left. The maximum length of a character constant can be 1 character. Ex. A, I, 5, =.

Variable names are names given to location in memory. 1. A variable name is any combination of 1 to 31 alphabets, digits or underscores. Some compilers allow variable names whose length could be up to 247 characters. Do not create unnecessarily long variable name. 2. The first character in the variable name must be an alphabet or underscore. 3. No commas or blanks are allowed within a variable name. 4. No special symbol other than an underscore can be used in a variable name. 5. Ex valid variable names- _ab, a, c, d, c123, d12a.

Keywords are the words whose meaning has already been explained to the compiler. The keywords cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed by the computer. There are 32 keywords available in C.

Auto Break Case Char Const Continue Default Do

double else enum extern float for goto if

int long register return short signed sizeof static

struct switch typedef union unsigned void volatile while

Each instruction in a C program is written as a separate statement. Therefore, a complete C program would comprise a series of statements. The statement in a program must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate jump or transfer of control to a statement, which is out of sequence. Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keywords. All statements are entered in small case letter. C has no specific rules for the position at which a statement is to be written. Thats why it is often called a free-form language. Every C statement must end with a ; thus ; acts as a statement terminator.

Comments about the program should be enclosed within /* */. It is a good practice to begin program with a comment indicating the purpose of the program, its author and the date on which the program was written. Any number of comments can be written at any place in the program. A comment can be split over more than one line, as in, /* this is a jazzy comment */ such a comment is often called multi-line comment. main() is a collective name given to a set of statement. This name has to be main(), it cannot be anything else. All statement belong to main() are enclosed within a pair of braces { } as shown below Void main() { Statement1; Statement2; }

Technically speaking main() is a function. Any variable used in a program must be declared before using it. For exampleint p,n; /* declaration */ float r, si; /* declaration */ si=p*n*r/100; /* usage */

1.

1.

There are technically three types of instruction in C Type declaration Instruction- To declare the type of variables used in a C program ex. Int b, c; float f; char ch; Arithmetic Instruction- To perform arithmetic operations between constants and variables. A C arithmetic statement could be of three types. These are as follows:1. Integer Mode arithmetic statement- In which all operands are either integer variables or integer constants. 2. Real Mode arithmetic statement- Operands are either real variables or real constants. 3. Mixed Mode Arithmetic statement- Some of operands are integer and some are real. ex. Int a,b,c; float f,g,h; c=a+b /* Integer Mode */ h= f+g /* Real Mode */ g= a+b+f /*Mixed Mode */

Control Instruction- To control the sequence of execution of various statements in a C program. There are 4 types of control statement in C 1. Sequence control Instruction- ensures that the instructions are executed in the same order in which they appear in the program. 2. Selection or Decision Control Instruction- It allows the computer to take a decision as to which instruction is to be executed. 3. Repetition or Loop Control InstructionHelps computer to execute a group of statement repeatedly. 4. Case Control Instruction- It allows the computer to take a decision as to which instruction is to be executed.

Some guidelines that should be remember while writing Arithmetic instruction: C allows only one variable on left hand side of =. That is z=k*l is legal, whereas k*l=z is illegal. An arithmetic instruction is at times used for storing character constants in character variable. Char a, b, d; a=F; b= G; d=+; When we do this the ASCII values of the characters are stored in the variables. ASCII values are used to represent any character in memory.

No operator is assumed to present. It must be written explicitly. for ex. A=c.d.b(xy) /* illegal */ A= c*d*b*(x*y) /* legal */ There is no operator for performing exponentiation operation. pow() function is used to determine exponentiation but before using it we have to include header file math.h

Rules that are used for the implicit conversion of floating point and integer values in C.
An arithmetic operation between an integer and integer always yields an integer result. An operation between a real and real always yields a real result. An operation between an integer and real always yields a real result. In this operation the integer is first promoted to a real and then the operation is performed. Hence the result is real.

1.

2.

3.

It may so happen that the type of the expression and the type of the variable on the left hand side of the assignment operator may not be the same. In such a case, the value of the expression is promoted or demoted depending on the type of the variable on the left hand side of = Exint I; Float b; i=3.5; /* I is int type */ b=30; /* b is float type */

In first assignment expression, 3.5 is float hence it cannot be stored in I since it is an int. In such a case, the float is demoted to an int and then its value is stored. Hence what gets stored in I is 3. In second assignment expression 30 is promoted to 30.000000 and then stored in b, since b being a float variable cannot hold anything except a float value.

Exfloat a, b, c; int s; s=a*b*c/100+32/4-3*1.1; Here, in the assignment statement some operands are int whereas others are float. During evaluation of the expression, the ints would be promoted to floats and the result of the expression would be a float. But when this float value is assigned to s it is again demoted to an int and then stored in s.

Priority 1st 2nd * ,

Operators / , +, %

Description Multiplication, Division, Modular Division Addition, Subtraction

3rd
1.

Assignment

Within parentheses the same hierarchy as mentioned in table is operative . 2. If there are more than one parentheses, the operation within the innermost parentheses would be performed first, followed by the operation within the second innermost pair and so on. 3. We, must always remember to use pair of parentheses. A careless imbalance of the right and left parentheses is a common error.

When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. There are two types of associativity:1. Left to Right & 2. Right to Left 1. Left to Right- means that the left operand must be unambiguous. 2. Right to Left- means that the right operand must be unambiguous.

Exa= 3/2*5; In the above example / and * both have equal priority and both are left to right associative but only / operation has unambiguous left operand so it is performed earlier. In case, both operators of equal priority have unambiguous operands in the desired position according to associativity then compiler is free to perform any operation as per its convenience.

Thank You

Das könnte Ihnen auch gefallen