0% found this document useful (0 votes)
93 views

Basics of C Language - Advanced

C is a general-purpose programming language developed in the 1970s. A C program consists of preprocessing commands, functions, variables, statements, and expressions. It has basic syntax elements like semicolons, comments, identifiers, keywords, data types, variables, constants and literals. C supports various operators like arithmetic, relational, logical, bitwise and assignment operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Basics of C Language - Advanced

C is a general-purpose programming language developed in the 1970s. A C program consists of preprocessing commands, functions, variables, statements, and expressions. It has basic syntax elements like semicolons, comments, identifiers, keywords, data types, variables, constants and literals. C supports various operators like arithmetic, relational, logical, bitwise and assignment operators.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Basics of C Language

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

• Smallest individual units in C program


•  A keyword, an identifier, a constant, a string literal, or a symbol
               C LANGUAGE TOKENS
(contd..)
                       SEMICOLONS
   
• In a C program, the semicolon is a statement terminator.
•  That is, each individual statement must be ended with a semicolon.
•  Given below are two different statements −
•       printf("Hello, World! \n");
      return 0;
                         COMMENTS
• Comments are like helping text in your C program 
• They are ignored by the compiler. 
• They start with /* and terminate with the characters */ as shown
below −
• /* my first program in C */
                          IDENTIFIERS
• A C identifier is a name used to identify a variable, function, or any other
user-defined item. 
• An identifier starts with a letter A to Z, a to z, or an underscore '_' followed
by zero or more letters, underscores, and digits (0 to 9).
• C does not allow punctuation characters such as @, $, and % within
identifiers.
•  C is a case-sensitive programming language.
• Some examples of acceptable identifiers −
• mohd       zara    abc   move_name  a_123
• myname50   _temp   j     a23b9      retVal
                                  KEYWORDS
• The reserved words may not be used as constants or variables or any
other identifier names.
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
                        C – DATA TYPES
• Data types is used for declaring variables or functions of different
types. 
• The type of a variable determines how much space it occupies in
storage
                    C – DATA TYPES (contd..)
                C – DATA TYPES (contd..)

Data type Size(in bytes) Range

 character 1 -128 to 127

 integer 2 -32,768 to 32,767

 floating 4 -3.4e-38 to 3.4e+38


                        C – VARIABLES 
• A variable is a name given to a storage area that our programs can
manipulate.
• It is a data name that is used to store any data value.
• The name of a variable can be composed of letters, digits, and the
underscore character. 
• It must begin with either a letter or an underscore. 
• Upper and lowercase letters are distinct because C is case-sensitive.
                Variable Definition in C

• 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;

Some valid declarations are shown here −


int    i, j, k;
char   c, ch;
float  f, salary;
double d;
C - Constants and Literals

• 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.

>= Checks if the value of left operand is (A >= B) is not true.


greater than or equal to 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 or equal to the value of right
operand. If yes, then the condition
becomes true.
         
                           Logical Operators

Operator Description Example (A holds 1 and B holds 0)

&& Called Logical AND operator. If both the operands are non- (A && B) is false.
zero, then the condition becomes true.

|| Called Logical OR Operator. If any of the two operands is (A || B) is true.


non-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

Operators Meaning of operators

& Bitwise AND

| Bitwise OR

^ Bitwise XOR

~ Bitwise complement

<< Shift left

>> Shift right


               Assignment Operators
Operators Example/Description
sum = 10;
=
10 is assigned to variable sum
sum += 10;
+= This is same as sum = sum + 10
sum -= 10;
-=
This is same as sum = sum – 10
sum *= 10;
*= This is same as sum = sum * 10
sum /= 10;
/=
This is same as sum = sum / 10
sum %= 10;
%=
This is same as sum = sum % 10

&= 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:

• Value of variable j is: 0


• Value of variable j is: 1
• Value of variable j is: 2
• Value of variable j is: 3
FOR CONTROL
STATEMENT
• The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)


{
// statements inside the body of loop
}
Example for FOR Control
Statements
Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)

{ printf("%d ", i);


}
return 0;
}
Example for FOR Control Statements

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..)

/* This print statement would not execute for the


* loop iteration where j ==4 because in that case
* this statement would be skipped.
*/
printf("%d ", j);
}
return 0;
}
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>

// function to print numbers from 1 to 10


void printNumbers()
{
int n = 1;
label:
printf("%d ",n);
n++;
if (n <= 10)
goto label;
}
EXAMPLE FOR GOTO STATEMENT (CONTD..)

// Driver program to test above function


int main() {
printNumbers();
return 0;
}

OUTPUT
1 2 3 4 5 6 7 8 9 10
THANK YOU

You might also like