C tutorial

Share Embed Donate


Short Description

Download C tutorial...

Description

C LANGUAGE SYLLABUS 1)Introduction to C 2)Data types 3)Operators 4)Escape sequences 5)If condition 6)For loop 7)While 8)Do While

9)goto 10) Breaks** 11) Continue** 12)Gotoxy Concept* 12) Switch** 13) Array 14) String function** 15) Pointer 16) Function 17) Input Output Function 18) File 19) Structure 20) Scope variable declaration

C LANGUAGE 1

What is program? A program is set of instruction or commands used to solve any task or problem’s program is stored in file. Introduction of C language: 1) C is a structure programming language 2) "C" seems a strange name for a programming language was an offspring of Basic Combined Programming Language. (BCPL) called B. 3) Developed in 1960`s at Cambridge University. Dennis Ritchie modified “B” language. 4) It was implemented at Telephone Bell Laboratories in 1972. The new language name C. Since it was developed along with the Unix Operating System. 5) FORTRAN used for Engineering and Scientific Applications. 6) COBOL used for commercial application 7) Easily interact with hardware 8) C is a case sensitive 9) BCPL -> basic combined programming language is called B language. FEATURES OF C LANGUAGE: 1) C is a middle level Language 2) C is highly portable. That means C program written for one computer can be run on another. 3) C is also support for both high and low level languages.

4) C has an ability to extend itself. A "C" program is basically a collection of function that is supported by the library (Header files). 5) Extension of C file is .C Compiler and Interpreter: 1) Computer can understand only machine level coding. Computer can’t understand our high level programming. So must translate our high level coding into machine level coding. 2) Both compiler and interpreter convert our high level program into machine low level code. 3) It also help to us debug the error in our program. 4) The only difference is compiler compiles entire program at a time and finally list all the errors whereas the execution of an interpreter is line by line. 5) The compiler is faster than the interpreter. C TOKENS: In a passage of text, Individual words and punctuation marks are called tokens. Similarly in c language the smallest individual units are known as TOKENS. 1) Keywords 2) Identifier 3) Constants 4) Strings 5) Operators 6) Special Symbols. To Enter into C language: Start  Programs  Turbo C++ IDE Click 3

Or

Click the icon (Turbo C++ IDE) on the desktop. Or Click the My Computer icon  C Colon TC Folder  Bin Folder  Tc File  click. Shortcut Keys: Meaning

Shortcut key

Window Full screen

ALT+Enter

Working area full screen

F5

Close old program

ALT+F3

Close C software window

ALT+X

Compile

Alt + F9 (Error Checking)

Run

Ctrl + F9

Output

Alt + F5

Save

F2

Open

F3

Trace

F7

Structure of C program: main( )

{ ; . . . } Comment Line: * User understandable code * They are two types 1) Single line 2) Multi line Single line: * Your program titles have single line that time we use single Line comment line. Syntax: // EG: //Addition of two numbers 2) Multiline: * Your program titles have more than one line that time We can use multi line.

Syntax: /* .... ................... .........................*/ 5

EG: /* Addition of two numbers Created by saranya*/ Directives: * Directive means all header files. 1) #include standard input output .header 2) #include Conson input output.header 3) #include Mathematical.header 4) #include Graphical.header 5) #include String.header here string means more than one character. main( ) * main is one of the functions. * used to display program output result. 2It doesn’t end with semicolon. Statements: 3All statements are end with semicolon. printf():* It prints the message or output scanf():-

* Gets the data from user at run time. Program: 1

//To display some text #include #include void main( ) { clrscr(); printf("Welcome to C language "); getch(); } Execution of c program: 1) Write a c program in c software 2) Compile a program --> ALT+F9 3) Link to library or Run -> CTRL+F9 4) To see output --> ALT+F5 4To see output with out using ALT+ F9 that time we can use one header file that name is #include 5clrscr( )  is used to clear previous output . 6getch( )  is used to see output without using ALT+F5

Data types 8 bits 1 byte

7

Data type

Byte and Range

Int

2 byte [-32,768 to 32,769]

Float

4 byte

Char

1 byte

Long int

4 byte

Double

10 byte

Short int

2 byte

Unsigned int

2 byte

-128 to 127

VARIABLE: 7The combination of operand and operator is called variable. Data types: 8Data types are types of data. 9Data means collection of information. 10They are several types of data types 1) Integer 2) Floating points 3) Character 4) Double

Integer: 11It accepts only integer or decimal values only. Syntax:

int ; Eg: int a; a=2345; Float: 12It accepts only floating or point numbers.

Syntax: float ; Eg: float a; Eg: a=45.56; Character: 13Used to accept only single character only. Syntax: char ; Eg: char a; c='z'; 9

Double: 14Used to accept both integer and also floating points. Syntax: double ; Eg: double a; a=10.45; (or) a=123; Printf ( ): 15Used to display output to user. a= 15; Data type

Printf types

Integer

printf("%d",a); 10

Float

printf("%f",a); 10.00

Char

printf("%c",a); c

Double

printf("%d",a); 10.00

String

printf("%s",a); s

Hexa

printf("%x",a);

Octal

printf("%o",a);

Scanf( ): 16Used to get some value or text from user at run time. & Addressof or ampersand Data type

Scanf types

Integer

scanf("%d ",&a);

Float

scanf("%f ",&a);

Char

scanf("%c ",&a);

Double

scanf("%d ",&a);

String

scanf("%s ",&a);

Hexa

scanf("%x ",&a);

Octal

scanf("%o ",&a);

Program: 2

//Eg for data types(printf) #include #include void main() { int i=32000; float f=32.89; double d=10.5; char c='z'; unsigned int u=65000; signed int s=-100; clrscr(); printf("int value is:%d",i); printf("\nfloat value is:%f",f); 11

printf("\ndouble value is:%d",d); printf("\nchar value is:%c",c); printf("\nunsigned int is:%u",u); printf("\nsigned int is:%d",s); getch(); } Program: 3

//program for data types(scanf) #include #include void main() { int a; char n[100]; clrscr(); printf("enter the a value:"); scanf("%d",&a); printf("enter any name:"); scanf("%s",&n); printf("hexa number is:%x",a); printf("\noctal number is:%o",a); printf("\ngiven name is:%s",n); getch(); } OPERATOR: Operator is a Symbol, Which is used to do some arithmetic Operations. 1)Arithmetic Operator: Operator

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulus

2)Relational Operator: Operator

Meaning

>

Greater Than

<

Lesser than

>=

Greater than or equal to

, !=

Not Equal to

3)Logical Operator: Operator

Meaning

&&

AND (Both condition True)

||

OR (Any one condition True)

!

NOT

4)Assignment Operator: Operator

Meaning

+= -= *=

13

/= %=

5)Unary Operator: Operator

Meaning

A++

Post increment

++a a--

Pre increment Post Decrement

--a

Pre Decrement

6)Conditional Operator: *Conditional operator is used to check condition. Syntax: (condition)? : ;

7)Special Operator: Operator

Meaning

{

Left Brace

} (

Right Brace Left Parenthesis

)

Right Parenthesis

[

Left Square Bracket

]

Right Square Bracket

8) Conversion function:

Conversion type

Meaning

%d

Integer

%f

Float

%c

Single Character

%bakiya

String

9) Escape Sequence:

\n

Next line

\t

Horizontal tab

\a

Bell Alert

\r

Carriage return

\b

Back space

\\

Front slash

\/

Back slash

\"

Double Quote

\'

Single Quote

15

Program: 4

//eg for escape sequence #include #include void main() { clrscr(); printf("New line is=david\nbakthakumar"); printf("\nHorizontal tab is=david\tbakthakumar"); printf("\nBellalert is=david\abakthakumar"); getch(); } Program: 5

//eg for arithmetic operator #include #include void main() { .int a,b; clrscr(); printf("enter the a value:"); scanf("%d",&a); printf("enter the b value:"); scanf("%d",&b); printf("\naddition is:%d",(a+b)); printf("\nsubtraction is:%d",(a-b)); printf("\nmultiplication is:%d:",(a*b)); printf("\ndivision is:%d",(a/b)); printf("\nmodulus is:%d",(a%b));

getch(); }

Program:6 //eg for arithmetic operator #include #include void main() { int days,month; printf(“Enter the number of days”); scanf(“%d”,&days); month=days/30; days=days%30; printf(“month=%divya,days=%divya”,month,days); getch(); } Program:7

//eg for Relational Logical operator #include #include void main() { int mon; clrscr(); printrf(“Enter the value of month”); scanf(“%d”,&mon); if(mon==12||mon==1||mon==2) { printf(“Winter”); } else if(mon==3||mon==4||mon==5) { 17

pirntf(“Summer”); } else if(mon==6||mon==7||mon==8) { printf(“Spring”); } else if(mon==9||mon==10||mon==11) { printf(“Autumn”); } else { printf(“Please enter the no between 1 to 12”); } getch(); }

CONDITIONS STATEMENTS: 1If is one of the condition statements. 2In If statements if condition is true then true statements are executed else false statements executed 2At a time only one statements are executed either true or false 3Types of If condition 1. If condition 2. Else if condition 3. Ladder If condition 4. Nested If condition

SYNTAX FOR IF CONDITION:

If () { < True statements>;

} SYNTAX FOR ELSE IF CONDITION:

If () { < True statements>; } else { < False statements>; }

SYNTAX FOR LADDER IF CONDITION:

If () {+ < True statements>; } else if (condition>) { < True statements>; } else if() { < True statements>; } . . . else { < False statements>; } SYNTAX FOR NESTED IF CONDITION:

19

If () { < True statements>; if (condition>) { < True statements>; } else { < False statements>; } } else { ; }

PROGRAM: 8 //To check given number is odd or even #include #include void main() { int a; clrscr(); printf("Enter any value:"); scanf("%d",&a); if((a%2)==0) { printf("Even number"); } else { printf("Odd number");

} getch(); } PROGRAM: 9

//To check given year is leap year or not #include #include void main() { int a; clrscr(); printf("Enter any year:"); scanf("%d",&a); if((a%4)==0)

{ printf("leap year"); } else { printf("not leap year"); } getch(); } PROGRAM: 10

//To check given number positive or negative #include #include void main() { int a; clrscr(); 21

printf("Enter the a value:"); scanf("%d",&a); if(ab) { printf("A is greater"); } else { printf("B is greater"); } getch();

}

PROGRAM:12 //To check which number Pass or fail#include #include void main() { int mark; clrscr(); printf(“Enter the mark”); scanf(“%d”,&mark); if(mark>=80) { printf(“Honour”); } else if(mark=70) { printf(“First class”); } else if(mark=50); { printf(“Second class”); } else { pirntf(“Fail”); } getch(); }

PROGRAM: 13

//To check given number is single or double or triple or four digits

23

#include #include void main() { int a; clrscr(); printf("Enter the a value:"); scanf("%d",&a); if(a=40&&m>=40&&s>=40&&so>=40) { printf("\n%s is pass",n); if(avg>=75) { printf("\nGrade is:Dist"); } else if(avg>=60) { printf("\nGrade is:First"); } else if(avg>=50) { printf("\nGrade is:Second"); } else { printf("\nGrade is:Third"); } } else { printf("\n%s is fail",n); printf("\nGrade is:Nil"); } getch(); }

FOR LOOP: 4Loop is used to execute block of statements continuously until the condition is true. 5It is mainly used to avoid the reputation of coding. 6For is one of the loop statements. 7In for loop they have three functions. 27

1) Assign Starting value 2) Condition 3) Increment or Decrement 8They are two types 4) Normal For loop 5) Nested For loop SYNTAX FOR NORMAL FOR LOOP:

For (=; ; ) { ; } SYNTAX FOR NESTED FOR LOOP:

For (=; ; ) { ; For (=; ; ) { ; } } Program: 17

//To display 1 to 10 by using for loop #include #include void main() { int a; clrscr(); for(a=1;a
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF