際際滷

際際滷Share a Scribd company logo
Introduction to c
 C is Programming Language
 Developed at AT & Ts Bell Laboratory in 1972
 Designed and Written by Dennis Ritchie
 Replaced more familiar language like PL/I,
ALGOL etc.
 Reliable, Simple and easy to use.
 Nobody can learn directly C++/ Java directly
 C++, C# or JAVA make use of object oriented
programming to organize program
 Major parts of popular OS like windows, Linux
are still written in C
 Common consumer devices like microwave
ovens, washing machines and digital cameras
program part is written in C
 Some of 3D computer games
Introduction to c
Introduction to c
 VARIABLES
 Variables is entity that may change
 Variable is nothing but the name given to the
memory cell
 CONSTANTS
 Constant is an entity that doesnt change
 KEYWORDS
 Reserved Words
 Keywords are the words whose meaning has
already been explained to the C Compiler
 Keywords cannot be used as variable names.
 32 Keyword available in C.
Introduction to c
 Constant can be declared in the way we declare
variable
 But during declaration value need to be
assigned to declared contant name. Its value
cannot be modifeid in the program
 In another way, Constant is nothing but the
value that we assign to variable.
Introduction to c
 An integer constant must have atleast one digit
 It must not have decimal point
 It can be either positive or negative
 If no sign precedes an integer constant, it is assumed
to be positive
 No commas or blanks are allowed within an integer
constant
 The allowable range for integer constant is -32768
to 32767 (assuming 16bit Compiler)
 An integer constant must have atleast one digit
 It must have decimal point
 It can be either positive or negative
 Default sign is positive.
 No commas or blanks are allowed within an
integer constant
 The mantissa part and exponent part should be
separated by a letter e or E
 The mantissa part can be positive or negative
 Default sign for mantissa is positive
 The exponent part must have atleast one digit
which must be a positive or negative integer.
Default sign is positive
 Any single alphabet, single digit or a single
special symbol enclosed within single inverted
commas.
 Both inverted commas should point to left
 Variable name is any combination of 1 to 31
alphabets, digits or underscores.
 The first character in the variable name must be
an alphabet or underscore
 No commas or blanks are allowed within
variable name
 No special symbol other than an underscore
 C compiler expects from user to specify type of
variable user want to use in program
 This declaration is done at the beginning of
program
 Eg. int a, float akc, char code
 Different types of data allowed to be used in C
language
 Primary data types are
o Integer
o Float
o Character
 Some modifiers are allowed in C which gives
multiple sub class of data types
 Such modifiers are like long, short, signed,
unsigned, double, long double etc..
 Character occupies 1 byte memory location
 Example for character variable declaration
 Signed character (Range is -128 to 127)
 Unsigned character (Range is 0 to 255)
 Format specifier is %c.
For 16 bit compiler
2^16=65536
So Range is -32768 to +32767
For 32 bit compiler
2^32=4294967296
So range is -2147483648 to 2147483648
 Integer
 Long
 Short
 Unsigned
 Signed
For 16 bit compiler
Int a; (2byte)
a= 32800
Will not work as it is above 32767
long int a; (4byte)
a= 32800
Will work as it is falls in the range of -2147483648
to 2147483648
 Signed
 Unsigned
 Float occupies 4bytes in memory
-3.4e38 to +3.4e38 (6 digits after decimal point)
 Double occupies 8bytes in memory
-1.7e308 to +1.7e308 (12 digits after decimal point)
 Long double occupies 10bytes in memory
-1.7e4932 to +1.7e4932 (19 digits after decimal
point)
DATA TYPE SIZE RANGE FORMAT
SPECIFIER
int 4byte -2147483648 to 2147483647 %d
unsigned int 4byte 0 to 4294967296 %u
long int/long 4byte -2147483648 to 2147483647 %ld
unsigned long int 4byte 0 to 4294967296 %lu
long long int 8byte -9223372036854775808 To
-9223372036854775807
%lld
short int/short 2byte -32768 to 32767 %hd
unsigned short int 2 byte 0 to 65535 %hu
float 4byte -3.4e38 to 3.4e38 %f
double 8byte -1.7e308 to 1.7e308 %lf
long double 12 byte -1.7e4932 to 1.7e4932 %Lf
char 1byte -128 to 127 %c
unsigned char 1byte 0 to 255 %c
 Assignment Operators
= is an assignment operator
 a=15
 a=b=c=15
 a+=15
 Auto Increment/ Decrement Operators
These operators are ++, --,
 The operator ++ adds 1
 The operator  subtracts 1
 Two types: Prefix and Postfix auto
increment/decrement
 Prefix a=5 b=5
b=++a
After Execution a=6 b=6
 Postfix
a=5 b=5
b=a++
After execution a=6 b=5
 + addition
 - subtraction
 * Multiplication
 / division
 % modulo
 ==
