C RECAP Prepared by K Ravikumar M E
C RECAP Prepared by K. Ravikumar, M. E. , Asso. Professor – Department of Computer Science & Engineering Knowledge Institute of Technology KIOT Campus, Kakapalayam, Salem – 637 504.
Basics of C Environment Phase 1 Editor Phase 2 Preprocessor Phase 3 Phase 4 Compiler Linker Disk Program edited in Editor and stored on disk Disk Preprocessor program processes the code Disk Creates object code and stores on disk Links object code with libraries and stores on disk
Basics of C Environment Primary memory Phase 5 Loader Primary memory Phase 6 CPU Puts program in memory Takes each instruction and executes it storing new data values
Executing a C Program Steps involved in execution are n Creating the program n Preprocessing and Compiling the program n Linking and Loading the program with functions that are needed from the C library n Executing the program
Executing a C Program Edit Program Source Code Compile Object Code Library Files Link Object Code Executable
Basics Structure of C Program Documentation section Link section Definition section Global declaration section main() function section { Declaration part Executable part } Subprogram section (user defined function)
Simple C Program /* A first C Program*/ #include <stdio. h> void main() { printf("Hello World n"); }
C Character Set n n n Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Characters can be used to form words, numbers and expressions Characters in C are grouped into following categories • Letters ex: a…z, A…Z • Digits ex: 0… 9 • Special characters ex: , , &, @, _, +, -, …. . • White spaces ex: blank space horizontal tab new line…….
C Tokens n n n In a passage of text, individual words and punctuation marks are called tokens In a C source program, the basic element recognized by the compiler is the "token. " C Tokens are ØKeywords int, float, while ØIdentifiers sum, main ØConstants 100, -55. 5 ØStrings “ABC”, “Hello” ØOperators +, -, *, /, ++ ØSpecial symbols {, }, [, ]
Keywords n n n All keywords are reserved words have fixed meanings and these meanings cannot be changed Have special meaning to the compiler, cannot be used as identifiers in our program. Keywords serve as basic building blocks for program statement Keywords must be written in lowercase Displayed in BLUE color in MS Visual C++
Some Keywords auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
Identifiers n n Refer to the names of variables, functions and arrays User defined names and consist of a letters and digits, with a letter as a first character Rules for Identifiers n n n First character must be an alphabet Must consist of only letters, digits and underscore Only first 32 characters are significant Cannot use a keyword Must not contain white space Case sensitive-Identifier Sub differ from sub
Identifiers Examples of legal identifier: Student_age, Item 10, counter, number_of_character Examples of illegal identifier Student age (embedded blank) continue (continue is a reserved word) 10 th. Item (the first character is a digit) Principal+interest (contain operator character +)
Constants n Constants in C refer to fixed values that do not change during the execution a program Types of Constants v Numeric Constants Ø Integer Constants Ø Real Constants - 234, 045, 0 x 2 A, 0 X 3 B 2. 345, 0. 64 e-2 v Character Constants Ø Single Character Constants Ø String Constants ‘ 5’, ‘A’ “Hello”
Backslash Character Constants Escape Sequence Name Meaning a Alert Sounds a beep b Back space Backs up 1 character f Form feed Starts a new screen of page n New line Moves to beginning of next line r Carriage return Moves to beginning of current line t Horizontal tab Moves to next tab position v Vertical tab Moves down a fixed amount \ Back slash Prints a back slash ’ Single quotation Prints a single quotation ” Double quotation Prints a double quotation ? Question mark Prints a question mark
Backslash Character Example Program #include<stdio. h> void main() { printf("nabc"); printf("rdef"); printf("bghin"); printf("Hait. Hello"); }
Backslash Character Example Program Output #include<stdio. h> void main() { printf("nabc"); printf("rdef"); printf("bghin"); printf("Hait. Hello"); } deghi Hai Hello
Variables A variable is a data name used for storing a data value n The value may be changed during program execution Rules for defining variables n Must begin with a character n Should not be a C keyword n May be combination of lower and upper characters n Should not start with a digit n Maximum characters upto 31 characters n Example Sum, avg_wt, item
Declaration of Variables n Syntax for declaring a variable is as follows data-type v 1, v 2, …. vn; Example int i, j, sum; float avg; double ratio; unsigned int fact;
Data Types in C Type Keyword Bytes character char 1 -128. . . 127 integer int 2 -32768. . . 32767 short integer short 2 -32768. . . 32767 long integer long 4 -2, 147, 483, 648. . . 2, 147, 438, 647 long integer long 8 -9223372036854775808 … 9223372036854775807 unsigned char 1 0. . . 255 unsigned int 2 0. . . 4, 294, 967, 295 unsigned short integer unsigned short 2 0. . . 65535 unsigned long integer unsigned long 4 0. . . 4, 294, 967, 295 single-precision float 4 1. 2 E-38. . . 3. 4 E 38 double-precision double 8 2. 2 E-308. . . 1. 8 E 308 unsigned character unsigned integer Range
Program illustrating some C tokens er i f i nt ide main() { s d r o W d delimiters Reserve int a, b, sum; a = 14; b = 25; sum = a + b; printf("%d + %d = %dn", a, b, sum); }
Input and Output Functions Formatted Functions scanf() printf() Unformatted Functions getch() getche() getchar() gets() putchar() puts()
Formatted & unformatted I/O Fundamental Data Type Conversion Symbol+Format Specifier Integer short integer %d or %i short unsigned %u long signed %ld long unsigned %lu unsigned hexadecimal %X or %x unsigned octal %o float %f or %g double %lf character %c string %s Real Character
Operators n An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations Types of Operators ü Arithmetic Operators +, -, *, /, % ü Relational Operators <, <=, >, >=, ==, != ü Logical Operators &&, ||, ! ü Assignment Operators = ü Increment and Decrement Operators ++, -ü Conditional Operators ? = ü Bitwise Operators &, |, ^, <<, >> ü Special Operators , , sizeof, &, *. , ->
Decision Making - Branching n Decision making statements are used to skip or to execute a group of statements based on the result of some condition. n The decision making statements are, − simple if statement − if…else statement − nested if − else … if ladder − switch statement − goto n These statements are also called branching statements
Simple if statement Syntax: if(condition) { Statements; } if(condition) True Statements; False (Bypass)
Simple if - Example # include <stdio. h> void main () { int number; Output Type a number -50 The absolute value is 50 printf("Type a number: "); scanf("%d", &number); if (number < 0) number = -number; printf ("The absolute value is %d", number); }
if - else statement Syntax: if(condition) { True block statements; } else { False block statements; } if(condi tion) False True Block Statement False Block Statements
if – else Example # include <stdio. h> Output void main () { int num; Type a number 50 The number is positive printf ("Type a number: "); scanf ("%d", &num); if (number < 0) printf(“The number is negative”); else printf(“The number is positive”); }
Nested if Statement n. The if statement may itself can contain another if statement is known as nested if statement. Syntax: if(condition 1) { if(condition 2) { True block statement of condition 1 & 2; } else { False block statement of condition 2; } } else { False block statements of condition 1; }
Nested if Example # include <stdio. h> Output void main() { int n 1, n 2, n 3, big; Enter 3 numbers: 10 25 20 printf (“Enter 3 numbers: "); scanf ("%d %d %d", &n 1, &n 2, &n 3); The largest number is: 25 if (n 1 > n 2) { if(n 1 > n 3) big = n 1; else big = n 3; } if(n 2 > n 3) big = n 2; else big = n 3; printf(“The largest number is: %d”, big); }
Else - if Ladder Statement Syntax if (condition 1) statement block 1; else if (condition 2) statement block 2; else if (condition 3) statement block 3; : : else if (condition) statement block n; else default statement;
Else - if Ladder Example #include <stdio. h> void main () { int mark; printf ("Enter mark: "); scanf ("%d", &mark); if (mark <= 100 && mark >= 70) printf ("n Distinction"); else if (mark >= 60) printf("n First class"); else if (mark >= 50) printf ("n Second class"); else printf ("Fail"); } Output Enter mark: 75 Distinction
Switch Statement Syntax switch ( expression ) { case value 1: case value 2: ……. case valuen: default: } program statement; . . . break; program statement; . . . . break;
Switch Statement Example #include<stdio. h> #include<conio. h> #include<string. h> void main() { char st[100]; int i, count=0; clrscr(); printf("Enter line of text: "); gets(st); for(i=0; st[i]!='