Session 01 PDF
Session 01 PDF
• Introduction to C:
• Introduction, Environment setup, Basic I/O
• Variables &Data types, Constants and Literals
• Storage classes , Type casting, operators
• Conditional Branching control statements: If-Else, switch-case,
A Brief History
• Developed in 1972 (traditional C – K&R C)
• By Dennis Ritchie
• AT At&T bell laboritories
• Influenced by Languages ALGOL, CPL, BCPL, B.
• Developed to make Unix more portable.
• 1989 –Standardised by ANSI (American National Standards Institute) – c89
• ISO (International Organization for standardization) adopted C89 with changes
in 1990 – c90
• Later on updated in 1995, 1999, 2011 -C95, C99, c11
About C
• Mother language
• System programming language
• Procedure-oriented programming language
• Structured programming language
• Mid level programming language
Features of C Language
Compiler
• Program that converts Source code to Executable machine language Programs.
• C is a compiled Language.
• Executable machine language program are self contained and runs very quickly.
• Programs written once and run Multiple times, without carrying the source code
or compiler around.
The C Compilation Model
RUNNING A C PROGRAM
• Compiler
Eg: gcc, cc, tcc etc
• Online compilers
Eg: codechef.com, jdoodle.com etc
C Program Structure
• Documentations
• Pre-processor Statements
• Global Declarations
• The main() Function
• Local Declarations
• Program Statements And expressions
• User Defined Functions
Program Example
Example:
/* description: writes the words "hello world" on the screen */
#include<stdio.h>
int main()
{
/* first c program */
printf("hello, world!\n");
return 0;
}
Explanation
• /*……*/ - comments
• #include<stdio.h> - pre-processor directive.
• int/void – return value
• main() – main function
• {….} – block
• printf – function to print text to screen
• return 0 – returns value 0 at the end of the main function.
Note
• Size qualifiers
• long
• Short
• Sign qualifiers
• Signed
• Unsigned
Size & Range
Type Size (bytes) Range Format Specifier
Char 1 -127 to +127 %c
Signed char
Unsigned char 1 0 to 255 %c
Short 2 −32,767 to +32,767 %hi
Short int
Signed short
Signed short int
Int 2 −32,767 to +32,767 %i or %d
signed
Signed int
unsigned short 2 0 to 65536 %hu
unsigned short int
unsigned 2 0 to 65536 %u
unsigned int
Size & Range cont’d
Const #define
• Syntax • Syntax
const data_type variable_name = Constant; #define Constant_Identifier Value
• Example • Example:
const float PI = 3.141; #define PI 3.141
Storage Classes
• Every variable has a storage class and a scope
• For variables storage class determines
• Part of memory
• Scope
• Visibility
• Lifetime
• 4 storage classes
• Automatic variables
• External variables
• Static variables
• Register variables
Automatic Storage Class
• Keyword: auto
• Scope: local
• Visibility: function or block
• Lifetime: function or block
• Any variable declared within a function or block with or without the ‘auto’
Keyword is by default a variable of the automatic storage class.
• auto is the default storage class for local variables.
• Example:
{
int Count;
auto int Month;
}
External Variable
• Keyword: Extern • Example:
• Scope: Global #include <stdio.h>
• Visibility: Entire duration of program extern int x;
execution int main()
• Lifetime: Entire duration of program {
execution
printf("x: %d\n", x);
• extern defines a global variable that is
visable to ALL object modules. }
• The extern keyword is used before a int x = 10;
variable to inform the compiler that
this variable is declared somewhere
else.
• The extern declaration does not
allocate storage for variables.
Static Variables
• Keyword: static void test(); //Function declaration
• Scope: global main()
• Visibility: function or block {
• Lifetime: Entire duration of program test();
execution test();
• A static variable tells the compiler to persist test();
the variable until the end of program.
• Static variables have default initial value zero }
and initialized only once in their lifetime. void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%d\t",a);
}
output :
1 2 3
Register Variable
• Keyword: register
• Scope: local
• Visibility: function or block
• Lifetime: function or block
• Memory location: CPU registers
• Register should only be used for variables that require quick access - such as
counters.
Example:
{
register int Miles;
}
Type Casting
• Process of converting an entity of one data type to another.
• 2 Types:
• Implicit Type casting
• Explicit Type casting
Implicit Type casting
• automatic type conversion by the
compiler
• data of one or more subtypes can be
converted to a supertype as needed at
runtime
• The compiler converts all operands
into the data type of the largest
operand
• Example:
int i=20;
double p;
p=i; // implicit conversion
Explicit type conversion
• 2 types #include <stdio.h>
• Upcasting- lower to upper datatype int main()
• Downcasting- upper to lower datatype
{
• Upcast – no data loss
int value1 = 10, value2 = 3;
• Downcast – data loss
float result;
• Example:
• float to int causes truncation, i.e., removal
of the fractional part. result = (float)value1 / value2;
• double to float causes rounding of digit.
printf("Result with type casting: %f",
• long long to int causes dropping of
excess higher order bits. result);
• Syntax: return 0;
(data_type)expression; }
Operators
• Special characters used to perform particular operations in C
• Used to manipulate data and variables
• They are:
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators
• Special Operators
Operators Cont’d
Assignment Operators
Logical Operators
Bitwise Operators
Operator Description
Operator Description
&& Logical AND. True only
when all operands are true. << Binary Left Shift Operator
(performs logical
conjunction on 2
>> Binary Right Shift Operator
expressions)
|| Logical OR. True if either
one operand is true.( ~ Binary ones compliment
Performs a logical operator
disjunction on 2
expressions) & Binary AND operator
• Sizeof Operator
• returns the size of data
• Eg:
int a,b;
b=sizeof(a);
• * - Pointer to a variable
Eg: * a
Control Statements
Control the flow of program
3 types:
• Conditional branching control statement
• Conditional looping control statement
• Unconditional control statement
Branching control statement
• 2 types
• Decision Control Statements
Checks for one or more conditions and if the condition is satisfied i.e. true one or
more statements are executed.
• IF statement
• IF- ELSE statement
• IF-ELSE-IF Statement
• Nested IF and IF-ELSE Statement
• Selection Control Statement
A switch statement decides which of several statements to execute.
• Switch-Case Statement
IF Statement
• Most simple form of the branching #include <stdio.h>
statements. int main()
• Syntax: {
if (expression) int x = 20;
statement; int y = 22;
if (x<y)
or {
printf("Variable x is less than y");
if (expression) }
{ return 0;
Block of statements; }
}
IF-ELSE Statement
• Executes some code if the test expression #include <stdio.h>
is true (nonzero) and some other code if int main()
the test expression is false (0).
{
• Syntax:
int num;
if (expression)
printf("Enter an integer: ");
{
scanf("%d",&num);
Block of statements;
// True if remainder is 0
}
if( number%2 == 0 )
else
printf("%d is an even integer.",num);
{
else
Block of statements;
printf("%d is an odd integer.",num);
}
return 0;
}
IF-ELSE-IF Statement
• if-else-if statement allows you to check for #include <stdio.h>
multiple test expressions and execute int main()
different codes for more than two conditions. {
• Syntax: int var1, var2;
if (expression) printf("Enter two integers: ");
{ scanf("%d %d", &var1, &var2);
Block of statements; if (var1 >var2)
} {
else if(expression) printf("var1 is greater than var2");
{ }
Block of statements; else if (var2 > var1)
} {
else printf("var2 is greater than var1");
{ }
Block of statements; else
} {
printf("var1 is equal to var2");
}
return 0;
}
Nested IF-ELSE Statement
#include <stdio.h>
• More than one conditions are int main()
checked one after another to execute {