0% found this document useful (0 votes)
7 views47 pages

C Unit-1 PPT

jgi

Uploaded by

24f3000279
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views47 pages

C Unit-1 PPT

jgi

Uploaded by

24f3000279
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 47

Programming Using ‘C’

Language

Unit -1
Introduction of ‘C’
• C was originally developed in the 1970s, by Dennis Ritchie at
Bell Telephone Laboratories, Inc. to develop the UNIX
operating system.
• C programming is considered as the base for other
programming languages, that is why it is known as mother
language.
• C is a High level , general –purpose structured programming
language.
• Advantages of C Programming:
 Easy to learn
 Structured language
 It produces efficient programs
 It can handle low-level activities
 It can be compiled on a variety of computer platforms
The Character set of ‘C’
• C language consist of some characters set, numbers and some
special symbols.
• The character set of C consist of all the alphabets of English
language.
• C consist of
Alphabets a to z, A to Z
Numeric 0,1 to 9
Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
• The words formed from the character set are building blocks
of C and are sometimes known as tokens. These tokens
represent the individual entity of language. The following
different types of token are used in C
1) Identifiers 2)Keywords 3)Constants
4) Operators 5)Punctuation Symbols
Identifiers

• A 'C' program consist of two types of elements , user defined


and system defined.

• An identifier is a word used by a programmer to name a


variable , function.

• Identifiers consist of letters and digits if any order, except that


the first character must be letter.

• Both Upper and lowercase letters can be used


Keywords

• Keywords are nothing but auto double int struct


system defined identifiers.
• Keywords are reserved words break else long switch
of the language.
case enum register typedef
• They have specific meaning in
the language and cannot be char extern return union
used by the programmer as
variable or constant names
const float short unsigned
• C is case sensitive, it means
these must be used as it is continue for signed void
• 32 Keywords in C Programming
default goto sizeof volatile

do if static while
Variables
• A variable is a name of the memory location. It is used to store data.
Its value can be changed, and it can be reused many times.
• It is a way to represent memory location through symbol so that it
can be easily identified.

Rules for defining variables:


• A variable can have alphabets, digits, and underscore.
• A variable name can start with the alphabet, and underscore only. It
can't start with a digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword, e.g. int,
float, etc.
• Example: int a;
int _ab;
int a30;
Data Types
A data type specifies the type of data that a variable can store.

Types Data Types


Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
Basic Data Types

Integer Types:
The integer data type in C is used to store the whole numbers without
decimal values. Octal values, hexadecimal values, and decimal values can
be stored in int data type in C.
Range: −32,768 to 32,767
Size: 2 bytes

Character Types:
Character data type allows its variable to store only a single character.
Range: −128 to 127
Size: 1 bytes
Float Types:
Float data type in C is used to store decimal and exponential values. It is
used to store decimal numbers (numbers with floating point values)
with single precision
Range: 1.2E-38 to 3.4E+38
Size: 4 bytes

Double Types:
A Double data type in C is used to store decimal numbers (numbers with
floating point values) with double precision.
Range: 1.7E-308 to 1.7E+308
Size: 8 bytes
Constants
• A constant is a value or an identifier whose value cannot be altered in a
program.
• For example: 1, 2.5

Integer constants:

• A integer constant is a numeric constant without any fractional or


exponential part. There are three types of integer constants in C
programming:
• decimal constant(base 10)
• octal constant(base 8)
• hexadecimal constant(base 16)
Floating-point constants:

• A floating point constant is a numeric constant that has either a


fractional form or an exponent form.
• For example: 2.0,0.0000234,-0.22E-5

Character constants:

• A character constant is a constant which uses single quotation


around characters.
• For example: 'a', 'l', 'm', 'F'

String constants:

• String constants are the constants which are enclosed in a pair of


double-quote marks.
• For example: "good" ,"x", "Earth is round"
Backslash Character Constants:

These Constants are used in output functions. They have one character
which is preceded by a backslash (\). These are also called an escape
sequences.

Sequences Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
Operators in C
• An operator is a symbol that help us to perform specific
mathematical and logical computations on operands..
• C programming has wide range of operators to perform various
operations.

• Arithmetic Operators
• Unary or Increment and Decrement Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Shorthand Assignment Operators
• Conditional Operators
• Bitwise Operators
• Comma Operators
Arithmetic Operator:

These operators are used to perform arithmetic/mathematical


operations on operands.

Operator Meaning of Operator


+ addition
- subtraction
* multiplication
/ division
% remainder after division ( modulo division)
Increment and Decrement Operators:

• Increment and Decrement operators are also known as unary operators,


as they only operate on a single operand.
• Increment operator (++) and decrement operator (--) to change the
value of an operand (constant or variable) by 1.
• Increment ++ increases the value by 1 whereas decrement -- decreases
the value by 1.
• For Example:
int a=10, b=100
++a = 11
--b = 99
Relational Operators:

• A relational operator checks the relationship between two operands.


If the relation is true, it returns 1; if the relation is false, it returns
value 0.
• Relational operators are used in decision making and loops.

Operator Meaning of Operator Example


== Equal to 5 == 3 returns 0
> Greater than 5 > 3 returns 1
< Less than 5 < 3 returns 0
!= Not equal to 5 != 3 returns 1
>= Greater than or equal to 5 >= 3 returns 1
<= Less than or equal to 5 <= 3 return 0
Logical Operators:

• Logical Operators are used to combine two or more conditions


and return Boolean value either true or false.
Operator Description
&& Called Logical AND operator. If both the operands
are non-zero, then the condition becomes true.
|| Called Logical OR Operator. If any of the two
operands is non-zero, then the condition
becomes true.
! Called Logical NOT Operator. It is used to reverse
the logical state of its operand. If a condition is
true, then Logical NOT operator will make it false.
Assignment Operators:

• An assignment operator (=) is used for assigning a value to a


variable.
• Shorthand assignment operator combines one of
the arithmetic or bitwise operators with assignment operator.

Assignment Operator Example Same as


= a=b a=b
Shorthand assignment
Example Meaning
operator
+= a += 10 a = a + 10
-= a -= 10 a = a - 10
*= a *= 10 a = a * 10
/= a /= 10 a = a / 10
%= a %= 10 a = a % 10
&= a &= 10 a = a & 10
|= a |= 10 a = a | 10
^= a ^= 10 a = a ^ 10
~= a ~= 10 a = a ~ 10
<<= a <<= 10 a = a << 10
>>= a >>= 10 a = a >> 10
Conditional Operator:

• The conditional operator is also known as a ternary operator. It is


represented by two symbols, i.e., '?' and ':'.
• The conditional operator is of the form

•In the above syntax, the expression1 is a Boolean condition that can be
either true or false value.
•If the expression1 results into a true value, then the expression2 will
execute.
•If the expression1 returns false value then the expression3 will
execute.
Comma Operator:

• The comma (,) operator works as a separator and an operator too.


• The comma operator has the lowest precedence of any C operator.

Comma (,) as separator:

While declaration multiple variables and providing multiple arguments


in a function, comma works as a separator.

Example:
int a, b, c;

In this statement, comma is a separator and tells to the compiler that


these (a, b, and c) are three different variables.
Comma (,) as an operator:

Sometimes we assign multiple values to a variable using comma, in that


case comma is known as operator.

Example:

a=10, 20, 30;


b=(10, 20, 30);

In the first statement, value of a will be 10, because assignment operator


(=) has more priority more than comma (,), thus 10 will be assigned to the
variable a.

In the second statement, value of b will be 30, because 10, 20, 30 are
enclosed in braces, and braces has more priority than assignment (=)
operator. When multiple values are given with comma operator within the
braces, then right most value is considered as result of the expression.
Thus, 30 will be assigned to the variable b.
Bitwise Operators:

Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ is as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Operator Description Example

& Binary AND Operator copies a bit to the (A & B) = 12, i.e.,
result if it exists in both operands. 0000 1100
| Binary OR Operator copies a bit if it exists in (A | B) = 61, i.e.,
either operand. 0011 1101
^ Binary XOR Operator copies the bit if it is set (A ^ B) = 49, i.e.,
in one operand but not both. 0011 0001
~ Binary One's Complement Operator is unary (~A ) = ~(60), i.e,. -
and has the effect of 'flipping' bits. 0111101
<< Binary Left Shift Operator. The left operands
A << 2 = 240 i.e.,
value is moved left by the number of bits
1111 0000
specified by the right operand.
>> Binary Right Shift Operator. The left operands
value is moved right by the number of bits A >> 2 = 15 i.e.,
specified by the right operand. 0000 1111
First C Program
#include <stdio.h>
void main()
{
printf("Hello World.");
}

• #include is a preprocessor directives. It include the contents of


the system or a user defined header file within the application or
the program
• #include <stdio.h> includes the standard input output library
functions.
• void main() function is the entry point of every program and
void means no return type.
• printf() function is used to print data on the console and is
defined in stdio.h header file.
How to compile and run the c program

There are 2 ways to compile and run the c program, by menu and by
shortcut.
By menu:

• Click on the compile menu then compile sub menu to compile the c
program.
• Click on the run menu then run sub menu to run the c program.

By shortcut:

• Press ctrl+f9 keys compile and run the program directly.


• You will see the output on user screen.
• You can view the user screen any time by pressing the alt+f5 keys.
printf() and scanf() function in C:
• The printf() and scanf() functions are used for input and output.
• Both functions are inbuilt library functions, defined in stdio.h (header file).
• The format specifier can be %d (integer), %c (character), %s (string), %f
(float) etc.

printf() function:

The printf() function is used for output. It prints the given statement to the
console.

printf("format specifier",argument_list);

scanf() function:

The scanf() function is used for input. It reads the input data from the console.

scanf("format specifier",&argument_list);
Program to print sum of 2 numbers
#include<stdio.h>
int main()
{
int x,y,result;

printf("enter first number:");


scanf("%d",&x);
printf("enter second number:");
scanf("%d",&y);

result=x+y;
printf("sum of 2 numbers:%d ",result);

return 0;
}
C Control Structures

Control Structures are just a way to specify flow of control in programs.


C provides two types of flow control:

• Branching
• Looping

Branching: It means deciding what actions to take. There are various


ways for that like

• If statement
• If-else statement
• If else-if ladder
• Nested if
• Switch Statement
If Statement
• It takes an expression in parenthesis and an statement or block of
statements in curly braces.
• If the expression is true then the statement or block of statements gets
executed otherwise these statements are skipped.
• The syntax of the if statement is given below.
if(expression)
{
Block of statements;
}
• Example: To check a number is even. #include<stdio.h>
void main()
{
int number;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
If-else statement

• if-else statement is used for decision-making.


• If the given expression/condition is true, then the code inside if block
is executed, otherwise else block code is executed.
• The syntax of the if statement is given below.

if(expression)
{
Block of statements;
}
else
{
Block of statements;
}
Example: To check whether a number is even or odd.

#include<stdio.h>
void main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("%d is odd number",number);
}
}
If else-if ladder Statement
• In if-else-if ladder statement, if a condition is true then the statements
defined in the if block will be executed, otherwise if some other condition is
true then the statements defined in the else-if block will be executed, at the
last if none of the condition is true then the statements defined in the else
block will be executed.
• The syntax of the if statement is given below.
if(expression)
{
Block of statements;
} else if(expression)
{
Block of statements;
} else if(expression)
{
Block of statements;
} else
{
Block of statements;}
Example: To calculate the grade of the student according to the specified marks.

#include <stdio.h>
void main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ..."); }
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ..."); }
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ..."); }
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ..."); }
else
{
printf("Sorry you are fail ..."); }
}
Nested if statements

• It means you can use one if or else if statement inside another if or


else if statement(s)
• The syntax of the if statement is given below.
if(expression)
{
if(expression)
{
Block of statements;
}
}
Example:

#include <stdio.h>

void main ()
{
int a = 100;
int b = 200;

if( a == 100 )
{
if( b == 200 )
{
printf("Value of a is 100 and b is 200\n" );
}
}

printf("Exact value of a is : %d\n", a ); Output:


printf("Exact value of b is : %d\n", b ); Value of a is 100 and b is 200
Exact value of a is : 100
} Exact value of b is : 200
Switch Statement

