290 likes | 385 Views
The C Language: Intro. The C Language. Currently, the most commonly-used language for general purpose as well as embedded systems “High-level assembly” Very portable: compilers exist for virtually every processor Easy-to-understand compilation Produces efficient code Fairly concise.
E N D
The C Language • Currently, the most commonly-used language for general purpose as well as embedded systems • “High-level assembly” • Very portable: compilers exist for virtually every processor • Easy-to-understand compilation • Produces efficient code • Fairly concise
C History • Developed between 1969 and 1973 along with Unix • Due mostly to Dennis Ritchie • Designed for systems programming • Operating systems • Utility programs • Compilers • Evolved from B, which evolved from BCPL
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
C History • Original machine (DEC PDP-11) 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
Efficiency Limited amount of memory Fast Portability Compilers are small and easily written C: UNIX and ANSI/ISO standard Power Flexibility Standard library Input/output, string handling, storage allocation, etc. Integration with UNIX C Strengths
Can be error-prone Flexibility C compiler doesn’t detect many programming mistakes Pitfalls Can be difficult to understand Some features are easier to be misused Flexibility Can be difficult to modify No modules C Weakness
Hello World in C • Preprocessor used to share information among source files • Clumsy • + Cheaply implemented • + Very flexible #include <stdio.h> void main() { printf(“Hello, world!\n”); }
Hello World in C Program mostly a collection of functions “main” function special: the entry point “void” qualifier indicates function does not return anything #include <stdio.h> void main() { printf(“Hello, world!\n”); } I/O performed by a library function: not included in the language
Euclid’s algorithm in C “New Style” function declaration lists number and type of arguments Originally only listed return type. Generated code did not care how many arguments were actually passed. Arguments are call-by-value int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; }
Euclid’s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; } Automatic variable Storage allocated on stack when function entered, released when it returns. All parameters, automatic variables accessed w.r.t. frame pointer. Extra storage needed while evaluating large expressions also placed on the stack Excess arguments simply ignored n m Frame pointer ret. addr. Stack pointer r
Euclid’s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; } Expression: C’s basic type of statement. Arithmetic and logical Assignment (=) returns a value, so can be used in expressions % is remainder != is not equal
Euclid’s algorithm in C int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; } High-level control-flow statement. Ultimately becomes a conditional branch. Supports “structured programming” Each function returns a single value, usually an integer. Returned through a specific register by convention.
C tokens • Basic Building Blocks • Constructed together to write C program • Types • Keywords (eg: int, while), • Identifiers (eg: main, total), • Constants (eg: 10, 20), • Variable (eg: int x) • Operators (eg: +, /,-,*) • Strings (eg: “total”, “hello”), • Special symbols (eg: (), {}),
C Token example void main() { int x, y, total; x = 10, y = 20; total = x + y; printf ("Total = %d \n", total); } where, main – identifier {,}, (,) – delimiter int – keyword x, y, total – identifier + = - Operator main, {, }, (, ), int, x, y, total – tokens
Keywords • Keywords are pre-defined words in a C compiler. • Each keyword is meant to perform a specific function in a C program. • Since keywords are referred names for compiler, they can’t be used as variable name. • Supports 32 keywords • Eg: auto, int, char, float, if, else, for, do, while, break, continue etc
Identifier • Each program elements in a C program are given a name called identifiers. • Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name given to integer variable in above program. • RULES • First character should be an alphabet or underscore. • Succeeding characters might be digits or letter. • Punctuation and special characters aren’t allowed except underscore. • Identifiers should not be keywords.
Constant • C Constants are also like normal variables. But, only difference is, their values can not be modified by the program once they are defined. • Constants refer to fixed values. They are also called as literals • Constants may be belonging to any of the data type. • Syntax: • constdata_typevariable_name; (or) constdata_type *variable_name; OR #define pi 3.14
Types of C constant • Integer constants • Real or Floating point constants • Octal & Hexadecimal constants • Character constants • String constants • Backslash character constants
C variables • C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable. • The value of the C variable may get change in the program. • C variable might be belonging to any of the data type like int, float, char etc.
Rules For Naming C Variable • Variable name must begin with letter or underscore. • Variables are case sensitive • They can be constructed with digits, letters. • No special symbols are allowed other than underscore. • sum, height, _value are some examples for variable name • Three types: • Local • Global • Environment
Operator and Expressions • The symbols which are used to perform logical and mathematical operations in a C program are called C operators. • These C operators join individual constants and variables to form expressions. • Operators, functions, constants and variables are combined together to form expressions. • Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.
Types of C operator • Arithmetic operators • Assignment operators • Relational operators • Logical operators • Bit wise operators • Conditional operators (ternary operators) • Increment/decrement operators • Special operators
Special Symbol • The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose. • [] () {} , ; : * … = # • Braces{}: These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement. • Parentheses(): These special symbols are used to indicate function calls and function parameters. • Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts.