1 of 45

C Language

Modules-02: Variable, Keyword, Datatypes & Type of Operator

National Institute of Electronics & Information Technology

Ministry of Electronics & Information Technology (MeitY), Government of India

Gorakhpur Center

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

2 of 45

Contents to be covered

  • Variable
  • Keyword
  • Data Type
  • Types of Operators
  • Basic programming Examples

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

3 of 45

The fundamental components of a program

  • The fundamental components of a program include constants, identifiers, operators, keywords, and comments. Each serves a distinct purpose in defining and structuring the program's logic and data.
  • Constants: 
  • These are fixed values that do not change during the execution of a program. They can be numeric (e.g., 10, 3.14), character (e.g., 'A', 'z'), or string literals (e.g., "Hello, World!"). Constants are used to represent values that are known at compile time and remain immutable.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

4 of 45

  • Identifiers: 
  • These are user-defined names given to various program entities such as variables, functions, classes, or labels. Identifiers allow programmers to refer to specific data or code blocks within the program. They must follow specific naming rules defined by the programming language, typically involving alphanumeric characters and underscores, and often starting with a letter or underscore.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

5 of 45

  • Character Set
  • A character refers to the digit, alphabet or special symbol used to data representation.
  • Alphabets :          A-Z, a-z
  • Digits : 0-9
  • Special Characters : ~ ! @ # $ % ^ & * ( ) _ + { } [ ] - < > , . / ? \ | : ; " '
  • White Spaces : Horizontal tab, Carriage return, New line

  • Variable
  • It is a data name which is used to store data and may change during program execution. It is opposite to constant. Variable name is a name given to memory cells location of a computer where data is stored.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

6 of 45

Rules for Variables

  • First character should be letter or alphabet.
  • Keywords are not allowed to use as a variable name.
  • White space is not allowed.
  • C is case sensitive i.e. UPPER and lower case are significant.
  • Only underscore, special symbol is allowed between two characters.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

7 of 45

  • Keywords: 
  • Also known as reserved words, these are special words that have predefined meanings in a programming language and cannot be used as identifiers. Keywords are integral to the language's syntax and are used to define data types, control flow (e.g., if, else, for, while), storage classes (e.g., static, extern), and other fundamental language constructs.
  • Keywords are the system defined identifiers.
  • All keywords have fixed meanings that do not change.
  • White spaces are not allowed in keywords.
  • Keyword may not be used as an identifier.
  • It is strongly recommended that keywords should be in lower case letters.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

8 of 45

Reserved Keywords of C Language

  • There are totally 32 (Thirty Two) keywords used in a C programming.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

9 of 45

Constant in C

  • A constant is an entity that doesn't change during the execution of a program.

Syntax: const data type var_name=expression;

Example: const float pi=3.147

Followings are the different types of constants :

1. Real Constant :

It must have a decimal point which may be positive or negative.

Example: +194.143, -416.41

2. Integer Constant :

It should not contain a decimal place.

It can be positive or negative.

Example: 1990, 194, -394

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

10 of 45

Constant in C Contd..

3. Character Constant :

It is a single alphabet or a digit or a special symbol enclosed in a single quote.

Maximum length of a character constant is 1.

Example: 'T', '9', '$'

4. String Constant :

It may contain letters, digits, special characters and blank space.

Example: “Hello friends"

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

11 of 45

Backslash Character Constant in C

  • C supports some special escape sequence characters that are used to do special tasks.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

12 of 45

Comment in C

  • Comments in C are of the general of

  • /* . . . . . * /

  • Comments can be inserted anywhere you can put a space (blank). Comments are ignored by the C compiler and not included in the executable file.
  • �The */ can be on the next line but then every character between the /* and the */ is ignored by the C compiler.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

13 of 45

Translator

  • In computer programming, a translator is a software tool that converts human-readable source code written in a high-level programming language into machine code or another lower-level form that a computer's processor can understand and execute. The main types of translators are  compilers, interpreters & assemblers

Compilers, which translate an entire program at once into an executable file;

 Interpreters, which translate and execute code line by line; and

 Assemblers, which convert low-level assembly language into machine code. 

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

14 of 45

Editor Vs compiler

  • An editor is a tool for writing and managing source code (text files), while a compiler is a program that translates that human-readable source code into a machine-readable format, like binary code or bytecode, allowing the computer to execute the program. An editor handles text manipulation, and the compiler performs the translation, with both being essential for software development.  

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

15 of 45

Error in Programming

  • Syntax Errors

The C compiler will catch these errors and give you error messages,such as Missing Parenthesis (}), Missing semicolon, Printing the value of variable without declaring it.

Example: x + 1 = x; (should be x = x+1; for a valid assignment statement)

  • Run-time Errors

The C compiler will not catch these errors. Such as division by zero error.

Example: User enters the value 0 and your code reads this value into � variable x, and then computes 1/x .

  • Logical Errors

The C compiler will not catch these errors. Program will run and not generate any error messages but results outputted by the program are incorrect.

