Introduction C
Introduction C
What is C?
A language written by Brian Kernighan and
Dennis Ritchie. This was to be the language
that UNIX was written in to become the first
"portable" language
Include files
Global variable and
function declaration
Main functions
Function subprogram
Simple C Program
/* A first C Program*/
#include <stdio.h>
void main()
{
printf("Hello World \n");
}
Simple C Program
Line 1: #include <stdio.h>
As part of compilation, the C compiler runs a
program called the C preprocessor. The
preprocessor is able to add and remove code from
your source file.
In this case, the directive #include tells the
preprocessor to include code from the file stdio.h.
This file contains declarations for functions that
the program needs to use. A declaration for the
printf function is in this file.
Simple C Program
Line 2: void main()
• This statement declares the main function.
• A C program can contain many functions but
must always have one main function.
• A function is a self-contained module of code that
can accomplish some task.
• Functions are examined later.
• The "void" specifies the return type of main. In
this case, nothing is returned to the operating
system.
Simple C Program
Line 3: {
\n new line
\t tab
\a alert
\\ backslash
\” double quote
Comment
• Comment should be enclosed between /*
*/
• It is used to increase the readability of the
program
• Any number of comments can be given at
any place in the program
• Comment cannot be nested
• It can be split over more than one line
Getting started with C
Communicating with a computer involves speaking
the language the computer understands
Steps in learning English language
Steps in learning C
Alphabets Constants
Digits Variables Instruction Program
Special-symbols Keywords
Keywords
• Keywords are the reserved words whose meaning has
already been explained to the C compiler
• C has 32 keywords
• These keywords combined with a formal syntax form a
C programming language
• Rules to be followed for all programs written in C:
• All keywords are lower-cased
• C is case sensitive, do-while is different from DO WHILE
• Keywords cannot be used as a variable or function name
C Keywords
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Identifiers
• Identifiers refer to the name of variables, functions and
arrays
• These are user-defined names and consist of sequence
of letters and digits, with a letter as a first character
• Both uppercase and lowercase letters are permitted,
although lowercase letters are commonly used
• The underscore character is also permitted in identifiers
• It is usually used as a link between two words in long
identifiers
Identifier Names
Some correct identifier names are - arena, s_count
marks40
class_one
X
1stsst
oh!god
start….end
The number of characters in the variable that are
recognized differs from compiler to compiler
An identifier cannot be the same as a C keyword
Variables
• Variables are named locations in memory that are used
to hold a value that may be modified by the program
• Unlike constants that remain unchanged during the
execution of a program
• A variable may take different values at different times
during execution
• The syntax for declaring a variable is –
DataType IdentifierName ;
• Example- int num;
long int sum , a;
Constants
• Constants are the fixed values that do not
change during the execution of a program
• C supports several types of constants
• Numeric Constants
• Integer constants
• Real constants
• Character Constants
• Single character constant
• String Constants
Special Symbols
Example - c=a+b;
C operation Algebraic C
Multiplication(*) bm b*m
• Highest to lowest
()
*, /, %
+, -
Example
Algebra:
z = pr%q+w/x-y
C:
z = p * r % q + w / x – y ;
Precedence:
1 2 4 3 5
Example
Algebra:
a(b+c)+ c(d+e)
C:
a * ( b + c ) + c * ( d + e )
;
Precedence:
3 1 5 4 2
Decision Making
() left to right
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
= left to right
Assignment Operators
=
+=
-=
*=
/=
%=
Increment/ decrement
operators
++ ++a
++ a++
-- --a
-- a--
Increment/ decrement
operators
main()
{
int c;
c = 5;
printf(“%d\n”, c); 5
printf(“%d\n”, c++); 5
printf(“%d\n\n”, c); 6
c = 5; 5
printf(“%d\n”, c); 6
printf(“%d\n”, ++c); 6
printf(“%d\n”, c);
return 0;
}
printf & scanf
Printf & Scanf
2. double
• Double data type is also same as float data type
which allows up-to 10 digits after decimal
• The range for double data type is from 1E–37 to
1E+37.
Modifiers in C
• The amount of memory space to be allocated for a
variable is derived by modifiers
• Modifiers are prefixed with basic data types to modify
(either increase or decrease) the amount of storage
space allocated to a variable
• For example:
storage space for int data type is 4 byte for 32 bit
processor
We can increase the range by using long int which is 8
byte
We can decrease the range by using short int which is
2 byte
storage
S.No Data Types Range
Size
1 Char 1 –127 to 127
2 Int 2 –32,767 to 32,767
• Enum syntax in C:
int main()
{
enum MONTH { Jan = 0, Feb, Mar };
if(month == 0)
printf("Value of Jan");
else if(month == 1)
printf("Month is Feb");
if(month == 2)
printf("Month is Mar");
}
Derived Data Type
int main()
{
int m = 22, n = 44;
// m, n are local variables of main function
/*m and n variables are having scope within this main function
only. These are not visible to test funtion.*/
/* If you try to access a and b in this function, you will get
'a' undeclared and 'b' undeclared
error */
printf("\nvalues : m = %d and n = %d", m, n);
test();
}
void test()
{
int a = 50, b = 80;
// a, b are local variables of test function
/*a and b variables are having scope within this test
function only. These are not visible to main function.*/
/* If you try to access m and n in this function, you will get
'm' undeclared and 'n' undeclared error */
printf("\nvalues : a = %d and b = %d", a, b);
VARIABLE
printf("\nvalues : m= %d : n= %d : a= %d : b= %d",m,n,a,b);
test();
void test()
{
2
• Variable can be declared many • It can happen only one time
times in a program for a variable in a program
3
• The assignment of properties • Assignments of storage
and identification to a variable space to a variable
CONSTANT
Syntax:
const data_type variable_name;
(or)
const data_type *variable_name;
CONSTANT
Rules for constructing C constant:
1. Integer Constants in C:
• An integer constant must have at least one digit
• It must not have a decimal point
• It can either be positive or negative
• No commas or blanks are allowed within an integer constant
• If no sign precedes an integer constant, it is assumed to be
positive
• The allowable range for integer constants is -32768 to 32767
CONSTANT
Rules for constructing C constant:
2. Real constants in C:
• A real constant must have at least one digit
• It must have a decimal point
• It could 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 a real constant
CONSTANT
Rules for constructing C constant:
3. Character and string constants in C:
• A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single quotes
• The maximum length of a character constant is 1 character
• String constants are enclosed within double quotes
CONSTANT
Rules for constructing C constant:
4. Backslash Character Constants in C:
• There are some characters which have special meaning in C
language
• They should be preceded by backslash symbol to make use
of special function of them
• Given below is the list of special characters and their
purpose
Backslash Character
Backslash character Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\” Double quote
\’ Single quote
\\ Backslash
\v Vertical tab
\a Alert or bell
\? Question mark
Octal constant (N is an octal
\N
constant)
Hexadecimal constant (N – hex.dcml
\XN
cnst)
CONSTANT
S.no Constant type data type Example
void main()
{
const int height = 100; /*int constant*/
const float number = 3.14; /*Real constant*/
const char letter = 'A'; /*char constant*/
const char letter_sequence[10] = "ABC"; /*string constant*/
const char backslash_char = '\?'; /*special char cnst*/
void main()
{
}
OPERATORS AND EXPRESSIONS
• The symbols which are used to perform logical and
mathematical operations in a C program are called C
operators
• These C operators join individual constants and variables
to form expressions
• Operators, functions, constants and variables are
combined together to form expressions
• Consider the expression A + B * 5. where, +, * are
operators, A, B are variables, 5 is constant and A + B * 5 is
an expression
OPERATORS AND EXPRESSIONS
Types of C operators:
• Arithmetic operators
• Assignment operators
• Relational operators
• Logical operators
• Bit wise operators
• Conditional operators (ternary operators)
• Increment/decrement operators
• Special operators
Types of C operators
Arithmetic Operators in C:
• C Arithmetic operators are used to perform mathematical
calculations like addition, subtraction, multiplication,
division and modulus in C programs
S.no Arithmetic Operators Operation Example
1 + Addition A+B
2 - Subtraction A-B
3 * multiplication A*B
4 / Division A/B
5 % Modulus A%B
#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a + b;
sub = a - b;
mul = a * b;
div = a / b;
mod = a % b;
Total+i
}
printf("Total = %d", Total);
}
Types of C operators
Relational operators in C:
• Relational operators are used to find the relation between two
variables. i.e. to compare the values of two variables in a C program
S.no Operators Example Description
1 > x>y x is greater than y
2 < x<y x is less than y
x is greater than or
3 >= x >= y
equal to y
x is less than or equal
4 <= x <= y
to y
5 == x == y x is equal to y
6 != x != y x is not equal to y
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Types of C operators
Logical operators in C:
• Are used to perform logical operations on the given expressions
• There are 3 logical operators in C language
S.no Operators Name Example Description
It returns true when both conditions
1 && logical AND (x>5)&&(y<5)
are true
(x>=10)|| It returns true when at-least one of
2 || logical OR
(y>=10) the condition is true
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}
Types of C operators
• Bit wise operators in C:
• These operators are used to perform bit operations
• Decimal values are converted into binary values which are the
sequence of bits and bit wise operators work on these bits
• Bit wise operators in C language are
& (bitwise AND)
| (bitwise OR)
~ (bitwise NOT)
^ (XOR)
<< (left shift)
>> (right shift)
#include <stdio.h>
int main()
{
int m=40,n=80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("NOT_opr value = %d\n",NOT_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}
CONTROL STATEMENT
DECISION CONTROL STATEMENT
• In decision control statements (C if else and nested if),
group of statements are executed when condition is
true. If condition is false, then else part statements
are executed.
• There are 3 types of decision making control
statements in C language. They are,
• if statements
• if …… else statements
• nested if statements
DECISION CONTROL STATEMENT
Decision
control
Syntax Description
statement
s
if (condition) In these type of statements, if condition is
if
{ Statements; } true, then respective block of code is executed.
if (condition)
In these type of statements, group of
{ Statement1; Statem
statements are executed when condition is
ent2;}
if…else true.
else
If condition is false, then else part statements
{ Statement3; Statem
are executed.
ent4; }
if (condition1) If condition 1 is false, then condition 2 is
{ Statement1; } checked and statements are executed if it is
nested if else_if (condition2) true.
{ Statement2; } If condition 2 also gets failure, then else part is
else Statement 3; executed.
if statements
int main()
{
int m=40,n=40;
if (m == n)
{
printf("m and n are equal");
}
}
if ….. else statements
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n) {
printf("m and n are equal");
}
else {
printf("m and n are not equal");
}
}
Nested if statements
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n) {
printf("m is greater than n");
}
else if(m<n) {
printf("m is less than n");
}
else {
printf("m is equal to n");
}
}
LOOP CONTROL STATEMENTS
• Loop control statements in C are used to perform
looping operations until the given condition is true.
Control comes out of the loop statements once
condition becomes false.
• There are 3 types of loop control statements in C
language
• for
• while
• do-while
LOOP CONTROL STATEMENTS
S.n Loop
Syntax Description
o Name
Where,
exp1 – variable initialization
for (exp1; exp2; ( Example: i=0, j=2, k=3 )
1 For expr3) exp2 – condition checking
{ statements; } ( Example: i>5, j<3, k=3 )
exp3 – increment/decrement
( Example: ++i, j–, ++k )
while (condition) where,
2 While
{ statements; } condition might be a>5, i<10
do { statements;
where,
3 do while }
condition might be a>5, i<10
while (condition);
for Loop
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
printf("%d ",i);
}
}
while Loop
#include <stdio.h>
int main()
{
int i=3;
while(i<10)
{
printf("%d\n",i);
i++;
}
}
do … while Loop
#include <stdio.h>
int main()
{
int i=1;
do
{
printf("Value of i is %d\n",i);
i++;
}while(i<=4 && i>=2);
}
Difference between while & do while
loops in C:
S.
while do while
no
Loop is Loop is executed for first time
executed irrespective of the condition.
1 only when After executing while loop for
condition is first time, then condition is
true. checked.
CASE CONTROL STATEMENTS
Syntax: break;
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("\nComing out of for loop when i =
5");
break;
}
printf("%d ",i);
}
}
CASE CONTROL STATEMENTS
Continue statement in C:
• Continue statement is used to continue the next iteration
of for loop, while loop and do-while loops.
• So, the remaining statements are skipped within the loop
for that particular iteration.
Syntax : continue;
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5 || i==6)
{
printf("\nSkipping %d from display using " \
"continue statement \n",i);
continue;
}
printf("%d ",i);
}
}
CASE CONTROL STATEMENTS
goto statement in C:
• goto statements is used to transfer the normal flow of a
program to the specified label in the program.
Syntax
{
…….
go to label;
…….
…….
LABEL:
statements;
}
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("\nWe are using goto statement when i = 5");
goto HAI;
}
printf("%d ",i);
}
}
Thank You
• Thank You