Mangalayatan University
Faculty Name :- Prof. LALIT MOHAN GUPTA
Course Name :- Programming with ‘C’
Course Code :- CSM – 6151
Semester :- 1
Class No. :- 3
Block No. :- 2
UNIT No. :- 5,6
9/21/2024 CSM – 6151 Programming with "C" 1
Mangalayatan University
C Tokens
C Character Set
Features of C
C components
Introduction to C Programming
9/21/2024
CSM – 6151 Programming with "C"
2
Introduction to C Programming & components
Mangalayatan University
High Level Programming Language with some low level features.
Stable language.
Simple Language.
Large Number of Operators.
Less Number of Keywords (only 32 keywords)
Supports pointers to refer computer memory, arrays, structures and
functions.
Structured Programming Language.
Extensible Language
9/21/2024
CSM – 6151 Programming with "C"
3
Features of C
Mangalayatan University
The Character set (a-z, A-Z, 0-9, {, }…)
The data types (int, char, float, double, array,
struct, union)
Constants (24, 45.67, ‘X’, “Hello”)
Variables (a, b)
Keywords (int, for, if, else, break)
9/21/2024
CSM – 6151 Programming with "C"
4
Components of C Language
Mangalayatan University
Alphabets (A,..,Z,a,..,z)
Digits(0-9)
Special
Symbols(@,.,;,:)
White
Spaces(n,b)
Operators(+,-,/,%)
9/21/2024
CSM – 6151 Programming with "C"
5
C-Character Set
Mangalayatan University
Tokens are
the basic
building
blocks in C
language.
It is the
smallest
individual
unit in the C
program.
C program
is
constructed
by using
combination
s of these
tokens.
C token can
be
categorized
in five
forms:
Keywords
Identifiers
Constants
Operators
Special
symbols
9/21/2024
CSM – 6151 Programming with "C"
6
C Tokens
Mangalayatan University
Keywords
have
predefined
meaning
and they
are used to
perform
specific
task.
Also known
as Reserve
words.
Have fixed
meaning.
Always
written in
lowercase.
32 keywords
in C
programming
9/21/2024
CSM – 6151 Programming with "C"
7
Keywords
Mangalayatan University
9/21/2024
CSM – 6151 Programming with "C"
8
Keywords
auto continue double float int short struct unsigned
break const else for long signed switch void
case default enum goto register sizeof typedef volatile
char do extern if return static union while
Mangalayatan University
Identifiers are used to name various program elements such as
variables, functions and arrays.
Variable is the named memory location that is used to hold a
value that can be modified during the execution of the program.
Declaration of variable:
• datatype var_name;
• e.g. int x;
9/21/2024
CSM – 6151 Programming with "C"
9
Identifiers
Mangalayatan University
May consist of alphabets, digits and underscore ( _ ).
Must not start with a digit.
Keywords can not be used.
White spaces are not allowed.
Only first 31 characters are significant.
Examples: sum, a, x2, first_name, _SI.
Some Invalid identifiers
Simple interest,@abc,12_dfr etc.
9/21/2024
CSM – 6151 Programming with "C"
10
Rules for Naming Identifiers
Mangalayatan University
Constants are
values that
can’t change
during the
execution.
known as
literals.
Types of
constants:
Character
constants
Integer
constants
Floating
constants
Strings
constants
9/21/2024
CSM – 6151 Programming with "C"
11
Constants
Mangalayatan University
A constant of
integer type
consists of
sequence of
digits.
For example :
1, 234, 567, -
4563.
By default
integer literal
are of type int.
Wrong
representation
• 123 456
• 12,34,567
• $234
9/21/2024
CSM – 6151 Programming with "C"
12
Integer Constants
Mangalayatan University
A floating constant
consists of an
integer part, a
decimal point, a
fractional part and
exponent field
containing an e or
E followed by an
integer.
The
Fraction
part and
integer
part are
sequence
of digits.
For
example
0.02, -
23.567, -
4.57E-
23,
3.4e+25,
.98
By
default
floating
literal
are
double.
Invalid
real
constants
: 9.88.7,
5.6E 23,
• 3.4E9.
4
9/21/2024
CSM – 6151 Programming with "C"
13
Real or Floating Constants(CO1)
Mangalayatan University
Character constants
A character constant consists of a single character enclosed in
single quotes.
For example ‘a’ , ‘D’ and ‘@’.
Characters are stored using machine characters sets using ASCII
codes.
All escapes sequences are also characters sequences.
String constants
A string constant is a sequence of characters enclosed in double
quotes.
For example “a”, “ABC”.
9/21/2024
CSM – 6151 Programming with "C"
14
Character constants
Mangalayatan University
An escape sequence is a sequence of characters that does not
represent itself when used inside a character or string literal, but
is translated into another character.
used in formatting the output in output functions.
It represents only one character. However, it consists of two
characters.
9/21/2024
CSM – 6151 Programming with "C"
15
Backslash Character Constants
Mangalayatan University
Constants Hexadecimal Value Meaning
a 07 audible alert (bell sound)
b 08 back space
n 0A new line
r 0D carriage return
f 0C form feed
t 09 horizontal tab
v 0B vertical tab
’ 27 single quote
’’ 22 double quote
? 3F question mark
 5C backslash
nnn any octal number
xhh Any hexadecimal number
9/21/2024
CSM – 6151 Programming with "C"
16
Backslash Character Constants
Mangalayatan University
Every combination starts with back slash()
They are non-printing characters.
It can also be expressed in terms of octal digits or hexadecimal
sequence.
Each escape sequence has unique ASCII value.
9/21/2024
CSM – 6151 Programming with "C"
17
Characteristics of escape sequences
Mangalayatan University
Documentation Section
Preprocessor Directives
Global Declaration Section
void main()
{
Local Declaration Section
Statements
}
Other functions as required
9/21/2024
CSM – 6151 Programming with "C"
18
Structure of C program
Mangalayatan University
/*C Program to Display Welcome message*/
#include<stdio.h>
#include<conio.h>
void main()
{
printf(“Welcome to the world of C.”);
getch();
}
9/21/2024
CSM – 6151 Programming with "C"
19
First C program
Documentation Section
Preprocessor Directives
Statements
Welcome to the world of C.
Mangalayatan University
STRUCTURE (LAYOUT) OF C PROGRAM
// Sample of C Program(documentation Section)
#inclufe<stdio.h> // link section
#include<conio.h> // link section
#define pi 3.14 // definition section
int a=10; // global variable declaration
void disp(); // global function declaration
void main() // main function definition
{
float area,r;
printf(“enter radius”);
scanf(“%f”,&r);
area=pi*r*r;
printf(“area=%f”,area);
disp();
getch();
}
20
9/21/2024 CSM – 6151 Programming with "C" 20
Mangalayatan University
STRUCTURE (LAYOUT) OF C PROGRAM
Example
void disp() // user defined function definition
{
printf(“hello”);
}
l 21
9/21/2024 CSM – 6151 Programming with "C" 21
Mangalayatan University
Pre-processor works under the control of set of instructions,
called preprocessor directives.
These are executed before the compilation process.
These statements begin with # symbol.
Examples:
• #define
• #include
9/21/2024
CSM – 6151 Programming with "C"
22
Pre-processor Directives
Mangalayatan University
A data type is used to declare variables that specifies
• Size of memory allocated to variables.
• Define the range of allowed values.
• Operations that can be performed on those values.
Storage representation of different data types is different in
memory.
9/21/2024
CSM – 6151 Programming with "C"
23
Data Types(CO1)
Mangalayatan University
ANSI C supports
Three classes of
Data Types
Primary or
fundamental or
primitive or built-in
or basic data types.
int , char, float , void
Derived data types
array, function,
pointer
User Defined data
types
enum, typedef
,structure,
union
9/21/2024
CSM – 6151 Programming with "C"
24
Data Type
Mangalayatan University
9/21/2024
CSM – 6151 Programming with "C"
25
Data Type
char
int
float
void
unsigned
signed
short
int
long
float
double
long double
1 Byte
1 Byte
2 Bytes
2/4 Bytes
4 Bytes
4 Bytes
8 Bytes
10 Bytes
Mangalayatan University
Data type modifiers are
keywords used to
change the current
properties of data
types.
Type for
Data
modifiers
:
size
Modifiers
short
long
sign
Modifiers
signed
unsigned
9/21/2024
CSM – 6151 Programming with "C"
26
Data Modifiers
Mangalayatan University
Basic
DATA
TYPE
DATA TYPE with
type modifier
FORMAT
SPECIFIE
R
SIZE RANGE
char
char
signed char
%c 1 Byte -128 to 127
unsigned char %c 1 Byte 0 to 255
int
int
signed int
short
short int
Signed short int
%d
%i
%hi
2 Byte
Or
4 Byte
-32768 to 32767
Or
-2,147,483,648 to
2147,483,647
unsigned
unsigned int
Unsigned short int
%u
%hu
2 Byte
Or
4 Byte
0 to 65535
Or
0 to 4,294,967,295
9/21/2024
CSM – 6151 Programming with "C"
27
Primary Data Types(CO1)
Mangalayatan University
Basic
DATA
TYPE
DATA TYPE with type
modifier
FORMAT
SPECIFI
ER
SIZE RANGE
int
long
long int
signed long int
%ld
%li
4 Byte -2,147,483,648 to
2147,483,647
unsigned long
Unsigned long int
%lu 4 Byte 0 to 4,294,967,295
float
float %f
%e or %E
%g or %G
4 Byte +(1.17*10-38 to 3.4*1038 )
And
-(1.17*1038 to 3.4*1038 )
double
double %lf 8 Byte +(2.22*10-308 to 1.79*10308)
And
-(2.22*10308 to 1.79*10308)
long double %Lf 10 Byte +(3.4*10-4932 to 1.1*104932)
And
-(3.4*10-4932 to 1.1*104932)
void -- -- -- --
9/21/2024 CSM – 6151 Programming with "C" 28
Primary Data Types
Mangalayatan University
Type casting is a way to convert a variable from one data type to
another data type. For example, if you want to store a long value
into a simple integer then you can type cast long to int.
There are two types of Type Casting
• Implicit type conversion
• Explicit type conversion
9/21/2024
CSM – 6151 Programming with "C"
29
Type Casting or Type Conversion(CO1)
Mangalayatan University
In this the data
type/Variable of
lower type is
converted to a
higher type.
This automatic
conversion is
known as
implicit type
conversion or
promotion.
9/21/2024
CSM – 6151 Programming with "C"
30
Implicit Type Conversion
Mangalayatan University
When we want to convert a type forcibly in a way that is
different from automatic type conversion, we need to go for
explicit type conversion.
Lower to higher and higher to lower both type conversion can be
done in type casting.
Syntax
• (type name) expression;
• For e.g.
• x = (float)y;
• Where x is of type float and y is of type int.
9/21/2024
CSM – 6151 Programming with "C"
31
Explicit Type Conversion
Mangalayatan University
• 8.5 is converted to integer 8 by
truncation
x=(int) 8.5
• Evaluated as 23/7 and the result
would be 3
a = (int) 23.6/(int)7.2
• Division is done in floating point
mode
b= (double)sum/n
• Result of a+b is converted to
intger.
c= (int)(a+b)
• a is converted to integer and then
added to b.
p= (int)a+b
9/21/2024
CSM – 6151 Programming with "C"
32
Explicit Type Conversion or Type Casting
Mangalayatan University
Standard input or stdin is used for taking input from devices
such as the keyboard as a data stream
Standard output or stdout is used for giving output to a device
such as a monitor.
For using I/O functionality, programmers must
include stdio header-file within the program.
9/21/2024 CSM – 6151 Programming with "C" 33
Standard I/O
Mangalayatan University
Reading Character from the keyboard:
• getchar() function can be used to read a single character
Syntax:
var_name = getchar();
Example:
9/21/2024 CSM – 6151 Programming with "C" 34
Standard I/O
#include<stdio.h>
void main()
{
char title;
title = getchar();
}
Mangalayatan University
Writing Character to the monitor
• putchar() function can be used to display a single character on
the screen
Syntax:
putchar(var_name);
Example:
9/21/2024 CSM – 6151 Programming with "C" 35
Standard I/O
#include<stdio.h>
void main()
{
char title =‘t’;
putchar(title);
}
Mangalayatan University
Formatted input function takes an input data a specific format. It
allows by scanf() function
Syntax:
scanf(“control string”,&arg1,&arg2..);
Example:
9/21/2024 CSM – 6151 Programming with "C" 36
Standard I/O
#include<stdio.h>
void main() {
int var1;
scanf("%d ", &var1,
);
}
Mangalayatan University
Formatted output function displays a data in a specific format
on the screen. It allows by printf() function
Syntax:
printf(“control string”,arg1,arg2..);
Example:
9/21/2024 CSM – 6151 Programming with "C" 37
Standard I/O
#include<stdio.h>
void main() {
int var1= 60;
printf("%d ", var1, );
}
Mangalayatan University
There are three type
of errors can occur in
the c Programming.
Syntax
Error
Occurs
due to
incorrect
syntax
used in C-
statements.
Found at
compile
time
Logical
Error
Occurs
due to
incorrect
logic used
in C –
statements
by user.
Found
after
execution
i.e may
give
incorrect
output
Runtime
Error
Code links and
compiles just
fine, but during
execution, it
does something
illegal.
9/21/2024 CSM – 6151 Programming with "C" 38
Errors
Mangalayatan University
Examples
Syntax Error
void main()
{
printf(“ value of x
%d”,x):
/* x is not declared and
semicolon missing*/
}
Logical Error
floataverage(floata,floatb)
{
return a+b/2;
/* should be (a + b) / 2 */
}
Runtime
Error
int a;
printf("type an integer");
scanf("%d", a);.
9/21/2024 CSM – 6151 Programming with "C" 39
Errors
Mangalayatan University
Source code
• The input program
of compiler is known
as source code.
• It can be in any high
level programming
language
• It is human readable
code.
Object code
• The output program
of compiler is known
as object code
• It is machine
language code.
• It is machine
readable code
Executable Code
• Executable code is
generated after
linking all the object
module after
compilation.
9/21/2024 CSM – 6151 Programming with "C" 40
Object Code and Executable Code
Mangalayatan University
9/21/2024 41
Translation of Codes
CSM – 6151 Programming with "C"
Mangalayatan University
A variable is a name which is assigned to a data value at storage
location in computer’s memory.
Variables are used to store various kinds of data during program
executions.
Variables name assigned before it use and it can be letter , digit
and an underscore(_).
9/21/2024 42
Variables and Memory allocation
CSM – 6151 Programming with "C"
Mangalayatan University
• Syntax:
• <data type> <variable Name>;
Declaration:
• int num;
• char name;
Example:
• Two type of initialization
Initialization:
• int num=10; //at time of declaration
Example1:
• int num;
• num=10; // after the declaration
Example2:
9/21/2024 CSM – 6151 Programming with "C" 43
Declaration and Initialization
Mangalayatan University
Write a program of find area of circle
Write a program of find area of triangle.
Write a program of find swap two numbers using third variable
Write a program of swap two numbers without using third variable
Write a program of swap two numbers without using third variable
Write a program of find area of circle.
Write a program of find area of triangle.
Write a program of find swap two numbers using third variable.
Write a program of swap two numbers without using third
variable
9/21/2024 CSM – 6151 Programming with "C" 44
Programs
Mangalayatan University
/* Area of Circle*/
#include<stdio.h>
#include<conio.h>
void main()
{
float r, area;
clrscr();
printf(“enter radiusn”);
scanf(“%f”,&r);
area=22*r*r/7;
printf(“area of circle is=%f”,area);
getch();
}
9/21/2024 CSM – 6151 Programming with "C" 45
Program
OUTPUT:
Enter radius:
2
area of circle is =6.28
Mangalayatan University
/* Area of Triangle*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c, area,s;
clrscr();
printf(“Enter sides of trianglen”);
scanf(“%f%f%f”, &a, &b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“area of Triangle is=%f”,area);
getch();
}
9/21/2024 CSM – 6151 Programming with "C" 46
Program
OUTPUT:
Enter sides of triangle:
3
3
2
area of triangle is = 0.44
Mangalayatan University
/* Swap Two Numbers using third variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf (“enter two numbersn”);
scanf (“%d %d ” ,&a ,&b );
temp=a;
a=b;
b=temp;
printf(“After swap numbers are:n a=%dnb=%d”,a,b);
getch();
}
9/21/2024 CSM – 6151 Programming with "C" 47
Program
OUTPUT:
Enter two numbers
20
30
After swap numbers are:
a=30
b=20
Mangalayatan University
/* Swap Two Numbers without using third variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf (“enter two numbersn”);
scanf (“%d %d ” ,&a ,&b );
a=a+b;
b=a-b;
a=a-b;
printf(“After swap numbers are:n a=%dnb=%d”,a,b);
getch();
}
9/21/2024 CSM – 6151 Programming with "C" 48
Program(CO1)
OUTPUT:
Enter two numbers
20
30
After swap numbers are:
a=30
b=20
Mangalayatan University
 In C-programming what will be the value of r if r=p%q where p=-17 and q=5?
[MTU 2011-12 (Even) Marks 2]
 What is the value of 2^3? [GBTU 2011-12 (Odd) Marks 2]
 Write a program in C to find the greatest number among three numbers using
conditional operator. [GBTU 2013-14 (Odd) Marks 5]
 What will be the output of the following code? [GBTU 2011-12 (Odd) Marks 2]
void main( )
{
int a=5, b=6;
printf(“%dt”,a=b);
printf(“%dt”,a==b);
printf(“%dt%d”,a,b);
}
9/21/2024
CSM – 6151 Programming with "C"
49
Weekly Assignment
Mangalayatan University
 Data types
 Structure of C Program
 Basic of C program
9/21/2024 CSM – 6151 Programming with "C" 50
Prerequisite and Recap(CO1)
Mangalayatan University
 Data types
• int , char , float , double and void
 Structure of C Program
• Documentation section
• Preprocessor Directive section
• Global section
• Void main()
 Standard I/O
• printf()
• scanf()
9/21/2024 CSM – 6151 Programming with "C" 51
Prerequisite and Recap
Mangalayatan University
Thank You
9/21/2024 CSM – 6151 Programming with "C" 52

C Programming Language (Features and Components)

  • 1.
    Mangalayatan University Faculty Name:- Prof. LALIT MOHAN GUPTA Course Name :- Programming with ‘C’ Course Code :- CSM – 6151 Semester :- 1 Class No. :- 3 Block No. :- 2 UNIT No. :- 5,6 9/21/2024 CSM – 6151 Programming with "C" 1
  • 2.
    Mangalayatan University C Tokens CCharacter Set Features of C C components Introduction to C Programming 9/21/2024 CSM – 6151 Programming with "C" 2 Introduction to C Programming & components
  • 3.
    Mangalayatan University High LevelProgramming Language with some low level features. Stable language. Simple Language. Large Number of Operators. Less Number of Keywords (only 32 keywords) Supports pointers to refer computer memory, arrays, structures and functions. Structured Programming Language. Extensible Language 9/21/2024 CSM – 6151 Programming with "C" 3 Features of C
  • 4.
    Mangalayatan University The Characterset (a-z, A-Z, 0-9, {, }…) The data types (int, char, float, double, array, struct, union) Constants (24, 45.67, ‘X’, “Hello”) Variables (a, b) Keywords (int, for, if, else, break) 9/21/2024 CSM – 6151 Programming with "C" 4 Components of C Language
  • 5.
  • 6.
    Mangalayatan University Tokens are thebasic building blocks in C language. It is the smallest individual unit in the C program. C program is constructed by using combination s of these tokens. C token can be categorized in five forms: Keywords Identifiers Constants Operators Special symbols 9/21/2024 CSM – 6151 Programming with "C" 6 C Tokens
  • 7.
    Mangalayatan University Keywords have predefined meaning and they areused to perform specific task. Also known as Reserve words. Have fixed meaning. Always written in lowercase. 32 keywords in C programming 9/21/2024 CSM – 6151 Programming with "C" 7 Keywords
  • 8.
    Mangalayatan University 9/21/2024 CSM –6151 Programming with "C" 8 Keywords auto continue double float int short struct unsigned break const else for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while
  • 9.
    Mangalayatan University Identifiers areused to name various program elements such as variables, functions and arrays. Variable is the named memory location that is used to hold a value that can be modified during the execution of the program. Declaration of variable: • datatype var_name; • e.g. int x; 9/21/2024 CSM – 6151 Programming with "C" 9 Identifiers
  • 10.
    Mangalayatan University May consistof alphabets, digits and underscore ( _ ). Must not start with a digit. Keywords can not be used. White spaces are not allowed. Only first 31 characters are significant. Examples: sum, a, x2, first_name, _SI. Some Invalid identifiers Simple interest,@abc,12_dfr etc. 9/21/2024 CSM – 6151 Programming with "C" 10 Rules for Naming Identifiers
  • 11.
    Mangalayatan University Constants are valuesthat can’t change during the execution. known as literals. Types of constants: Character constants Integer constants Floating constants Strings constants 9/21/2024 CSM – 6151 Programming with "C" 11 Constants
  • 12.
    Mangalayatan University A constantof integer type consists of sequence of digits. For example : 1, 234, 567, - 4563. By default integer literal are of type int. Wrong representation • 123 456 • 12,34,567 • $234 9/21/2024 CSM – 6151 Programming with "C" 12 Integer Constants
  • 13.
    Mangalayatan University A floatingconstant consists of an integer part, a decimal point, a fractional part and exponent field containing an e or E followed by an integer. The Fraction part and integer part are sequence of digits. For example 0.02, - 23.567, - 4.57E- 23, 3.4e+25, .98 By default floating literal are double. Invalid real constants : 9.88.7, 5.6E 23, • 3.4E9. 4 9/21/2024 CSM – 6151 Programming with "C" 13 Real or Floating Constants(CO1)
  • 14.
    Mangalayatan University Character constants Acharacter constant consists of a single character enclosed in single quotes. For example ‘a’ , ‘D’ and ‘@’. Characters are stored using machine characters sets using ASCII codes. All escapes sequences are also characters sequences. String constants A string constant is a sequence of characters enclosed in double quotes. For example “a”, “ABC”. 9/21/2024 CSM – 6151 Programming with "C" 14 Character constants
  • 15.
    Mangalayatan University An escapesequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character. used in formatting the output in output functions. It represents only one character. However, it consists of two characters. 9/21/2024 CSM – 6151 Programming with "C" 15 Backslash Character Constants
  • 16.
    Mangalayatan University Constants HexadecimalValue Meaning a 07 audible alert (bell sound) b 08 back space n 0A new line r 0D carriage return f 0C form feed t 09 horizontal tab v 0B vertical tab ’ 27 single quote ’’ 22 double quote ? 3F question mark 5C backslash nnn any octal number xhh Any hexadecimal number 9/21/2024 CSM – 6151 Programming with "C" 16 Backslash Character Constants
  • 17.
    Mangalayatan University Every combinationstarts with back slash() They are non-printing characters. It can also be expressed in terms of octal digits or hexadecimal sequence. Each escape sequence has unique ASCII value. 9/21/2024 CSM – 6151 Programming with "C" 17 Characteristics of escape sequences
  • 18.
    Mangalayatan University Documentation Section PreprocessorDirectives Global Declaration Section void main() { Local Declaration Section Statements } Other functions as required 9/21/2024 CSM – 6151 Programming with "C" 18 Structure of C program
  • 19.
    Mangalayatan University /*C Programto Display Welcome message*/ #include<stdio.h> #include<conio.h> void main() { printf(“Welcome to the world of C.”); getch(); } 9/21/2024 CSM – 6151 Programming with "C" 19 First C program Documentation Section Preprocessor Directives Statements Welcome to the world of C.
  • 20.
    Mangalayatan University STRUCTURE (LAYOUT)OF C PROGRAM // Sample of C Program(documentation Section) #inclufe<stdio.h> // link section #include<conio.h> // link section #define pi 3.14 // definition section int a=10; // global variable declaration void disp(); // global function declaration void main() // main function definition { float area,r; printf(“enter radius”); scanf(“%f”,&r); area=pi*r*r; printf(“area=%f”,area); disp(); getch(); } 20 9/21/2024 CSM – 6151 Programming with "C" 20
  • 21.
    Mangalayatan University STRUCTURE (LAYOUT)OF C PROGRAM Example void disp() // user defined function definition { printf(“hello”); } l 21 9/21/2024 CSM – 6151 Programming with "C" 21
  • 22.
    Mangalayatan University Pre-processor worksunder the control of set of instructions, called preprocessor directives. These are executed before the compilation process. These statements begin with # symbol. Examples: • #define • #include 9/21/2024 CSM – 6151 Programming with "C" 22 Pre-processor Directives
  • 23.
    Mangalayatan University A datatype is used to declare variables that specifies • Size of memory allocated to variables. • Define the range of allowed values. • Operations that can be performed on those values. Storage representation of different data types is different in memory. 9/21/2024 CSM – 6151 Programming with "C" 23 Data Types(CO1)
  • 24.
    Mangalayatan University ANSI Csupports Three classes of Data Types Primary or fundamental or primitive or built-in or basic data types. int , char, float , void Derived data types array, function, pointer User Defined data types enum, typedef ,structure, union 9/21/2024 CSM – 6151 Programming with "C" 24 Data Type
  • 25.
    Mangalayatan University 9/21/2024 CSM –6151 Programming with "C" 25 Data Type char int float void unsigned signed short int long float double long double 1 Byte 1 Byte 2 Bytes 2/4 Bytes 4 Bytes 4 Bytes 8 Bytes 10 Bytes
  • 26.
    Mangalayatan University Data typemodifiers are keywords used to change the current properties of data types. Type for Data modifiers : size Modifiers short long sign Modifiers signed unsigned 9/21/2024 CSM – 6151 Programming with "C" 26 Data Modifiers
  • 27.
    Mangalayatan University Basic DATA TYPE DATA TYPEwith type modifier FORMAT SPECIFIE R SIZE RANGE char char signed char %c 1 Byte -128 to 127 unsigned char %c 1 Byte 0 to 255 int int signed int short short int Signed short int %d %i %hi 2 Byte Or 4 Byte -32768 to 32767 Or -2,147,483,648 to 2147,483,647 unsigned unsigned int Unsigned short int %u %hu 2 Byte Or 4 Byte 0 to 65535 Or 0 to 4,294,967,295 9/21/2024 CSM – 6151 Programming with "C" 27 Primary Data Types(CO1)
  • 28.
    Mangalayatan University Basic DATA TYPE DATA TYPEwith type modifier FORMAT SPECIFI ER SIZE RANGE int long long int signed long int %ld %li 4 Byte -2,147,483,648 to 2147,483,647 unsigned long Unsigned long int %lu 4 Byte 0 to 4,294,967,295 float float %f %e or %E %g or %G 4 Byte +(1.17*10-38 to 3.4*1038 ) And -(1.17*1038 to 3.4*1038 ) double double %lf 8 Byte +(2.22*10-308 to 1.79*10308) And -(2.22*10308 to 1.79*10308) long double %Lf 10 Byte +(3.4*10-4932 to 1.1*104932) And -(3.4*10-4932 to 1.1*104932) void -- -- -- -- 9/21/2024 CSM – 6151 Programming with "C" 28 Primary Data Types
  • 29.
    Mangalayatan University Type castingis a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. There are two types of Type Casting • Implicit type conversion • Explicit type conversion 9/21/2024 CSM – 6151 Programming with "C" 29 Type Casting or Type Conversion(CO1)
  • 30.
    Mangalayatan University In thisthe data type/Variable of lower type is converted to a higher type. This automatic conversion is known as implicit type conversion or promotion. 9/21/2024 CSM – 6151 Programming with "C" 30 Implicit Type Conversion
  • 31.
    Mangalayatan University When wewant to convert a type forcibly in a way that is different from automatic type conversion, we need to go for explicit type conversion. Lower to higher and higher to lower both type conversion can be done in type casting. Syntax • (type name) expression; • For e.g. • x = (float)y; • Where x is of type float and y is of type int. 9/21/2024 CSM – 6151 Programming with "C" 31 Explicit Type Conversion
  • 32.
    Mangalayatan University • 8.5is converted to integer 8 by truncation x=(int) 8.5 • Evaluated as 23/7 and the result would be 3 a = (int) 23.6/(int)7.2 • Division is done in floating point mode b= (double)sum/n • Result of a+b is converted to intger. c= (int)(a+b) • a is converted to integer and then added to b. p= (int)a+b 9/21/2024 CSM – 6151 Programming with "C" 32 Explicit Type Conversion or Type Casting
  • 33.
    Mangalayatan University Standard inputor stdin is used for taking input from devices such as the keyboard as a data stream Standard output or stdout is used for giving output to a device such as a monitor. For using I/O functionality, programmers must include stdio header-file within the program. 9/21/2024 CSM – 6151 Programming with "C" 33 Standard I/O
  • 34.
    Mangalayatan University Reading Characterfrom the keyboard: • getchar() function can be used to read a single character Syntax: var_name = getchar(); Example: 9/21/2024 CSM – 6151 Programming with "C" 34 Standard I/O #include<stdio.h> void main() { char title; title = getchar(); }
  • 35.
    Mangalayatan University Writing Characterto the monitor • putchar() function can be used to display a single character on the screen Syntax: putchar(var_name); Example: 9/21/2024 CSM – 6151 Programming with "C" 35 Standard I/O #include<stdio.h> void main() { char title =‘t’; putchar(title); }
  • 36.
    Mangalayatan University Formatted inputfunction takes an input data a specific format. It allows by scanf() function Syntax: scanf(“control string”,&arg1,&arg2..); Example: 9/21/2024 CSM – 6151 Programming with "C" 36 Standard I/O #include<stdio.h> void main() { int var1; scanf("%d ", &var1, ); }
  • 37.
    Mangalayatan University Formatted outputfunction displays a data in a specific format on the screen. It allows by printf() function Syntax: printf(“control string”,arg1,arg2..); Example: 9/21/2024 CSM – 6151 Programming with "C" 37 Standard I/O #include<stdio.h> void main() { int var1= 60; printf("%d ", var1, ); }
  • 38.
    Mangalayatan University There arethree type of errors can occur in the c Programming. Syntax Error Occurs due to incorrect syntax used in C- statements. Found at compile time Logical Error Occurs due to incorrect logic used in C – statements by user. Found after execution i.e may give incorrect output Runtime Error Code links and compiles just fine, but during execution, it does something illegal. 9/21/2024 CSM – 6151 Programming with "C" 38 Errors
  • 39.
    Mangalayatan University Examples Syntax Error voidmain() { printf(“ value of x %d”,x): /* x is not declared and semicolon missing*/ } Logical Error floataverage(floata,floatb) { return a+b/2; /* should be (a + b) / 2 */ } Runtime Error int a; printf("type an integer"); scanf("%d", a);. 9/21/2024 CSM – 6151 Programming with "C" 39 Errors
  • 40.
    Mangalayatan University Source code •The input program of compiler is known as source code. • It can be in any high level programming language • It is human readable code. Object code • The output program of compiler is known as object code • It is machine language code. • It is machine readable code Executable Code • Executable code is generated after linking all the object module after compilation. 9/21/2024 CSM – 6151 Programming with "C" 40 Object Code and Executable Code
  • 41.
    Mangalayatan University 9/21/2024 41 Translationof Codes CSM – 6151 Programming with "C"
  • 42.
    Mangalayatan University A variableis a name which is assigned to a data value at storage location in computer’s memory. Variables are used to store various kinds of data during program executions. Variables name assigned before it use and it can be letter , digit and an underscore(_). 9/21/2024 42 Variables and Memory allocation CSM – 6151 Programming with "C"
  • 43.
    Mangalayatan University • Syntax: •<data type> <variable Name>; Declaration: • int num; • char name; Example: • Two type of initialization Initialization: • int num=10; //at time of declaration Example1: • int num; • num=10; // after the declaration Example2: 9/21/2024 CSM – 6151 Programming with "C" 43 Declaration and Initialization
  • 44.
    Mangalayatan University Write aprogram of find area of circle Write a program of find area of triangle. Write a program of find swap two numbers using third variable Write a program of swap two numbers without using third variable Write a program of swap two numbers without using third variable Write a program of find area of circle. Write a program of find area of triangle. Write a program of find swap two numbers using third variable. Write a program of swap two numbers without using third variable 9/21/2024 CSM – 6151 Programming with "C" 44 Programs
  • 45.
    Mangalayatan University /* Areaof Circle*/ #include<stdio.h> #include<conio.h> void main() { float r, area; clrscr(); printf(“enter radiusn”); scanf(“%f”,&r); area=22*r*r/7; printf(“area of circle is=%f”,area); getch(); } 9/21/2024 CSM – 6151 Programming with "C" 45 Program OUTPUT: Enter radius: 2 area of circle is =6.28
  • 46.
    Mangalayatan University /* Areaof Triangle*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c, area,s; clrscr(); printf(“Enter sides of trianglen”); scanf(“%f%f%f”, &a, &b,&c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“area of Triangle is=%f”,area); getch(); } 9/21/2024 CSM – 6151 Programming with "C" 46 Program OUTPUT: Enter sides of triangle: 3 3 2 area of triangle is = 0.44
  • 47.
    Mangalayatan University /* SwapTwo Numbers using third variable*/ #include<stdio.h> #include<conio.h> void main() { int a,b,temp; clrscr(); printf (“enter two numbersn”); scanf (“%d %d ” ,&a ,&b ); temp=a; a=b; b=temp; printf(“After swap numbers are:n a=%dnb=%d”,a,b); getch(); } 9/21/2024 CSM – 6151 Programming with "C" 47 Program OUTPUT: Enter two numbers 20 30 After swap numbers are: a=30 b=20
  • 48.
    Mangalayatan University /* SwapTwo Numbers without using third variable*/ #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf (“enter two numbersn”); scanf (“%d %d ” ,&a ,&b ); a=a+b; b=a-b; a=a-b; printf(“After swap numbers are:n a=%dnb=%d”,a,b); getch(); } 9/21/2024 CSM – 6151 Programming with "C" 48 Program(CO1) OUTPUT: Enter two numbers 20 30 After swap numbers are: a=30 b=20
  • 49.
    Mangalayatan University  InC-programming what will be the value of r if r=p%q where p=-17 and q=5? [MTU 2011-12 (Even) Marks 2]  What is the value of 2^3? [GBTU 2011-12 (Odd) Marks 2]  Write a program in C to find the greatest number among three numbers using conditional operator. [GBTU 2013-14 (Odd) Marks 5]  What will be the output of the following code? [GBTU 2011-12 (Odd) Marks 2] void main( ) { int a=5, b=6; printf(“%dt”,a=b); printf(“%dt”,a==b); printf(“%dt%d”,a,b); } 9/21/2024 CSM – 6151 Programming with "C" 49 Weekly Assignment
  • 50.
    Mangalayatan University  Datatypes  Structure of C Program  Basic of C program 9/21/2024 CSM – 6151 Programming with "C" 50 Prerequisite and Recap(CO1)
  • 51.
    Mangalayatan University  Datatypes • int , char , float , double and void  Structure of C Program • Documentation section • Preprocessor Directive section • Global section • Void main()  Standard I/O • printf() • scanf() 9/21/2024 CSM – 6151 Programming with "C" 51 Prerequisite and Recap
  • 52.
    Mangalayatan University Thank You 9/21/2024CSM – 6151 Programming with "C" 52