1 / 13

Overview of C

Overview of C. A brief history of C programming language Preprocessor Keywords, special symbols, identifier Variable Names, data types, declarations, address, value Fundamental data types: integer, floating point, character Statements Assignment Arithmetic expression Functions and

keon
Télécharger la présentation

Overview of C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overview of C • A brief history of C programming language • Preprocessor • Keywords, special symbols, identifier • Variable • Names, data types, declarations, address, value • Fundamental data types: integer, floating point, character • Statements • Assignment • Arithmetic expression • Functions and • Main function • Function call • Input and output functions • Program style • Compilation and execution • Program errors

  2. A Brief History of C Programming Language • Who, when and why? • C programming language was created by Dennis Ritchie at AT&T Bell Labs in 1972, • a successor of B which was created by Ken Thompson at Bell Labs • Purpose of C was to enable programmers to develop applications more efficiently • UNIX OS almost totally rewritten in C, 1973 • ANSI made a standard for C in 1983 • MSDOS was also written in C, 1984

  3. An Example of a C Program

  4. Preprocessor Directives and Comments • Preprocessor directives • A preprocessor is a program that modifies the program prior to its compilation • A line starts with #, provides an instruction to the preprocessor • Syntax: # include <standard header file> # define Name value # include Name of another program file • Examples #include <stdio.h> #define KM_PER_MILE 1.609 #include ”file1.c” • Library • A collection of useful functions and symbols that may be accessed by a program. • scanf() and printf() are two functions in library stdio • Constant marco • A name that is replaced by a constant value in preprocessing • Comments • A comment is enclosed in /* and */, provides supplementary info, but is ignored by preprocessor and compiler

  5. Reserved Words and Special Symbols • Reserved words (keywords) (See Appendix E) • A word that has special meaning in C. There are 40 reserved words in C • Data type int, double, char, float, short, signed, unsigned, short, long, const, struct, union, typedef, static, extern • Flow control: if, else, while, do, for, switch, case, default, break, continue, auto, return • Special symbols #, /*, */, (, ), {, }, +, - *, /, %, ;, = [, ]

  6. Identifiers • Standard identifier • A word has a special meaning, but may be redefined. • Function names defined in ANSI C library: scanf, printf • User-defined identifiers • User defined names for constant, variable, function name • Rules in choosing an identifier • Must consists only of letters, digits, and underscores • Cannot begin with a digit • No reserved word • Not being used by standard library • Case sensitive • Examples: • Valid identifer: Letter1, LETTER1, letter_1, a2 • Invalid identifer: letter-1, $money, 3mp, double • Convention: meaningful, simple, consistent. miles, kms, area_of_circle, price_per_sq_in

  7. Variables, Data Types, and Declarations • What is a variable? • A variable represents a location in memory, whose value can be changed. A variable is used to hold data in a program • A variable has four parts: a name, a data type, address, and value. A variable must be declared before using • The name is expressed by user-defined identifier • Data type is a set of values and a set of operations on those values. The data type of a variable determines the number of memory cells required for the variable • Examples: int, double, char

  8. Variables, Data Types, and Declarations (con’d) • Declarations A variable declaration tells the compiler the name and the kind of value stored in the variable. Must end with semicolon ; Examples double kms; double miles;  double kms, miles; • Address: after declaration, an address of the location in the memory is assigned, the first address of the memory cell in the location For example the address of variable miles is &miles • The value of a variable is what stored in the variable. Much be assigned through assignment statement Examples kms = 1.609*miles;

  9. Test this example /* Converts distances from miles to kilometers. */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 /* conversion constant */ int main(void) { double miles, kms; /* Get and echo the distance in miles. */ printf("Enter a distance in miles>\n"); scanf("%lf", &miles); printf("The distance in miles is %.2f.\n", miles); /* Convert the distance to kilometers. */ kms = KMS_PER_MILE * miles; /* Display the distance in kilometers. */ printf("That equals %.2f kilometers.\n", kms); /* Display the address of miles and kms */ printf("The address of mile is %d\nThe address of kms is %d\n%", &miles, &kms); fflush(stdin); getchar(); return (0); }

  10. Data Types for Integers • How integers are represented in computers? • Only certain types of integers can be processed by C • Machine dependent • Example int age;

  11. Data Types for Real Numbers • Two data types for real numbers in C. • Single precision floating-point uses 32 bits represented in a fixed format (4 bytes) e.g. 3.1415926 is represented in single precision as 1 10000000 10010010000111111011010 • Double precision floating-point uses 64 bits e.g. 3.1415926 is represented in double precision as 010000000000 100100100001111110110100110100010010110110000100 • Delarations for single and double floating point variables float miles; double miles; • There are two ways to represent a real number in programs or display • Decimal format: 3.1415925 • Exponential format 15.0e2  15.0 times 10 to the power 2 1500.0 15.0e-2  15.0 times 10 to the power -2 0.15 • The differences in memory and what we write in program

  12. Data Type for Characters • In computer a character is represented by 8 bits code such as ASCII using one memory cell • Each character corresponds to an integer in code(See Appendix A) http://www.lookuptables.com/ char c1, c2; c1 = ’a’; c2 = ’b’; Note that the memory cell for c1 will store 97 in decimal, or in binary 01100001

  13. Example of Data Types #include <stdio.h> int main() { int apple; double orange; double pear; char name; apple = 5; orange = 8.5; pear = 94e-1; name = 'a'; printf("I have %d kilo of apple %d\n\n", apple, &apple); printf("I have %f kilo of orange %d\n\n", orange, &orange); printf("I have %e kilo of orange\n\n", orange); printf("I have %f kilo of pear %d\n\n", pear, &pear); printf("I have %e kilo of pear\n\n", pear); printf("I have %f kilo of pear\n\n", pear); printf("My name is %c %d\n\n", name, &name); printf("My name is %d \n\n", name); printf( "\nPress Enter to continue..." ); fflush( stdin ); getchar(); return 0; }

More Related