• The switch statement in C is an alternate to if-else-if ladder statement


which allows us to execute multiple operations for the different possible
values of a single variable called switch variable.
• The syntax of switch statement is given below:
switch(expression)
{
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......

default:
code to be executed if all cases are not matched;
}
Example:
#include<stdio.h>
void main()
{
int number;
printf("enter a number:");
scanf("%d",&number);
switch(number)
{
case 10: printf("number is equals to 10");
break;
case 50: printf("number is equal to 50");
break;
case 100: printf("number is equal to 100");
break;
default: printf("number is not equal to 10, 50 or 100");
}
}
Looping

It can be defined as repeating the same process multiple times until a


specific condition satisfies. There are three types of loops used in the C
language.

• do while
• while
• For
do while loop
• The do while loop is a post tested loop. Using the do-while loop, we can
repeat the execution of the statements. The do-while loop is mainly
used in the case where we need to execute the loop at least once.
• The syntax of the do-while loop is given below:
do{
//code to be executed
}while(condition);
Example: To print the table of 2.

#include<stdio.h>
void main()
{
int i=1, number;
printf("Enter a number: ");
scanf("%d",&number);
do
{
printf("%d \n",(number*i));
i++;
}while(i<=10);
}
while loop
• While loop is also known as a pre-tested loop. A while loop allows a part
of the code to be executed multiple times depending upon a given
condition.
• The syntax of while loop is given below:
while(condition)
{
//code to be executed
}
Example: To print the table of 2.
#include<stdio.h>
void main()
{
int i=1, number;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10)
{
printf("%d \n",(number*i));
i++; } }
for loop
• A for loop is a repetition control structure that allows you to efficiently write
a loop that needs to execute a specific number of times.
• The syntax of a for loop is given below:
for ( initialise; condition; increment )
{
//code to be executed
}
Example: To print the table of 2.
#include<stdio.h>
void main()
{
int i, number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d \n",(number*i));
}
}
break statement

• It can be used to terminate a case in the switch statement.


• When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following the
loop.
• If you are using nested loops, the break statement will stop the execution of the
innermost loop and start executing the next line of code after the block.
• The syntax for a break statement is as follows:
break;
Example :
#include<stdio.h>
void main () {
Output:
int i;
for(i = 1; i<10; i++) 1
{ 2
printf("%d\n ",i); 3
if(i == 5) 4
break;
}
}
continue statement
• The continue statement is used to bring the program control to the
beginning of the loop. The continue statement skips some lines of code
inside the loop and continues with the next iteration.
• The syntax for a continue statement is as follows:
continue;
Example :
#include<stdio.h>
void main () Output:
{ 1
int i; 2
for(i = 0; i<10; i++) 3
{ 4
if(i == 5) 6
{ 7
continue; } 8
printf("%d \n",i); 9
} 10
}
goto statement
• A goto statement provides an unconditional jump from the 'goto' to a
labeled statement in the same function.
• The syntax for a goto statement is as follows −
goto label;
..
label: statement;
Example:
#include <stdio.h> Output:
void main() Enter the number:10
10 x 1 = 10
{
10 x 2 = 10
int num,i=1; 10 x 3 = 10
printf("Enter the number :"); 10 x 4 = 10
scanf("%d",&num); 10 x 5 = 10
table: 10 x 6 = 10
printf("%d x %d = %d\n",num,i,num*i); 10 x 7 = 10
i++; 10 x 8 = 10
if(i<=10) 10 x 9 = 10
goto table; 10 x 10 = 10
}
exit() function
• The exit() function is used to terminate a process or function calling
immediately in the program
• The exit() function is the standard library function of the C, which is defined
in the stdlib.h header file.
• It does not return anything.
• The exit(0) function determines the program terminates without any error
message.
Example:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int x = 10;
printf("The value of x : %d", x);
exit(0);
printf("Calling of exit()");
}
Output: The value of x : 10

You might also like