Checks if values of two operands are equal or not.
If yes then condition becomes true
 !=
Checks if values of two operands are equal or not.
If no then condition becomes true
 >
Checks value of left operand is more than right
operand. If yes then condition is true
 <
Checks whether right operand is more than left
operand. If yes then condition is true.
 >=
 <=
 AND &&
 OR ||
 NOT !
 AND and OR operators allow two or more
conditions to be combined in if statement.
 Not operator reverses the result of an
expression it operates on. The output is logical
value i.e. 0 or 1.
 AND &
 OR |
 1s complement operator ~
 XOR operator ^
 Left shift operator <<
 Right shift operator >>
 Also called as Ternary Operator (?:)
Max=a>b?a:b
 One Expression may contain different
operators
 Precedence means the order in which the
operations are carried out for given
expression.
 Associativity comes into picture when
operators of same priority comes into
expression.
Hierarchy of Operators
! Logical Not
* / %
Arithmetic and
modulus
+ - Arithmetic
< > <=
>= Relational
== != Relational
&& Logical AND
|| Logical OR
= Assignment
printf(Text to printed on output screen);
e.g. printf(WELCOME to DKTE);
printf(Text with format specifier, variable list);
e.g. printf(year is %d, y);
printf(Text with format specifier, expression);
e.g. printf(addition is %d, a+4);
 scanf(format specifier, list of variable address);
 E.g
scanf(%d,&a);
scanf(%c,&d);
Introduction to c
Introduction to c
if (condition/expression)
{ statement1;
stetement2;
..
}
 If only one statement present, then there is no
need of curly bracket. But though they are
present there wont be an error.
if (condition/expression)
{ statement1;
statement2;
.
}
else
{
Statement1;
Statement2;
.
}
if(condition/expression)
{ statement1;
. }
elseif(condition/expression)
{ statement1;
.}
elseif(condition/expression)
{statement1;
}
If (condition)
{
if (condition)
{
statement1;
.
}
else
{
statement1
..
}
else
{
Statement1

}
Introduction to c
Syntax
while(condition)
{
statement1;
statement2;

}
 Syntax
do
{ statement1;
Statement2;
} while(condition);
 Syntax
for (initial counter; test counter; increment counter)
{
statement1;
statement2;.
}
switch(integer expression)
{
case constant1:
statement;
case constant2:
statement;
case constant3:
statement;
default:
statement;
}
switch(integer expression)
{
case constant1:
statement;
break;
case constant2:
statement;
break;
case constant3:
statement;
break;
default:
statement;
}
Introduction to c
 Array is a collective name given to group of
memory location containing similar data types.
 Array is a collection of similar elements.
 Similar elements could be all int, or all float or
all char, etc.
 Array of character is called as string.
int marks[30]
int salary[6]= {24,45,56,56,56,76}
int salary[]= {24,45,56,56,56,76}

More Related Content

Introduction to c

  • 2. C is Programming Language Developed at AT & Ts Bell Laboratory in 1972 Designed and Written by Dennis Ritchie Replaced more familiar language like PL/I, ALGOL etc. Reliable, Simple and easy to use.
  • 3. Nobody can learn directly C++/ Java directly C++, C# or JAVA make use of object oriented programming to organize program Major parts of popular OS like windows, Linux are still written in C Common consumer devices like microwave ovens, washing machines and digital cameras program part is written in C Some of 3D computer games
  • 6. VARIABLES Variables is entity that may change Variable is nothing but the name given to the memory cell CONSTANTS Constant is an entity that doesnt change
  • 7. KEYWORDS Reserved Words Keywords are the words whose meaning has already been explained to the C Compiler Keywords cannot be used as variable names. 32 Keyword available in C.
  • 9. Constant can be declared in the way we declare variable But during declaration value need to be assigned to declared contant name. Its value cannot be modifeid in the program In another way, Constant is nothing but the value that we assign to variable.
  • 11. An integer constant must have atleast one digit It must not have decimal point It can be either positive or negative If no sign precedes an integer constant, it is assumed to be positive No commas or blanks are allowed within an integer constant The allowable range for integer constant is -32768 to 32767 (assuming 16bit Compiler)
  • 12. An integer constant must have atleast one digit It must have decimal point It can be either positive or negative Default sign is positive. No commas or blanks are allowed within an integer constant
  • 13. The mantissa part and exponent part should be separated by a letter e or E The mantissa part can be positive or negative Default sign for mantissa is positive The exponent part must have atleast one digit which must be a positive or negative integer. Default sign is positive
  • 14. Any single alphabet, single digit or a single special symbol enclosed within single inverted commas. Both inverted commas should point to left
  • 15. Variable name is any combination of 1 to 31 alphabets, digits or underscores. The first character in the variable name must be an alphabet or underscore No commas or blanks are allowed within variable name No special symbol other than an underscore
  • 16. C compiler expects from user to specify type of variable user want to use in program This declaration is done at the beginning of program Eg. int a, float akc, char code
  • 17. Different types of data allowed to be used in C language Primary data types are o Integer o Float o Character Some modifiers are allowed in C which gives multiple sub class of data types Such modifiers are like long, short, signed, unsigned, double, long double etc..
  • 18. Character occupies 1 byte memory location Example for character variable declaration Signed character (Range is -128 to 127) Unsigned character (Range is 0 to 255) Format specifier is %c.
  • 19. For 16 bit compiler 2^16=65536 So Range is -32768 to +32767 For 32 bit compiler 2^32=4294967296 So range is -2147483648 to 2147483648
  • 20. Integer Long Short Unsigned Signed For 16 bit compiler Int a; (2byte) a= 32800 Will not work as it is above 32767
  • 21. long int a; (4byte) a= 32800 Will work as it is falls in the range of -2147483648 to 2147483648
  • 23. Float occupies 4bytes in memory -3.4e38 to +3.4e38 (6 digits after decimal point) Double occupies 8bytes in memory -1.7e308 to +1.7e308 (12 digits after decimal point) Long double occupies 10bytes in memory -1.7e4932 to +1.7e4932 (19 digits after decimal point)
  • 24. DATA TYPE SIZE RANGE FORMAT SPECIFIER int 4byte -2147483648 to 2147483647 %d unsigned int 4byte 0 to 4294967296 %u long int/long 4byte -2147483648 to 2147483647 %ld unsigned long int 4byte 0 to 4294967296 %lu long long int 8byte -9223372036854775808 To -9223372036854775807 %lld short int/short 2byte -32768 to 32767 %hd unsigned short int 2 byte 0 to 65535 %hu float 4byte -3.4e38 to 3.4e38 %f double 8byte -1.7e308 to 1.7e308 %lf long double 12 byte -1.7e4932 to 1.7e4932 %Lf char 1byte -128 to 127 %c unsigned char 1byte 0 to 255 %c
  • 25. Assignment Operators = is an assignment operator a=15 a=b=c=15 a+=15
  • 26. Auto Increment/ Decrement Operators These operators are ++, --, The operator ++ adds 1 The operator subtracts 1 Two types: Prefix and Postfix auto increment/decrement Prefix a=5 b=5 b=++a After Execution a=6 b=6
  • 27. Postfix a=5 b=5 b=a++ After execution a=6 b=5
  • 28. + addition - subtraction * Multiplication / division % modulo
  • 29. == Checks if values of two operands are equal or not. If yes then condition becomes true != Checks if values of two operands are equal or not. If no then condition becomes true > Checks value of left operand is more than right operand. If yes then condition is true
  • 30. < Checks whether right operand is more than left operand. If yes then condition is true. >= <=
  • 31. AND && OR || NOT ! AND and OR operators allow two or more conditions to be combined in if statement. Not operator reverses the result of an expression it operates on. The output is logical value i.e. 0 or 1.
  • 32. AND & OR | 1s complement operator ~ XOR operator ^ Left shift operator << Right shift operator >>
  • 33. Also called as Ternary Operator (?:) Max=a>b?a:b
  • 34. One Expression may contain different operators Precedence means the order in which the operations are carried out for given expression. Associativity comes into picture when operators of same priority comes into expression.
  • 35. Hierarchy of Operators ! Logical Not * / % Arithmetic and modulus + - Arithmetic < > <= >= Relational == != Relational && Logical AND || Logical OR = Assignment
  • 36. printf(Text to printed on output screen); e.g. printf(WELCOME to DKTE); printf(Text with format specifier, variable list); e.g. printf(year is %d, y); printf(Text with format specifier, expression); e.g. printf(addition is %d, a+4);
  • 37. scanf(format specifier, list of variable address); E.g scanf(%d,&a); scanf(%c,&d);
  • 40. if (condition/expression) { statement1; stetement2; .. } If only one statement present, then there is no need of curly bracket. But though they are present there wont be an error.
  • 42. if(condition/expression) { statement1; . } elseif(condition/expression) { statement1; .} elseif(condition/expression) {statement1; }
  • 47. Syntax for (initial counter; test counter; increment counter) { statement1; statement2;. }
  • 48. switch(integer expression) { case constant1: statement; case constant2: statement; case constant3: statement; default: statement; }
  • 49. switch(integer expression) { case constant1: statement; break; case constant2: statement; break; case constant3: statement; break; default: statement; }
  • 51. Array is a collective name given to group of memory location containing similar data types. Array is a collection of similar elements. Similar elements could be all int, or all float or all char, etc. Array of character is called as string.
  • 52. int marks[30] int salary[6]= {24,45,56,56,56,76} int salary[]= {24,45,56,56,56,76}

Editor's Notes