Example: User programs solution using the wrong formula.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

16 of 45

Data types in C

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

17 of 45

Data Types in C

  • Integer :
  • To access and to store any integer value int data type is use. C’s int data type occupies 2 bytes in the memory.
  • To declare any integer variable : int variable_name;

  • Float :
  • To access and to store any real value float data type is use. C’s float data type occupies 4 bytes in the memory.
  • To declare any float variable : float variable_name;

  • Character :
  • To access and to store single character it use char data type in C. Char data type occupies 1 bytes. It means that char data type use 1 byte of memory to store some character value.
  • Declaration of character variable : char variable_name;

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

18 of 45

Data Types in C

“Data type can be defined as the type of data of variable or constant store.”

  • Followings are the most commonly used data types in C.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

19 of 45

Variable Declaration and definition

  • Declaration of a variable is for informing the compiler of the following information: name of the variable and the type of the value it will hold i.e., declaration gives details about the properties of a variable.

Syntax:Data Type Variable Name

Ex: int a;

  •  Definition of a variable, memory is allocated for the variable.
  • In C language definition and declaration for a variable takes place at the same time. i.e. there is no difference between declaration and definition. For example, consider the following statement,
  • int a;
  • Here, the information such as the variable name: a, and data type: int, is sent to the compiler which will be stored in the data structure known as the symbol table. Along with this, a memory of size 2 bytes(depending upon the type of compiler) will be allocated.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

20 of 45

  • Suppose, if we want to only declare variables and not define them i.e. we do not want to allocate memory, then the following declaration can be used
  • extern int a;
  • In this example, only the information about the variable name and type is sent and no memory allocation is done. The above information tells the compiler that the variable a is declared now while memory for it will be defined later in the same file or in a different file.

Variable Declaration and definition

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

21 of 45

Initialization and Assignment:��

  • Initialization occurs at the time of a variable's declaration. It is the process of assigning an initial value to a newly created variable.
  • The purpose of initialization is to ensure that a variable starts with a defined, predictable value, preventing it from containing "garbage" or uninitialized data.

int a=10

  • Assignment occurs after a variable has already been declared and potentially initialized. It is the process of changing the value of an existing variable.
  • The purpose of assignment is to update the value stored in a variable during the program's execution.

a=100

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

22 of 45

Type Promotion Rules

  • In C, type promotion, particularly in expressions involving operands of different types, follows a set of rules to determine a common type for the operation. These rules are applied to ensure that operations are performed without loss of precision where possible.
  • Floating-Point Promotions:
  • If one operand is long double, the other is converted to long double. 
  • Otherwise, if one operand is double, the other is converted to double. 
  • Otherwise, if one operand is float, the other is converted to float

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

23 of 45

Integer Promotions:�

  • If none of the operands are floating-point types, integer promotions are performed:
  • Types smaller than int (e.g., char, signed char, unsigned char, short int, unsigned short int, _Bool, and bit-fields) are converted to int if int can represent all values of the original type.
  • Otherwise, they are converted to unsigned int

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

24 of 45

Types of Operator

  • Operator is a symbol that is used to perform operations.
  • Followings are the most commonly used operators in C.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

25 of 45

Arithmetic Operator

  • Arithmetic operators are used to perform numerical calculations among the values.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

26 of 45

Example of Arithmetic Operator

#include<stdio.h>

int main()

{

int a, b, add, sub, mul, div, rem;

printf("Enter a, b values : ");

scanf("%d%d",&a,&b); // Reading two values

add=a+b; // Addition Operator

sub=a-b; // Subtraction Operator

mul=a*b; // Multiplication Operator

div=a/b; // Division Operator

rem=a%b; // Remainder (Modulo) Operator

printf("Result of addition is=%d\n", add);

printf("Result of subtraction=%d\n", sub);

printf("Result of multiplication is=%d\n", mul);

printf("Result of division is=%d\n", div);

printf("Result of remainder=%d\n",rem);

return 0;

}

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

27 of 45

Logical Operator

  • These operators are used for testing more than one condition and making decisions.
  • C has three logical operators they are:

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

28 of 45

Example of Logical Operator

Example:

#include<stdio.h>

void main()

{

int a, b;

printf(“Enter values for a and b : ");

scanf(“%d %d", &a, &b);

printf("\n %d",(a<b)&&(a!=b));

printf("\n %d",(a<b)||(b<a));

printf("\n %d",!(a==b));

}

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

29 of 45

Relational Operator

  • Relational Operators are used to compare two quantities and take certain decision depending on their relation.
  • If the specified relation is true it returns one.
  • If the specified relation is false it returns zero.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

30 of 45

Example of Relational Operator

Example:

#include<stdio.h>

void main()

{

int a, b;

printf(“Enter values for a and b : ");

scanf("%d %d", &a, &b);

printf("\n The < value of a is %d", a<b);

printf("\n The <= value of a is %d", a<=b);

printf("\n The > value of a is %d", a>b);

printf("\n The >= value of a is %d", a>=b);

printf("\n The == value of a is %d", a==b);

printf("\n The != value of a is %d", a!=b);

}

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

31 of 45

Increment/Decrement Operator

  • C includes a unary increment operator (++) and decrement operator (--).
  • Increment and decrement operators increase and decrease a value stored in a number variable by 1.

For example, the expression,

count = count + 1; //increment the value of count by 1 is equivalent to count++;

Operator Use Description

++ i++ Increments i by 1; evaluates to

the value of i before it was incremented

++ ++i Increments i by 1; evaluates to

the value of i after it was incremented

-- i-- Decrements i by 1; evaluates to

the value of i before it was decremented

-- --i Decrements i by 1; evaluates to

the value of i after it was decremented

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

32 of 45

Example of Increment/Decrement Operator

Example:

#include<stdio.h>

void main()

{

int a,b,c;

printf("Enter the values for a and b :");

scanf("%d %d", &a, &b);

printf("\n The value of c is %d", c=++a);

printf("\n The value of c is %d", c=a++);

printf("\n The value of c is %d", c=--b);

printf("\n The value of c is %d", c=b--);

}

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

33 of 45

Conditional Operator

  • The conditional operator ? : is a ternary operator. This means that it takes in three arguments that together form a conditional expression.

Syntax:

exp1 ? exp2 : exp3;

  • Where in exp1 is a Boolean expression whose result must either be true or false. If exp1 is true, exp2 is the value returned. If it is false, then exp3 is returned.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

34 of 45

Example of Conditional Operator

Example:

void main()

{

int x;

printf("enter any number");

scanf("%d",&x);

x>0? printf("Positive"):printf("Negative");

}

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

35 of 45

Bitwise Operator

  • These operators works on bit level
  • Applied to Integers only.

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

36 of 45

Example of Bitwise Operator

let , a=10 b=8

a= 1010 (in binary)

b= 1000 (in binary)

Bitwise AND operator(&)

1010

1000

&

1000

8

4

2

1

1

0

0

0

output =8

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

37 of 45

Bitwise (OR) operator(|)

1010

| 1000

1010

8

4

2

1

1

0

1

0

Output= 10

Bitwise (XOR) operator (^)

1010

^ 1000

0010

8

4

2

1

0

0

1

0

Output=2

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

38 of 45

Bitwise complement operator (~)

If a=10

b = ~a

Then , b= - (a+1)

b=-11

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

39 of 45

Example of Bitwise Operator

x=2 (in binary 0010 )

y=7 ( in binary 0111 )

Right Shift(>>)

Z = x>>2

Z= 0

8

4

2

1

0

0

1

0

0

0

0

0

Remaining fill by 0

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

40 of 45

NOTE:

  • For multiply given number by two, left shifted by one time, i.e., a<<1
  • For divide given number by two, right shifted by one time, i.e., a>>1

Left shift (<<)

Z= x<<2

Z= 8

8

4

2

1

0

0

1

0

1

0

0

0

Remaining fill by 0

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

41 of 45

Example of Bitwise Operator

Example:

#include<stdio.h>

#include<conio.h>

Void main()

{

int a,b;

printf(“enter the value of a & b”);

scanf(“%d%d”,&a,&b);

printf(“AND %d\n”, a&b);

printf(“OR %d\n”, a|b);

printf(“XOR %d\n”, a^b);

printf(“COMPLE %d\n”, ~a);

printf(“LeftS %d\n”, a<<2);

printf(“RightS %d\n”, a>>2);

getch();

}

If , a=10 b=8

Output—

AND =8

OR = 10

XOR=2

COMPLE= -11

LeftS= 40

RightS=2

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

42 of 45

Arity operator

  • An "arity" or "arity operator" refers to the number of arguments or operands that an operator or function takes. For example, a unary operator takes one operand (like negation -x), a binary operator takes two (like addition x + y), and a ternary operator takes three (like the ?: conditional operator). Some functions and operators are also variadic, meaning they can accept a variable number of arguments. 
  • Unary (Arity 1): An operator or function that operates on a single operand or argument.Example: The negation operator - in -5. 
  • Binary (Arity 2): An operator or function that operates on two operands or arguments.Example: The addition operator + in 2 + 3. 
  • Ternary (Arity 3): An operator or function that operates on three operands or arguments.Example: The conditional (ternary) operator ?: in condition ? value_if_true : value_if_false. 

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

43 of 45

Operator precedence

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

44 of 45

1. If a = 20 , b=10,c=15,d=5 .find the value of e

    • e = (a + b) * c / d;
    • e = ((a + b) * c) / d;
    • e = (a + b) * (c / d);
    • e = a + (b * c) / d;

e = a + (b * c) / d;

Assignments

2 . Program to add, subtract, multiply, division of two numbers

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia

45 of 45

Thank You

Any Query

/GKP.NIELIT

http://www.nielit.gov.in/gorakhpur

@GKP_NIELIT

/NIELITIndia

/school/NIELITIndia