Basics of C Language - Advanced
Basics of C Language - Advanced
By
Chinnu Edwin A
M.Tech. CS
CONTENTS
• Introduction
• C Program Structure
• Parts of C Program
• C Basic Syntax
• C Control Statements
INTRODUCTION
• A programming language
• Developed by Dennis Ritchie in 1972
• Structured language
• Easy to learn
• Produces efficient programs
SAMPLE C PROGRAM
STRUCTURE
#include <stdio.h>
int main()
{
/* printf function displays the content present in
double quotes. */
printf("Hello World");
return 0;
•}
PARTS OF C PROGRAM
• A C program basically consists of the following parts −
• Preprocessor Commands
• Functions
• Variables
• Statements & Expressions
• Comments
C BASIC SYNTAX
C LANGUAGE TOKENS
• It tells the compiler where and how much storage to create for the variable.
• A variable definition specifies a data type and contains a list of one or more variables of that type
as follows −
type variable_list;
• Constants are fixed values that the program may not alter during its
execution.
• They are also called literals.
• Constants can be of any of the basic data types like an integer
constant, a floating constant, a character constant, or a string literal.
There are enumeration constants as well.
C - Constants and Literals(contd..)
• Following are other examples of various types of integer literals −
• 85 /* decimal */
• 0213 /* octal */
• 0x4b /* hexadecimal */
• 30 /* int */
• 30u /* unsigned int */
• 30l /* long */
30ul /* unsigned long */
C - Constants and Literals (contd..)
• Here are some examples of floating-point literals −
• 3.14159 /* Legal */
314159E-5L /* Legal */
C - Constants and Literals (contd..)
• Character literals are enclosed in single quotes
• A character literal can be
- a plain character (e.g., 'x'),
- an escape sequence (e.g., '\t'),
C - Constants and Literals (contd..)
String Literals
• String literals or constants are enclosed in double quotes "".
• A string contains characters that are similar to character literals: plain
characters, escape sequences, and universal characters.
• Eg. "hello, dear"
C- Operators
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions.
• C language is rich in built-in operators and provides the following types of
operators −
i. Arithmetic Operators
ii. Relational Operators
iii. Logical Operators
iv. Bitwise Operators
v. Assignment Operators
vi. Misc Operators
C- Operators (contd..)
• The following table shows all the arithmetic operators supported by
the C language.
• Assume variable A holds 10 and variable B holds 20 then
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after an integer division. B%A=0
++ Increment operator increases the integer value by one. A++ = 11
-- Decrement operator decreases the integer value by one. A-- = 9
== Checks if the values of two operands (A == B) is not true.
are equal or not. If yes, then the
condition becomes true.
!= Checks if the values of two operands (A != B) is true.
are equal or not. If the values are not
equal, then the condition becomes
true.
> Checks if the value of left operand is (A > B) is not true.
• Relational Operators greater than the value of right
operand. If yes, then the condition
becomes true.
< Checks if the value of left operand is (A < B) is true.
less than the value of right operand. If
yes, then the condition becomes true.
&& Called Logical AND operator. If both the operands are non- (A && B) is false.
zero, then the condition becomes true.
! Called Logical NOT Operator. It is used to reverse the logical !(A && B) is true.
state of its operand. If a condition is true, then Logical NOT
operator will make it false.
Bitwise Operators
| Bitwise OR
^ Bitwise XOR
~ Bitwise complement
&= sum&=10;
This is same as sum = sum & 10
^= sum ^= 10;
This is same as sum = sum ^ 10
CONTROL STATEMENTS IN
C
If statement
EXAMPLE FOR IF STATEMENT
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
EXAMPLE FOR IF STATEMENT
(CONTD..)
Output :
Variable x is less than y
SWITCH STATEMENT
• Switch statement in C tests the value of a variable and compares it
with multiple cases.
• Once the case match is found, a block of statements associated with
that particular case is executed.
SYNTAX FOR SWITCH
STATEMENT
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
EXAMPLE FOR SWITCH
STATEMENT
• switch (n)
•{
• case 1: // code to be executed if n = 1;
• break;
• case 2: // code to be executed if n = 2;
• break;
• default: // code to be executed if n doesn't match any cases
•}
WHILE STATEMENT
• while (condition)
{
statements;
}
Program illustrates while loop in C
programming example
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
DO- WHILE STATEMENT
do
{
//Statements
}while(condition test);
EXAMPLE FOR DO-WHILE
STATEMENT
#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}
EXAMPLE FOR DO-WHILE STATEMENT
• Output:
Output
1 2 3 4 5 6 7 8 9 10
C break statement
• The break is a keyword in C which is used to bring the program control out of the loop.
• The break statement is used inside loops or switch statement.
• The break statement breaks the loop one by one, i.e., in the case of nested loops, it
breaks the inner loop first and then proceeds to outer loops.
• The break statement in C can be used in the following two scenarios:
- With switch case
- With loop
Syntax:
//loop or switch case
break;
CONTINUE STATEMENTS IN C
• The continue statement is used inside loops.
• When a continue statement is encountered inside a loop, control
jumps to the beginning of the loop for next iteration, skipping the
execution of statements inside the body of loop for the current
iteration.
Example: Continue statement inside for
loop
#include <stdio.h>
int main()
{
for (int j=0; j<=8; j++)
{
if (j==4)
{
/* The continue statement is encountered when
* the value of j is equal to 4.
*/
continue;
}
•
Example: Continue statement inside for loop (CONTD..)
• Output
01235678
GOTO STATEMENT
• The goto statement is a jump statement which is sometimes also
referred to as unconditional jump statement.
• The goto statement can be used to jump from anywhere to anywhere
within a function.
GOTO STATEMENT
EXAMPLE FOR GOTO
STATEMENT
// C program to print numbers
// from 1 to 10 using goto statement
#include <stdio.h>
OUTPUT
1 2 3 4 5 6 7 8 9 10
THANK YOU