C PROGRAMMING BASICS- COMPUTER PROGRAMMING UNIT II
-
Upload
nithyakumaravel -
Category
Engineering
-
view
98 -
download
12
Transcript of C PROGRAMMING BASICS- COMPUTER PROGRAMMING UNIT II
UNIT-II
C PROGRAMMING BASICS
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
CHARACTER SETC CHARACTER SET
Execution Character Set
Source Character Set
Digits
Alphabets
Special
Character
White Space Character
SpaceEscape
Sequence
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
A Character denotes an alphabet, digit or a special character.
Lowercase Letters – A,B,C….. Z
Uppercase Letters - a,b,c…… z
Digits – 0,1,2,3,…7,8,9
+ Plus Sign - Minus Sign , Comma
* Asterisk / Slash = Equal to
@ At Symbol < Less Than : Colon
; Semicolon PREPARED BY K,NITHIYA
AP/ITANNAI CLG OF ENGG
It will work at the execution time only and it cannot be printed.It always represented by a backslash(\) followed by a character.
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
The smallest individual units of a c program are known as tokens
It can be categorized into following ways
C tokens
operatorsSpecial symbolstringconstantsIdentifier
sKeywords
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
Keyword is a reserved keyword that has a particular meaning It should be in lower case There are 32 keywords in c language
S.NO
keyword S.NO
keyword S.NO keyword S.NO
keyword keyword
1 auto 8 do 15 goto 22 signed 29.unsigned
2 break 9 double 16 if 23 sizeof 30.void3 case 1
0else 17 int 24 static 31.volatile
4 char 11
enum 18 long 25 struct 32.while
5 const 12
extern 19 register
26 switch
6 continue
13
float 20 return 27 typedef
7 default 14
for 21 short 28 unionPREPARED BY K,NITHIYA
AP/ITANNAI CLG OF ENGG
It can be a variable name and function name etc. Maximum length of an identifier is 31 Special character are not allowed except(_).
Valid invalidnithiya Nithya#NITHIYA Nith ya ->blank spaceNithya3 AutoNithya 3nithya
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
10-356
4600.5-0.963 ‘A’
‘*’“A”
“CSC”
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
Variables are named locations in memory that are used to hold a value that may be modified by the program.
K5.ico ANIMATAY006.ICO
The syntax for declaring a variable is
Data type Variablename;
Valid Examples :CSC avg_val
m1 Chennai Anumark_1
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
RULES for framing a variable :
The first character in the Variable name must be an alphabet
Alphabets can be followed by a number of digits or
underscores
No commas or Blank spaces are allowed within a variable
name
No Special character other than Underscore(_) can be used in
a variable name.
The variable name should not be a keyword
Variable names are case sensitive
It should not be of length more than 31 charactersPREPARED BY K,NITHIYA
AP/ITANNAI CLG OF ENGG
INTIALIZING VARIABLEAssigning some value to the variable is known as intializationSYNTAX:Data type variable name=value;ExampleInt a=10;
SCOPE OF VARIABLE1.Local variable 2.Global variable
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
Scope of variable
definition example
Local variable
Declared inside the block
main(){Int a=8; // local variable}
Global variable
Declared before the main function
Int a=8;Main(){//no of statement}
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
C data types
Empty data type
User defined data type
Derived or secondary data type
Primary data type
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
PRIMARY DATA TYPES
int float chardouble
1.PRIMARY DATA TYPES
S.NO
Data Type
Explanation Size(bytes) CONTROL STRING
RANGE SYNTAX EXAMPLE
1 int It store numeric values without decimal pointExample:9,10,23
2 bytes %d or %i -32,768 to +32,767
int Variable_name;
int roll_no;
2 float It store the numeric values with decimal pointExample:3.12,47.098
4 bytes %f or %g 3.4E-38 to 3.4E+38
Float Variable_name;
float a;
3 double It store the big number of decimal pointExample:2.13455
8 bytes %if 1.7E-308 to 1.7E+308
Double Variable name;
double b;
4 char It store the single characterExample:’a’,’9’
1 bytes %c -128 to +127 Char Variable name;
char a=“nithiya”;
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
1.Array An array is a collection of variable of same data typeSYNTAX: Datatype array_name[array size];Example: int a[10];2.Structures it stores the multiple values of same or different data type
under a single nameSyntax:Struct <struct name>{Member 1;member 2;};
DERIVED DATA TYPE
array pointersunionsstructure
s
EXAMPLE:Struct Student{Char name[10];Int roll_no;};3.UNION A union is store the multiple values of same or different data typeSYNTAX: union <union name>{Member 1; member 2;}EXAMPLE:Union student{Char name[25];}; PREPARED BY K,NITHIYA
AP/ITANNAI CLG OF ENGG
4.POINTERS A pointer is holds the memory address of another variableSYNTAX:Data type *variable nameEXAMPLE : int *a
EMPTY DATA TYPEi)Void its represents an empty value
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
1.Type Definition
Example: Typedef int age;
age male,female
2.Enumerated Datatype
Example :
enum mon{jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec };
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
USER DEFINED DATA TYPE
Control statement
conditionalunconditiona
l
selective looping
Nested if else
Simple if
If else
If-else if ladder
Switch case
whiledo while
for
gotobreak
continue
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
A program is a sequence of one or more instructions The condition will check whether the particular
condition are true (or) not. 1.SIMPLE IF STATEMENT If the condition is true the set of statements are
executed SYNTAX
if(condition){Statement 1;}
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
To find the given no is even or odd#include<stdio.h>#include<conio.h>main(){int no;clrscr();printf(“\n Enter the number”);scanf(“%d”,&no);if(no%2==0){printf(The given no is even”);}}
OutputEnter the number24The given no is even
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
2.if else statementIt is a two way decision making statementSYNTAX:if(condition){Statement 1;}else{ false statement;}
#include<stdio.h>#include<conio.h>main(){int no;clrscr();printf(“\n Enter the number”);scanf(“%d”,&no);if(no%2==0){printf(The given no is even”);}else{Printf(“The given no is odd”);}
OutputEnter the number
25The given no is odd
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
The number of logical conditions is checked for executing various statements SYNTAX if(condition 1) { True statement 1; } else { if(condition 2) { True statement 2; } else { False statement }}
#include<stdio.h>#include<conio.h>Main(){int a,b,c;clrscr();printf(“Enter a,b,c”);scanf(“%d%d%d”,&a,&b,&c);if((a>b)&&(a>c)){printf(“A is greater”);}else{If(b>c){printf(“B is greater”);}else{printf(“C is greater”);}}}
OutputEnter the values of a,b,c4 23A is greater
Nested if-else can be chained with one another
Syntax:if(condition 1)
{
Statement 1;
}
else if(condition 2);
{
Statement 2;
}
else if(condition 3)
{
statement;
}
else
{
Final statement
}
#include<stdio.h>#include<conio.h>Main(){int a,b,c;clrscr();printf(“Enter a,b,c”);scanf(“%d%d%d”,&a,&b,&c);if(a>b){If(a>c){printf(“A is greater”);}else{printf(“C is greater”);} }else if(b>c){printf(“B is greater”);}else{printf(“C is greater”);}}
OutputEnter the values of a,b,c4 23A is greater
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG
If the value matches with case constant, this particular case statement is executed. if not default is executed.
SYNTAX: Switch (variable) { Case constant 1: statement 1; Break; Case constant 2: Statement 2; Break; .. .. default: Default statement; }
#include<stdio.h>#include<conio.h>Main(){int a=10,b=5;char symbol; clrscr();printf(“Enter the symbol”);scanf(“%c”,&symbol);switch(symbol){case’+’: result=a+b; break;case’-’: result=a-b; break;case’*’: Result=a*b; Break;Case’/’: Result=a/b; break;case’%’: Result=a%b; break;}Default:Printf(“invalid symbol”);}Printf(“the result is %d”,result);}
OutputEnter the symbol:+The result is 15Enter the symbol:%The result is 0
PREPARED BY K,NITHIYA AP/IT
ANNAI CLG OF ENGG