Notes

Share Embed Donate


Short Description

Download Notes...

Description

12.08.2010 Installing Turbo C++ (3.0) – DOS Based – 16 bit software - Copy Turboc.exe file on C drive or D Drive - Goto Command Prompt and then D drive (D:) - Issue the following command on DOS prompt TURBOC –d -

It creates TC folder with sub folders like BIN, BGI, LIB, INCLUDE etc. Start Turbo C++ from TC\BIN\TC.EXE Setting the path of directories o Options  Directories… o Change the path from C drive to D drive if using the D drive

Types of entry points void main() int main() void main(int argc, char *argv[]) int main(int argc, char *argv[]) void main(int argc, char *argv[], char *env[]) int main(int argc, char *argv[], char *env[])

19.08.2010 Data Types in C -

-

-

Special keywords used to define type the type of data and range of data Data Types can be of three types o Primary Data Types o Modified Data Types o Derived or User Defined Data Types (UDT) C provides five primary types o char – 1 byte o int – 2 byte or 4 byte o float – 4 bytes (6 decimal places) o double – 8 bytes (14 decimal places) o void - nothing These primary types get modified using special keywords called modifiers o signed o unsigned o short

-

o long Data types modified from primary types using modifiers are called as modified data types o char  

-

signed char or char unsigned char

o

int

o

 signed int or int  unsigned int or unsigned  short int or short – 2 bytes  long int or long – 4 bytes  unsigned short  unsigned long double

 long double – 10 bytes Derived data types are user defined data types created as per programmer’s choice o Arrays o Pointers o Structures o Unions o Bitfields o Enumerators

Creating Variables -

A variable is a name given some memory location to store and retrieve the data Every variable must have some data type associated with it

Example unsigned rollno; char gender; float price; double weight; char name[50]; int i,j,k; Variable Naming Rules - A variable name can have alphabets (a-z, A-Z), digits (0 to 9) and underscore only - Can never start with digit - Two variables cannot have same name in same scope - Can be of any length but first 31 characters are significant

Data Input in a Program C provides various function for data input 1. scanf() a. To read formatted data of any type 2. gets() a. To read as string 3. getchar() a. To get a character in buffered mode 4. getch() a. To get a character but don’t echo it b. It uses un-buffered mode 5. getche() a. To get a character and echo it b. It uses un-buffered mode To read formatted data C provides special codes which starts with % called as format specifiers %d %o %x or %X %c %s %[^\n] %f %e %g %lf %ld %u

          

decimal number Octal number Hexa Decimal Number Character String without Space  Input a string until Enter key is pressed including space Fixed Float scientific float General Float double and long double Long Integer Unsigned

Passing data to variables using Literals -

-

A literal is a value used from our side in any assignment or expression o int age=56; o char gender=’M’; Literals can be of different types o Integrals  

Default is int Use l or L with long and u or U with unsigned as suffix • int num=67; • long p=67L; • unsigned n=5U;

o

If a number starts with 0, treated as octal and if a number starts with 0x or 0X treated as Hexa Decimal Floating Literals

o

Default is double • Use f or F with floats • double num=6.7; • float num=6.7f; Character Literals

o

Enclosed in single quotes Characters and numbers are interchangeable • char ch=’A’; • char ch=65;  C provides some special characters called as Escape Sequence Character s  Starts with \ • \t Horizontal Tab (8 spaces) • \n New Line • \r Carriage Return – Move cursor to start of current line • \a or \007 Alert Beap • \\ print \ • \” print “ • \0 NULL Character • \b Backspace String Literals





 

 

Enclosed in double quotes Each string is terminated by NULL character • char name[]="Amit";

Note: use sizeof() operator to see size of a data type or variable

21.08.2010 Turbo C Shortcuts (Block Commands) 1. Selection a. Shift + Arrow keys 2. Copy Text a. Ctrl+KC 3. Move a. Ctrl+KV 4. Delete a. Ctrl+KY

5. De-Select a. Ctrl+KH 6. Undo a. Alt+Backspace 7. Delete a line a. Ctlr+Y Reading data using scanf() function - It can any kind of data using format specifier - Use & (address of operator) to get address of any variable Test Case Write a program to input rollno, name and gender of a student #include #include void main() { int rollno; char gender; char name[51]; clrscr(); printf("Enter rollno : "); scanf("%d",&rollno); printf("Enter Name : "); fflush(stdin); scanf("%[^\n]",&name); printf("Enter Gender [M/F] : "); fflush(stdin); //clear the keyboard buffer scanf("%c",&gender); printf("Roll no is %d, Gender is %c and Name is %s",rollno,gender,name); }

Note: Use fflush(stdin) function to clear the keyboard buffer if required

Input the characters -

Use any of three function as per needs o getchar() o getch() o getche()

/* Getting a character as input */ void main() { char a,b,c; clrscr(); printf("Enter first character : "); a=getchar(); printf("\nEnter second character : "); b=getch();printf("*");

printf("\nEnter third character : "); c=getche(); printf("\nFirst Character is %c Second is %c and third is %c",a,b,c);

}

Reading Strings -

Use gets() function to read the strings o gets(variable) It takes space as well

How to write a program Break the program into three components 1. Input 2. Process 3. Output Input can have one more sub component as variable declaration. Example Write a program to input temperate in degree Celsius and convert into Fahrenheit Note C = 5

F-32 ----9

F=9.0/5 * C + 32

Operators Arithmetic Operators + * / Relational == != > >= <

%

or Comparison operators Equals to Not Equals to Greater than Greater than or equals to Less than

=90 A >=70 B+ >=60 B >=50 F for others #include #include int main() {

char name[51]; float m1,m2,m3,avg; printf("Enter your name : ");gets(name); printf("Enter marks of three subjects : "); scanf("%f%f%f",&m1,&m2,&m3); avg=(m1+m2+m3)/3; if(avg>=90.0f) printf("Grade is A+"); else if(avg>=70.0f) printf("Grade is A"); else if(avg>=60.0f) printf("Grade is B+"); else if(avg>=50.0f) printf("Grade is B"); else printf("Grade is F"); getch(); } Example 4 Get three numbers from keyboard and print the greatest one Method 1: Nested If /* Using Nested If */ #include #include int main() { int a,b,c; printf("Enter three numbers : "); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) printf("%d is greatest one",a); else printf("%d is greatest one",c); } else {

if(b>c) printf("%d is greatest one",b); else printf("%d is greatest one",c); } getch(); } Method 2: If-else ladder /* Using if-else ladder */ #include #include int main() { int a,b,c; printf("Enter three numbers : "); scanf("%d%d%d",&a,&b,&c); if(a>b && a>c) printf("%d is greatest one",a); else if(b>a && b>c) printf("%d is greatest one",b); else printf("%d is greatest one",c); getch(); }

29.08.2010 switch statement -

Used to check equality comparisons only

Syntax switch(variable) { case value1: statements; break; case value2: statements; break; … default: statements; } -

The variable can be of integral or character type only

Test Example Write a program to input two numbers and show a menu 1. Add 2. Substract 3. Product 4. Division 5. Remainder

#include #include int main() { int a,b,choice; printf("Enter two numbers : "); scanf("%d%d",&a,&b); printf("\n\n1: Add"); printf("\n2: Substract"); printf("\n3: Product"); printf("\n\nEnter choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Sum is %d",a+b);break; case 2: printf("Subtraction is %d",a-b);break;

case 3: printf("Product is %d",a*b);break; default: printf("Invalid choice"); } getch(); } Ternary operator It is shortcut to if-else. It has three operations and called as ternary operator Condition? Statement for true : statement for false; Example 1 Write a program to input two numbers and print greater one. g=a>b?a:b; Example 2 Write a program to input three numbers and print greatest one. g=a>b ? (a>c?a:c) : (b>c?b:c); Example 3 Write a program to input basic of an employee and print the DA as per rules basic>=15000 as 85% basic>=10000 as 70% basic >=5000 as 50% else Nil Case 1: using if double da; if(basic>=15000) da=basic*0.85; else if(basic>=10000) da=basic*0.7; else if(basic>=5000) da=basic*.5; else da=0; Case 2: using ternary

da=basic>=15000? basic*.85 : basic>=10000?basic*.7 : basic>=5000? basic*.5 : 0; Full Code /* Input the basic and calculuate DA using ternary operator */ #include #include int main() { int basic; double da; printf("Enter basic : "); scanf("%d",&basic); da=basic>=15000?basic*.85 : basic>=10000?basic*0.7 : basic>=5000? basic*0.5 : 0; printf("DA is %lf",da); getch(); } Assignment Write a program to get three numbers, calculate average of that numbers. Print the grade of the student based on following criteria 90 >= A+ 70 >= B+ 50> = C+ Else Fail Solution #include #include int main() { int x,y,z; float avg; printf("Enter three numbers : "); scanf("%d%d%d",&x,&y,&z); avg=(x+y+z)/3.0; if(avg>=90) printf("Grade is A+"); else if(avg>=70) printf("Grade is B+"); else if(avg>=50)

printf("Grade is C+"); else printf("Grade is Fail"); getch(); } Assignment 2 Get a character from keyboard and check it to alphabet, digit or special character. Input/Process/Output Input: Character (char) char ch; ch=getchar(); Processing : Condition check  if if((ch>=’a’ && ch=’A’ && ch=’0’ && ch='a' && ch='A' && ch='0' && ch
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF