UNIT-1 (1)
UNIT-1 (1)
CLR -3: Store and retrieve data in a single and multidimensional array
Apply the logic operators and expressions. Use loop constructs and recursion. Use
CLO -2: array to store and retrieve data
Analyze programs that need storage and form single and multi-dimensional
CLO -3: arrays. Use preprocessor constructs in C
Create user defined functions for mathematical and other logical operations. Use
CLO -4: pointer to address memory and data
Create structures and unions to represent data constructs. Use files to store and
CLO -5: retrieve data
S. No TEXT BOOKS
1. Zed A Shaw, Learn C the Hard Way: Practical Exercises on the Computational
Subjects You Keep Avoiding (Like C), Addison Wesley, 2015
4. http://www.c4learn.com/learn-c-programming-language/
UNIT I
INTRODUCTION
Evolution of Programming & Languages - Problem Solving
through Programming - Creating Algorithms - Drawing
Flowcharts - Writing Pseudocode - Evolution of C language, its
usage history - Input and output functions: Printf and scanf -
Variables and identifiers – Expressions - Single line and
multiline comments - Constants, Keywords - Values, Names,
Scope, Binding, Storage Classes - Numeric Data types: integer -
UNIT I
INTRODUCTION
floating point - Non-Numeric Data types: char and string -
Increment and decrement operator - Comma, Arrow and
Assignment operator - Bitwise and Sizeof operator
1. 1 Evolution of Programming & Languages
❑ A Computer needs to be given instructions in a programming
language that it understands
❑ Programming Language
❑ Artificial language that controls the behavior of computer
❑ Defined through the use of syntactic and semantic rules
❑ Used to facilitate communication about the task of organizing
and manipulating information
❑ Used to express algorithms precisely
1. 1 Evolution of Programming & Languages Contd…
an imaginative way
❑ Code – Instructions
❑ Disadvantages
❑ No accepted Standard
1960
1967
1970
1972
1978
1989
1990
1999
Evolution of C
1. 6 History & Evolution of C Cont…
❑ Why the Name “C” was given ?
❑Many of C’s principles and ideas were derived from the earlier
language B
❑BCPL and CPL are the earlier ancestors of B Language (CPL is
common Programming Language)
❑In 1967, BCPL Language ( Basic CPL ) was created as a scaled
down version of CPL
❑As many of the features were derived from “B” Language the
new language was named as “C”.
1. 6 History & Evolution of C Cont…
❑ Characteristics of ‘C’
❑ Low Level Language Support
❑ Structured Programming
❑ Extensive use of Functions
❑ Efficient use of Pointers
❑ Compactness
❑ Program Portability
❑ Loose Typing
1. 6 History & Evolution of C Cont…
❑ Advantages of C
❑ Compiler based Language
❑ Programming – Easy & Fast
❑ Powerful and Efficient
❑ Portable
❑ Supports Graphics
❑ Supports large number of Operators
❑ Used to Implement Datastructures
.
comma
g) Opening and closing of braces should be balanced
1. 7 Structure of ‘C’ Program Contd…
/* Program to Find Area of Circle */
Comm
ent
#include <stdio.h>
Preprocessor
#include <conio.h> Directives
const float pi = 3.14; void Global
Directiv Description
e
#define Substitutes a preprocessor macro.
#include Inserts a particular header from another file.
#undef Undefines a preprocessor macro.
#ifdef Returns true if this macro is defined.
#ifndef Returns true if this macro is not defined.
#if Tests if a compile time condition is true.
#else The alternative for #if.
#elif #else and #if in one statement.
#endif Ends preprocessor conditional.
1. 7 Structure of ‘C’ Program Contd…
Directiv Description
e
#error Prints error message on stderr.
Issues special commands to the compiler, using a standardized
#pragma
method.
1. 7 Structure of ‘C’ Program Contd…
3) Global Declaration Section
❑ Used to Declare Global variable (or) Public variable
❑ Variables are declared outside all functions
❑ Variables can be accessed by all functions in the program
❑ Same variable used my more than one function
1. 7 Structure of ‘C’ Program Contd…
4) main( ) Section
❑ main( ) written in all small letters (No Capital Letters)
❑ Execution starts with a Opening Brace : {
❑ Divided into two sections: Declaration & Execution
❑ Declaration : Declare Variables
❑ Executable: Statements within the Braces
❑ Execution ends with a Closing Brace : }
❑ Note: main( ) does not end with a semicolon
1. 7 Structure of ‘C’ Program Contd…
5) Local Declaration Section
❑ Variables declared within the main( ) program
❑ These variables are called Local Variables
❑ Variables initialized with basic data types
1. 8 C Programming Fundamentals
1. 8 C Programming Fundamentals Contd…
1. 8 C Programming Fundamentals Contd…
❑C Token - Smallest
individual unit of a C
program
❑C program broken into
many C tokens
❑Building Blocks of C
program
. 8 C Programming Fundamentals Contd…
1. 9 Single Line and Multiline Comments
❑ Comment – Definition
❑ Used to provide information about lines of code
❑ Provide clarity to the C source code
❑ Allows others to better understand what the
code was intended to
❑ Helps in debugging the code
❑ Important in large projects containing
hundreds or thousands of lines of source code
❑ Types – Single line and multiline comment
1. 9 Single Line and Multiline Comments Contd…
a) Single Line Comment
❑ Represented by double slash \\
#include<stdio.h>
int main( ){
//printing information
printf("Hello C");
return 0;
}
1. 9 Single Line and Multiline Comments Contd…
b) Multi-Line Comment
❑ Represented by slash asterisk \* ... *\
#include<stdio.h>
int main( ){
/*printing information
Multi Line Comment*/
printf("Hello C");
return 0;
}
1. 9 Single Line and Multiline Comments Contd…
❑ int sum;
❑ Syntax
datatype
variablename;
1. 12 Variables & Identifiers Contd…
❑ Examples
int a=5, b, c,
int sum=0;
float avg;
char name;
❑ Variable Initialization
❑ Assigning a value to the declared variable
❑ Values assigned during declaration / after declaration
1. 12 Variables & Identifiers Contd…
❑ Examples
i. int a, b, c;
a=10, b=20, c=30;
ii. int a=10 ,b=10, c=10;
❑ Scope of Variables
❑ Local Variables
❑ Global Variables
1. 13 Scope of Variables
❑ Definition
❑ A scope in any programming is a region of the program
where a defined variable can have its existence and beyond
that variable it cannot be accessed
❑ Variable Scope is a region in a program where a variable is
declared and used
❑ The scope of a variable is the range of program statements that
can access that variable
❑ A variable is visible within its scope and invisible outside it
1. 13 Scope of Variables Contd…
❑ There are three places where variables can be declared
a) Inside a function or a block which is called local variables
b) Outside of all functions which is called global variables
int a, b; int
c;
/* actual initialization */
a = 10; b = 20;
c = a + b;
#include <stdio.h>
/* global variable declaration */
int g;
int main ( )
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10; b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
1. 13 Scope of Variables Contd…
❑ Note: A program can have same name for local and global
variables but the value of local variable inside a function will
take preference
1. 14 Binding
❑ A Binding is an association between an entity and an attribute
❑ Between a variable and its type or value
❑ Between a function and its code
❑ Binding time is the point at which a binding takes place
❑ Types of Binding
a) Design Time
b) Compile Time
c) Link Time
d) Run Time
1. 14 Binding Contd…
a) Design Time
❑ Binding decisions are made when a language is designed
❑ Example
❑ Binding of + to addition in C
b) Compile Time
❑ Bindings done while the program is compiled
❑ Binding variables to datatypes
❑ Example
❑ int a; float b; char c;
1. 14 Binding Contd…
c) Link Time
❑ Compiled code is combined into a full program for C
❑ Example
❑ Global and Static variables are bound to addresses
d) Run Time
❑ Any binding that happens at run time is called Dynamic
❑ Any binding that happens before run time is called Static
❑ Values that are dynamically bound can change
1. 15 Storage Classes in C
❑ What is a Variable?
❑ Storage Class Specifiers tells the Compiler about the following:
❑ Where to Store a Variable
❑ What is the Initial value of the Variable
❑ What is the Lifetime of a Variable
❑Variable Scope: Area or block where the variables can be
accessed
#include<stdio.h>
#include<conio.h>
void main( )
{
int n = 10;
block1( );
block2( );
printf(“In Main Block n=%d”, n);
getch( );
}
block1( )
{
int n = 20;
printf(“In Block 1 n=%d”, n);
}
block2( )
{
int n = 30;
printf(“In Block 2 n=%d”, n);
}
Output
In Block 1 n=20
In Block 2 n= 30
#include<stdio.h>
#include<conio.h>
int n = 10;
void main( )
{
block1( );
block2( );
clrscr( );
printf(“In Main Block n=%d”, n);
getch( );
}
block1( )
{
printf(“In Block 1 n=%d”, n);
return;
}
block2( )
{
printf(“In Block 2 n=%d”, n);
return;
}
Output
In Block 1 n=10
In Block 2 n= 10
#include<stdio.h>
#include<conio.h>
void main( )
{
int x;
static int y;
clrscr( );
printf(“x=%dy=%d”, x, y);
getch( );
}
Output
x = 28722
y=0
1. 15 Storage Classes in C Contd…
d) Register Variables
❑ Variables stored in the CPU registers instead of Memory
❑ Keyword Register is used to define the variable
❑ Scope of the Variable: Local
❑ Advantages
❑ CPU register access is faster than memory access
❑ Disadvantages
❑ Number of CPU registers is less
❑ Less Number of variables can be stored in CPU registers
/* Program to Demonstrate Register Variables*/
#include<stdio.h>
#include<conio.h>
void main( )
{
register int n=1;
clrscr( );
for(n=1; n<=10; n++)
printf(“%d”, n); getch( );
}
Output
1 2 3 4 5 6 7 8 9 10
1. 16 Datatypes
❑ Defines a variable before use
❑ Specifies the type of data to be stored in variables
❑ Basic Data Types – 4 Classes
a) int – Signed or unsigned number
b) float – Signed or unsigned number having Decimal Point
c) double – Double Precision Floating point number
d) char – A Character in the character Set
❑ Qualifiers
1. 16 Datatypes Contd…
1. 16 Datatypes Contd…
a) Integer Data Type
❑ Whole numbers with a range
❑ No fractional parts
❑ Integer variable holds integer values only
❑ Keyword: int
❑ Memory: 2 Bytes (16 bits) or 4 Bytes (32 bits)
❑ Qualifiers: Signed, unsigned, short, long
❑ Examples: 34012, 0, -2457
1. 16 Datatypes Contd…
b) Floating Point Data Type
❑ Numbers having Fractional part
❑ Float provides precision of 6 digits
❑ Integer variable holds integer values only
❑ Keyword: float
❑ Memory: 4 Bytes (32 bits)
❑ Examples: 5.6, 0.375, 3.14756
1. 16 Datatypes Contd…
c) Double Data Type
❑ Also handles floating point numbers
❑ Double provides precision of 14 digits
❑ Integer variable holds integer values only
❑ Keyword: float
❑ Memory: 8 Bytes (64 bits) or 10 Bytes (80 bits)
❑ Qualifiers: long, short
1. 16 Datatypes Contd…
d) Character Data Type
❑ handles one character at a time
❑ Keyword: char
❑ Memory: 1 Byte (8 bits)
1. 17 Expressions
❑ Expression : An Expression is a
collection of operators and operands that
represents a specific value
❑ Operator : A symbol which performs
tasks like arithmetic operations, logical operations and
conditional operations
❑ Operands : The values on which the operators perform the task
❑ Expression Types in C
a) Infix Expression
b) Postfix Expression
c) Prefix Expression
1. 17 Expressions Contd…
a) Infix Expression
❑ The operator is used between operands
❑ General Structure : Operand1 Operator Operand2
❑ Example : a + b
b) Postfix Expression
❑ Operator is used after operands
❑ General Structure : Operand1 Operand2 Operator
❑ Example : ab+
1. 17 Expressions Contd…
c) Prefix Expression
❑ Operator is used before operands
❑ General Structure : Operator Operand1 Operand2
❑ Example : +ab
1. 17 Expressions Contd…
1. 18 Input and Output Functions
❑ Ability to Communicate with Users during execution
❑ Input Operation
❑ Feeding data into program
❑ Data Transfer from Input device to Memory
❑ Output Operation
❑ Getting result from Program
❑ Data Transfer from Memory to Output device
❑ Header File : #include<stdio.h>
1. 18 Input and Output Functions Contd…
❑ Input / Output Function Types
a) Formatted Input / Output Statements
b) Unformatted Input / Output Statements
1. 18 Input and Output Functions Contd…
a) Formatted Input / Output Statements
❑ Reads and writes all types of data values
❑ Arranges data in particular format
❑ Requires Format Specifier to identify Data type
❑ Basic Format Specifiers
❑ %d – Integer
❑ %f – Float
❑ %c – Character
❑ %s - String
1. 18 Input and Output Functions Contd…
i. The scanf ( ) Function
❑ Reads all types of input data
❑ Assignment of value to variable during Runtime
❑ Syntax
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
int a; int a;
a=10; scanf(“%d”, &a);
} }
1. 18 Input and Output Functions Contd…
}
1. 18 Input and Output Functions Contd…
/* Getting Multiple Input using scanf ( ) function */
#include<stdio.h>
#include<conio.h>
void main( )
{
int a, b;
float c;
scanf(“%d %d”, &a, &b);
scanf(“%f ”, &c);
}
1. 18 Input and Output Functions Contd…
ii. The printf ( ) Function
❑ To print Instructions / Output onto the Screen
❑ Requires Format Specifiers & Variable names to print
data
❑ Syntax
printf(“Control String/Format
Specifier”,arg1,arg2,… argn)
is pressed
1. 18 Input and Output Functions Contd…
iii. gets ( ) – Accepts any string from Keyboard until Enter Key
is pressed
❑ Unformatted Output Statements
operator
❑ Operator placed before variable (Pre)
❑ Operator placed before variable (Post)
❑ T he increment operator is written as ++ and the decrement
operator is written as --
1. 19 Operators in C Contd…
a) Increment and Decrement Operators Contd…
❑ Classification
❑ Pre Increment Operator
❑ Post Increment Operator
❑ Pre Decrement Operator
❑ Post Decrement Operator
1. 19 Operators in C Contd…
a) Increment and Decrement Operators Contd…
❑ Syntax
❑ Examples
expression.
increment operator.
5”.
/* Program for Pre Increment*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int i = 1;
while (++i<5)
{
printf(“%d”
} , i ); }
getch
( );
}
Output
234
1. 19 Operators in C Contd…
a) Increment and Decrement Operators Contd…
expression.
expression.
decrement operator.
getch
( );
Output
}
9876
1. 19 Operators in C Contd…
a) Increment and Decrement Operators Contd…
expression.
variables
evaluated at last
#include<stdio.h>
int main( )
{
int i, j;
i=(j=10, j+20);
return 0;
}
1. 19 Operators in C Contd…
c) Arrow Operator (->)
variable to access it
used
1. 19 Operators in C Contd…
d) Assignment Operators
❑ Assigns result of expression to a variable
❑ Performs Arithmetic and Assignment operations
❑ Commonly used Assignment operator: =
❑ Syntax
variable = expression;
❑ Examples
❑ num = 25; age = 18; pi = 31.4; area = 3.14 * r * r;
1. 19 Operators in C Contd…
❑ Shorthand Assignment Operators
Simple Assignment
Shorthand Operator
Operator
a=a+1 a+=1
a=a–1 a-=1
a=a*2 a*=2
a=a/b a/=b
a=a%b a%=b
c = c * (a + b) c *= (a + b)
b = b / (a + b) b /=(a + b)
/* Program for Assignment Operations*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int a; a
= 11;
a+ = 4;
printf(“Value of A is %d\n”,a); a =
11;
a- = 4;
printf(“Value of A is %d\n”,a); a =
11;
a* = 4;
printf(“Value of A is %d\n”,a); a =
11; a/ = 4;
printf(“Value of A is %d\
n”,a); a = 11;
a% = 4;
printf(“Value of A is %d\
n”,a); getch ( );
}
Output
Value of A is
15 Value of A
is 7 Value of A
is 44 Value of
A is 2 Value of
A is 3
1. 19 Operators in C Contd…
e) Bitwise Operators
❑ In arithmetic-logic unit, mathematical operations like:
#include <stdio.h>
int main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0; c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c ); c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c ); c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c ); c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c ); c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c ); c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
return 0;
}
Output
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15
1. 19 Operators in C Contd…
f) Sizeof Operators
❑ This operator returns the size of its operand in bytes
❑ The sizeof operator always precedes its operand
❑ Used to calculate the size of data type or variables
❑ Can be nested
❑ Returns the size in integer format
❑ Syntax looks more like a function but it is considered as an
operator in c programming
/* Program for Sizeof Operators*/
#include<stdio.h>
int main( )
{
int a;
float b;
double c;
char d;
printf(“Size of Integer :%d\n\n”,sizeof(a));
printf(“Size of Floating Point :%d\n\n”,sizeof(b));
printf(“Size of Double :%d\n\n”,sizeof(c));
printf(“Size of Charcter :%d\n\n”,sizeof(d));
return 0;
}
Output
Size of Integer :2
Size of Floating Point : 4
Size of Double : 8
Size of Character :1
THANK
YOU