C
Short Description
Download C...
Description
C Language Set of programs are called as language Computer languages 1. Low level languages (machine Language) 2. Assembly languages (Nuemonics) 3. High level languages Compiler: It checks the entire source program and if it is error free, produces a complete object program. Source program- Retained for modifications and corrections Object program- Loaded into the computer’s memory for execution. Interpreter: It translates one statement at a time and if it is error free executes the instructions and so on. Difference between compiler & interpreter Compiler Interpreter 1. Error correction (called debugging) is here error correction issimpler Somewhat tedious. Since it produces an error .because it is done in stages list for the entire program before execution of even the first instruction can begin. 2. Programs are executed fastly. Here, it takes more time for execution, because a statement has to be translated every time the program is executed Execution of a program: This is basically a two step process. 1. The source program is first compiled. 2. Then it is converted into object program and loaded into memory Programming Techniques Three types of techniques: 1. Top down approach->Ex: C-language 2. Bottom Up approach -> Ex: C++ 3. Modular Programming Year 1960 1963 1967
1970
Language ALGOL
History of C: Developed By International committee
Remarks too general, Too abstract CPL Cambridge University Hard to Learn Difficult to implement BCPL Martin Lichards at Too specific Cambridge University could deal with only specific problems B
Ken Thampson at
Too specific could
AT&T’S Bell Labs 1972
C generality of
Dennis Ritchie at
deal with only specific problems Host
AT&T’s Bell labs
BCPL & B
Restored. C is a Middle Level Language: Reason: Basically programming Languages are classified into two categories 1. Problem oriented language: [HLL-High Level Language] i) Design to give a better program efficiency. ii) Faster Program development. 2. Machine Oriented Language: [LLL-Low Level Language] i) Design to give a better Machine efficiency. ii) Faster Program execution C is designed to satisfy these two conditions. Hence, it is called as Middle level Language. C is a Structured Programming Language: Reason: Structured programs used only 3 basic constructs. 1. Sequential structure- Steps are performs one after another in succession. (Step by step action) 2. Selection Structure – Implemented with an If then else (or) case statement. 3. Repetition Structure – Implemented with a for, while (or) Repeated statements. Features of C: 1. Economy of expressions 2. Modern control flow and structures 3. Rich set of operators. Characteristics of C: 1. Size of Language 2. Modern Control structures [if statement, if… else statement, case statement, while loop, for loop, do…while loop] 3. Bitwise control 4. Pointer Implementation Uses of C: 1. System Applications: a) Operating System b) Interpreters c) Editors d) Assembly Programs e) Compilers 2. Database Systems 3. Graphics Package 4. Spread sheet 5. Word processors 6. CAD/CAM applications 7. Office Automations 8. Scientific and Engineering applications
C-Character sets: A to Z 1 to 9 a to z Special characters: + - * /(division) %(Modulus) #(hash) $(dollar) ~(tilde) {}(curly brackets) ()(Open and Close brackets) [](square brackets) , . ; : ’ ” _ @(at) ? < > ! & (ambersand) ^ (exponent) \ (backslash) | (Pipe symbol) Data Types: Data Types
Primary Data Types (Or) Built in data Types
Secondary Data Types (Or) User defined Data Types
1. Integer 1. Arrays 2. Float 2. Pointers 3. Character 3. Structures 4. Double 4. Unions 5. Void (meaning less data) 5. Enums Primary Data Types: 1. Integer: A Positive or Negative whole Number 2. Float: A number having decimal point (either positive or negative) 3. Character: A single character represented within single quote. 4. Double: Floating Point Number having exponent 5. Void: Meaning less data. Secondary Data types: 1. Arrays: It is a collection of similar / like data types, which occupies the adjacent memory location. 2. Pointers: It is a special type of variable which is used to store the address of another variable 3. Structures: It is a collection of unlike data types which occupies the adjacent memory location. 4. Unions: It is same as structure but uses the same memory location. 5. Enums This gives us an opportunity to define our own data types and determine what values it can take.
Constants: A value that does not change. C Constant
Character type
Character constant
Unsigned
long
short
string
Numeric type
integer Constant
integer
real constant
Float
constant
Double
Integer Constant This is an integer quantity Rules for constructing Integer constant 1. It must have at least one digit 2. It must not have a decimal point 3. It could be either positive or negative. 4. If no sign precedes an integer constant it is assumed positive. 5. No commas or blanks are allowed within an integer constant. 6. Range -32768 to +32767 7. It occupies two bytes e.g.: 425,-68,78 Long Integer 1. It occupies 4 bytes 2. Range -2,14,74,83,648 to +2,14,74,83,647 e.g.: 3456789,-2345897 Short integers Ordinary integers Unsigned Integer If we declare an integer as unsigned, the range will change to 0 to 65535. Declaring this will free the 16th bit and is used to store the number. It occupies two bytes in memory E.g.: 89, 67. Long unsigned Integer 1. Range 0 to 4294967295 2. It occupies two bytes. E.g.: 567897, 5683452 Note
By default, a short int is an unsigned short integer and a long integer is an unsigned long integer. Real constants It is often called as floating point constant. It occupies four bytes in memory Rules for constructing real constant expressed in fractional form 1. It must have at least one digital 2. It must have a decimal point. 3. It could be either positive or negative. 4. Default sign is positive. 5. No commas are blanks are allowed within a real constant. E.g.: +325.34,-35.78 Rules for constructing real constant expressed in exponential form 1. Mantissa part and exponent part should be separated by a letter e. 2. Mantissa part may have a Positive or negative sign. 3. Default sign of mantissa part is positive. 4. The exponent must have at least one digit, which must be a positive or a negative integer. Default sign is positive. 5. Range -3.4e38 to +3.4e38 Double Real constant with a precision of 16 digits are called as double constant. It occupies 8 bytes in memory. Character constant It is a single character enclosed within single quotes. Range -128 to +127 It occupies one byte in the memory. E.g.: ‘a’ ,’7’ String constant Collection of characters enclosed within double quotes is called as string constant. String constant are terminated by a null (‘\0’) character. E.g.: “hello” , “4567” Variable: A name given to the memory location where the constant value is stored in the memory. Rules for constructing variable Names 1. Variable name is any combination of 1 to 8 alphabets, digits or underscores. 2. First character must be an alphabet 3. No commas or blank spaces are allowed within a variable name. 4. No special symbols other than an underscore can be used in a variable name. Variable Declaration: Syntax: data type variable name; Eg: int a; float b; char c;
char name[30]; int a, b; Scope of variables Local – Available only to certain selected statements in the program. It is defined inside a function Global – It is accessible to all the statements in the function. It is declared outside the function. C keywords Keywords are the words whose meaning has already been explained to the C compiler. It cannot be used as variable names. There are 32 keywords available in C. They are auto break case char const continue default goto do
double int else enum extern float for sizeof if
struct long switch register typedef return union short unsigned signed void volatile static while C-PROGRAM
It is collection of functions. Every C program must have a main ( ). The program execution starts from the main function. A function name is always followed by a pair of parantheses ().Apart from these each C-Compiler provides a library of around 200 predefined functions and macros designed for use in C Programs. For using these functions, certain files must be included in the program which makes call to these functions. These files are known as header files. It contains macro definitions and function declarations. It has an extension .h. The statements within a function are always enclosed within a pair of curly brackets { }. Some Common Header files: 1. stdio.h –Contains standard Input and output functions 2. conio.h – Contains console Input and output functions 3. math.h – Contains Mathematical functions 4. string.h – Contains string Input and output functions 5. graphics.h – Contains graphical functions Escape sequences Certain ASCII characters are unprintable, which means they are not displayed on the screen or printer. Those characters perform other functions aside from displaying text. Escape sequences usually consist of a backslash and a letter or a combination of a digit. An escape sequence is regarded as a single character and is therefore valid as a character constant. Escape sequences are typically used to specify actions such as carriage return and tab movements on terminals and printers. The commonly used escape sequences are listed below. Escape sequences Represents \a Bell (alert) \b Backspace
\f \n \r \t \v \’ \’’ \? \\ \0
Form feed new line Carriage Return Horizontal tab Vertical tab Single quotation Mark Double Quotation Mark Literal Quotation Mark Backslash NullString Terminate
Format of a C Program: #include main() { Variable declarations; clrscr(); Program statements; getch(); } Ex: #include #include main() { int a,b,sum; clrscr(); printf(“Enter the values for a and b:”); scanf(“%d%d”,&a,&b); sum=a+b; printf(“Sum=%d”,sum); getch(); } Standard Input and Output Functions: Standard Input function: scanf( ) Syntax: scanf(“format string”, list of address variable); Definition: This function accepts the input value from the keyboard and stores it into the corresponding memory location. Ex: scanf(“%d”,&a); scanf(“%f%f”s,&a,&b); Conversion character: %d Data item is a decimal integer %f Data item is a floating point value %s Data item is a string %c Data item is a character %lf Data item is a double
%ld Data item is a long integer. & Address Operator 2. Standard output function: printf( ) Syntax: printf(“format string”, list of variables); Description: This function prints the given output on the screen. Ex: printf(“%d”,sum); printf(“Enter the values for a and b”); printf(“Sum=%d”,sum); Ex 1: Program for Addition: #include #include main( ) { int a,b,sum; clrscr(); printf(“Enter the values for a&b:”); scanf(“%d%d”,&a,&b); sum=a+b; printf(“Sum=%d”,sum); getch( ); } Character Input Output function: Single character Input function: getchar( ); Syntax: Variable name=getchar( ); Description: This function inputs a single character from the keyboard and converts it into the equivalent ASCII value. Ex: C=’A’ C=getchar( ); Single character Output function: putchar( ); Description: This function converts the ASCII value of a single character and displays the result on the monitor. Syntax: Putchar(variable name); Ex: putchar(a); Ex: 2 Program for converting uppercase character into lowercase letter: #include
#include main() { int c; clrscr(); printf(“Enter an upper case letter”); c=getchar(); printf(“Lowercase=”); putchar(c+32); getch(); } Standard string Input output functions: Standard String Input Function: gets( ); Syntax: Variable name=gets( ); Description: This function inputs the string given in the keyboard and stores it into the corresponding memory location. Ex: Name=”Kavitha” Name=gets( ); Standard String Output function: puts( ); Syntax: puts(variable name); Description: This function prints the string on the monitor. Ex: 3 #include #include main( ) { char name[30]; clrscr( ); puts(“Enter your name”); gets(name); puts(“Your Name is:”); puts(name); getch(); }
Operators: Five types: 1. Arithmetic operators 2. Unary Operators 3. Relational and Logical Operators 4. Assignment Operators 5. Conditional Operators
1. Arithmetic Operators: To do arithmetical calculations. 1. + Addition 2. Subtraction 3. * Multiplication 4. / Division (Quotient) 5. % Division (Reminder) 2. Unary Operators: 1. -- Unary Minus (To represent Negative Number) 2. ++ Increment Operator Two types 1. Preincrement operatorEx: ++i 2. Post increment OperatorEx: i++ 3. -- Decrement Operator Two Types 1. Predecrement OperatorEx:--i 2. Post decrement OperatorEx:i-4. sizeof To print the size of data types. Ex: 4 #include #include main() { int i=1; clrscr(); printf(“i=%d\n”,i); printf(“i=%d\n”,++i); printf(“i=%d\n”,i); getch(); } Similarly do this program by changing i++,--i,i--) Ex:4 #include #include main() { clrscr(); printf(“Integer=%d\n”,sizeof(int)); printf(“Float=%d\n”,sizeof(float)); printf(“Character=%d\n”,sizeof(char)); printf(“Double=%d”,sizeof(double)); getch(); } 3.Relational Operators > greater than
< >= 0) printf(“The given no is a positive no”); else printf(“The given no is a negative no”); getch(); } 3.Nested if statement syntax: if (condition) statements; elseif (condition) statements; else statements; eg: #include #include main() { int pos; clrscr(); printf(“Enter any no:”); scanf(“%d”,&pos); if(pos>0)&&(pos!=0) printf(“The given no is a positive no”); elseif(pos
View more...
Comments