C Chapter 1
C Chapter 1
Introduction to C language
1.1 History
1.2 Basic structure of C Programming
1.3 Language fundamentals
1.3.1 Character set, tokens
1.3.2 Keywords and identifiers
1.3.3 Variables and data types
1.4 Operators
1.4.1 Types of operators
1.4.2 Precedence and associativity
1.4.3 Expression
.
➢ C is General Purpose programming language .
.
Year Language Developed By Remark
.
➢ General Purpose Programming Language:
➢ Structured Programming:
.
➢ Wide Acceptability: C is widely popular in the software industry
.
➢ Memory Management
➢ Recursion technique.
.
➢ A character denotes any alphabet, digit or special symbol used to
represent information. Valid alphabets, numbers and special
symbols allowed in C. The alphabets, numbers and special
symbols when properly combined form constants, variables and
keywords.
➢ A–Z
➢ a -z
➢ 0-9
➢ (space) . , : ; ' $ "
➢ # % & ! _ {} [] () $ & |
➢ +-/*=
.
➢ Keywords are the reserved words used in programming.
➢ Each keywords has fixed meaning and that cannot be changed by
user.
➢ For example: int money;
➢ Here, int is a keyword that indicates, 'money' is of type
integer.
➢ As, C programming is case sensitive, all keywords must be written
in lowercase.
.
auto double int struct
break else long switch
case enum register typedef
char extern return union
continue for signed void
do If static while
default goto sizeof volatile
.
➢ In C programming, identifiers are names given to C entities, such
as variables,functions, structures etc.
➢ Identifier are created to give unique name to C entities to identify
it during the execution of program.
➢ For example: int money; int mango_tree;
➢ Here, money is a identifier which denotes a variable of type
integer.
Similarly, mango_tree is another identifier, which denotes another
variable of type integer.
➢ They must begin with a alphabet or underscore(_).
➢ They must consist of only letters, digits, or underscore. No other
special character is allowed.
➢ It should not be a keyword.
➢ It must not contain white space or Blank Space.
➢ It should be up to 31 characters long as only first 31 characters are
significant.
.
Variables are memory location in computer's memory to store data.
To indicate the memory location, each variable should be given a unique name
called identifier.
.
➢ Variable name can be composed of letters (both uppercase and lowercase
letters), digits and underscore '_' only.
➢ There is no rule for the length of length of a variable. However, the first 31
characters of a variable are discriminated by the compiler. So, the first 31
letters of two variables in a program should be different.
.
➢ Constants are the terms that can't be changed during the
execution of a program.
➢ For example: 1, 2.5, "Programming is easy." etc.
1. Integer constant
3. Character Constant
.
1)Integer constants-
1. Floating point constants are the numeric constants that has either
fractional form or exponent form.
For example:
-2.0
0.0000234
-0.22E-5
3)Character constants
Character constants are the constant which use single quotation around
characters. For example: 'a', 'l', 'm', 'F' etc.
.
➢ Data Type means which type of data used in program,.
➢ According to type of data the memory is allocated to variable.
char 1
char a;
.
Declaration of Variables:-
Synatx:-
for e.g.
Types of operators
1. Arithmetic Operators
2.Relational Operator
3.logical operators
4.increment /decrement operator
5.Assignment operator
6.Bitwise operator
7.Conditional operator
8.Compound Assignment Operators
.
Operator Meaning of Operator
+ addition or unary plus
subtraction or unary
-
minus
* multiplication
/ division
remainder after
% division( modulo
division)
int a=10,b=5;
f=a / b f=10/5 f= 2
g= a%b g= 0
.
Relational operators checks relationship between two operands. If the
relation is true, it returns value 1 and if the relation is false, it returns
value 0.
For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not
then, it returns 0.
Relational operators are used in decision making and loops in C
programming.
.
Operator Meaning of Operator Example
|| Logical OR
! Logical NOT
.
Condition1 Condition2 Output (Logical AND( && ))
F F F
F T F
T F F
T T T
F T
T F
Example
1) Logical && (AND)
int a,b,c,d;
a=10;b=20;c=30;
d= a<b && b<c; 10<20 && 20<30 T(1) && T(1) T(1)
Value of d will be 1 (true)
d=a>b && b<c ; 10>20 && 20<30 F(0) && T(1) F(0)
value of d will be 0 (false)
2) Logical || (OR)
int a,b,c,d;
a=10;b=20;c=30;
d= a<b || b<c; 10<20 || 20<30 T(1) || T(1) T(1)
Value of d will be 1 (T)
d=a>b || b<c 10>20 || 20<30 F(0) || T(1) T(1)
value of d will be 1 (T)
3) Logical ! (NOT)
a=10; b=20;
c= ! a<b; 10<20 T(1) c= F(0)
Operators Meaning of operators
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement(NOT)
.
Bitwise AND Operator
▪ The & (bitwise AND) operator compares each bit of its first operand to
the corresponding bit of the second operand. If both bits are 1's, the
corresponding bit of the result is set to 1. Otherwise, it sets the
corresponding result bit to 0.
▪ The bitwise AND (&) should not be confused with the logical AND. (&&)
& operator.
▪ Logical table of Bitwise AND
0 0 0
0 1 0
1 0 0
1 1 1
28/46
Suppose int n1=20;
int n2=15;
n1=20 = 0000 0000 0001 0100 (int allocates 2 bytes means 16 bits)
32/46
Suppose int n1=20;
int n2=15;
We have to perform n1 | n2
First convert this n1 and n2 into binary number.
n1=20 = 0000 0000 0001 0100 (int allocates 2 bytes means 16 bits)
0 0 0
0 1 1
1 0 1
1 1 0
30/46
Suppose int n1=20;
int n2=15;
We have to perform n1 ^ n2
First convert this n1 and n2 into binary number.
n1=20 = 0000 0000 0001 0100 (int allocates 2 bytes means 16 bits)
Operand 1 ~ Operand
0 1
1 0
25/46
Suppose int n1=15;
29/46
Bitwise Shift Operators
Left shift operator, shift their first operand left (<<) by the
<< nbits << nshiftSize
number of positions specified by the second operand.
Right shift operator, shift their first operand right (>>) by the
>> nbits >> nshiftSize
number of positions specified by the second operand.
▪ Both operands must be integral values. These operators perform the usual arithmetic conversions; the
type of the result is the type of the left operand after conversion.
▪ For leftward shifts, the vacated right bits are set to 0. For rightward shifts, the vacated left bits are filled
based on the type of the first operand after conversion. If the type is unsigned, they are set to 0.
Otherwise, they are filled with copies of the sign bit. For left-shift operators without overflow, the
statement:
------------------------------------------------------
n= 0000 0000 0111 1000 = 120
29/46
Suppose int n=15;
------------------------------------------------------
n= 0000 0000 0111 1000 = 120
29/46
➢ ++ is a increment operators.
➢ -- is a decrement operators.
➢ Both of these operators are unary operators, i.e, used on single
operand.
➢ ++ adds 1 to operand and -- subtracts 1 to operand respectively.
➢ For example:
Let a=5 ;
b=10;
a++; //a becomes 6
a--; //a becomes 5
++a; //a becomes 6
--a; //a becomes 5
.
➢ When i++ is used as prefix(like: ++var),
➢ ++var will increment the value of var and then
return it
➢but, if ++ is used as postfix(like: var++),
➢operator will return the value of operand first and
then only increment it
➢Conditional operator takes three operands and consists of two
symbols ? and : . Conditional operators are used for decision
making in C.
➢Condition?true:flase;
➢ For example:
c=(c>0)?10:-10;
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b
➢Comma operators are used to link related expressions
together.
➢ For example:
int a,c=5,d;
Structure Of C program:-
Header File Declaration
void main( )
{
Statements;
}
.
Ex: 1.C Program to Print a Sentence
void main()
{
printf("C Programming");
}
C Program to Add Two Integers Entered by User
/*C programming source code to add and display the sum of two integers entered by user
*/
#include <stdio.h>
int main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2); /* Stores the two integer entered by
user in variable num1 and num2 */
sum=num1+num2; /* Performs addition and stores it in variable sum */
printf("Sum: %d",sum); /* Displays sum */
return 0;
}
Output
Enter two integers: 12
11
Sum: 23
C Program to Swap Two Numbers
#include <stdio.h> printf("\nAfter swapping,
int main(){ value of a = %.2f\n", a);
float a, b, temp; printf("After swapping,
printf("Enter value of a: "); value of b = %.2f", b);
scanf("%f",&a); return 0;
printf("Enter value of b: "); }
scanf("%f",&b);
temp = a;
/* Value of a is stored in variable temp */
a = b; Output
Enter value of a: 1.20
/* Value of b is stored in variable a */ Enter value of b: 2.45
b = temp; After swapping, value of a = 2.45
/* Value of temp(which contains initial After swapping, value of b = 1.2
value of a) is stored in variable b*/
Control Statements.
.
Decision making with if statement
• if statement
• if else Statement
• switch statement
if statement
Syntax:
if (Condition)
{
statement/s to be executed if Condition is true;
}
➢ If the Condition is true then, statements for the body if, i.e, statements inside
parenthesis are executed.
➢ But, if the test expression is false, the execution of the statements for the body
of if statements are skipped.
Decision making with if statement
➢ Example:
void main()
{
int num;
clrscr();
printf(“Enter any number”);
scanf(“%d”,&num);
if(num>0)
{
printf("%d is Positive\n",num);
}
getch();
}
Decision making with if...else statement
➢ The if...else statement is used, if the programmer wants to execute some code, if
the test expression is true and execute some other code if the test expression is
false.
Syntax of if...else :
if (Condition)
statements to be executed if test expression is true;
else
statements to be executed if test expression is false;
Decision making with if-else statement
➢ Example:
void main()
{
int num;
clrscr();
printf(“Enter any number”);
scanf(“%d”,&num);
if(num>0)
{
printf("%d is Positive\n",num);
}
else
{
printf("%d is Negative\n",num);
}
getch();
}
.
Decision making with Nested if...else statement
➢ The if...else statement can be used in nested form when a serious decision are
involved.
➢ Syntax of nested if...else statement.:
if (test expression)
statements to be executed if test expression is true;
else if(test expression 1)
statements to be executed if test expressions 1 is true;
else if (test expression 2)
.
.
.
else
statements to be executed if all test expressions are false;
.
Decision making with Nested if...else statement
➢ Example:
void main()
{
int num;
clrscr();
printf(“Enter any number”);
scanf(“%d”,&num);
if(num>0)
{
printf("%d is Positive\n",num);
}
else if(num<0)
{
printf("%d is Negative\n",num);
}
else
{
printf("%d is Zero\n",num);
}
getch();
}
.
switch....case Statement
Decision making are needed when, the program encounters the situation to choose a particular
statement among many statements. If a programmer has to choose one among many alternatives
if...else can be used but, this makes programming logic complex.
• Syntax of switch...case:
switch (Option)
{
case Option1 :
Statements;
break;
case Option2 :
Statements;
break;
[ Default :]
codes to be executed if Option doesn't match to any
}
.
switch....case Statement
Example-
Option=4;
switch(option)
{
case 1:
printf(“ONE”);
break;
case 2:
printf(“TWO”);
break;
case 3:
printf(“THREE”);
break;
case 4:
printf(“FOUR”);
break;
Default : printf(“Enter Valid Option”);
}
.
switch....case Statement
Example-
switch(operator)
{
case'+':
printf("num1+num2=%.2f",num1+num2)
;break;
case'-':
printf("num1-num2=%.2f",num1-num2);
break;
case'*':
printf("num1*num2=%.2f",num1*num2)
;break;
case'/':
printf("num2/num1=%.2f",num1/num2)
;break;
default:/* if operator is other than +, -, * or /, error message is
shown */ printf(Error!operatorisnot correct");
break;
}
.
C Program to Check Vowel or Consonant
/* Source Code to Check Whether a Character is Vowel or consonant*/
#include <stdio.h>
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c==
'o'||c=='O'||c=='u'||c=='U')
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}
Output 1
Enter an alphabet: i
i is a vowel.
Output 2
Enter an alphabet: G
G is a consonant.
.
C Program to Check whether a year is leap year or not
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
.
C Program to Check whether a year is leap year or not
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
Output 1
Enter year: 2000
2000 is a leap year.
Output 2
Enter year: 1900
1900 is not a leap year
.
Source Code to Make Simple Calculator in C programming
Example-
# include <stdio.h>
int main()
{
char operator;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&operator);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f",num1+num2);
break;
case '-':
printf("num1-num2=%.2f",num1-num2);
break;
.
case '*':
printf("num1*num2=%.2f",num1*num2);
break;
case '/':
printf("num2/num1 = %.2f",num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
return 0;
}
Output
Enter operator either + or - or * or divide: /
Enter two operands: 13.456
4.56
num2/num1 = 2.95
.
4.Iterative/Looping Statements.
.
Looping Statements
➢ C programming loops:
➢ Loops causes program to execute the certain block of code repeatedly until some
conditions are satisfied, i.e., loops are used in performing repetitive work in programming.
.
Loops: for.
.
For loop
.
Loops: while
➢ Example of while loop
i=1;
While(i<=n)
{
printf(“%d”,i);
i++;
}
1 2 3 4 5 6 7 8 9 10
.
do...while loop
do
/* Codes inside the body of do...while loops are at least executed
once. */
{
printf("Enter a number\n");
scanf("%d",&num);
sum+=num;
}
while(num!=0);
.
break Statement:
break Statement:
In C programming, break is used in terminating the loop immediately after it is encountered. The
break statement is used with conditional if statement.
break Example:
for(i=1;i<=n;++i)
{
printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)
break; //for loop breaks if num<0.0
sum=sum+num;
}
.
continue Statement
continue Statement
➢ It is sometimes desirable to skip some statements inside the loop. In such
cases, continue statements are used.
.
continue Statement
Example:
if(num==0)
continue; / *In this program, when num equals to zero, it
skips the statement product*=num and continue the loop. */
product*=num;
.
Source Code to C Program to Reverse an Integer
/*C Program to Reverse an Integer*/
#include <stdio.h>
int main()
{
int n, reverse=0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number = %d",reverse);
return 0;
}
Output
Enter an integer: 2345
Reversed Number = 5432
.
Source Code to c Program Sum of Integers
/* This program is solve using for loop. */
#include <stdio.h>
int main()
{
int n, count, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
for(count=1;count<=n;++count) /* for loop terminates if count>n */
{
sum+=count; /* sum=sum+count */
}
printf("Sum = %d",sum);
return 0;
}
Output
Enter an integer: 100
Sum = 5050
.
Goto statement
label:
statement;
e.G
goto a;
printf(“hello”);
a: printf(“Welcome”);
.
Thank You