0% found this document useful (0 votes)
26 views33 pages

C Operators

Uploaded by

jasase6236
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views33 pages

C Operators

Uploaded by

jasase6236
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

C programming keywords

and identifiers
Presented by: Lochan raj dahal
▪ All the words used in C program which have fixed predefined
meaning and whose meanings can’t be changed by the users are
termed as Keywords. There are fixed number of keywords in C
programming language and every keyword serves as a building
block for program statements.

C List of Keywords of ANSI C


Programming auto double int struct
Keywords char extern return union
break else long switch
case enum register typedef
const float short unsigned
continue for signed void
do if static while
default goto sizeof volatile

Note: Since C is a case sensitive programming language all the keywords must be written in lowercase.
▪ In the real world, when a new baby is born a name is given.
Similarly, in C program when a new variable, function or an
array is declared a particular name is given to them which is
called an identifier. For example

C
Programmin int apple;

g Identifiers ▪ Here, apple is an identifier of integer type variable.

▪ So identifiers are the user defined names. However, there are


certain rules that must be followed while writing an identifier.
• First letter of an identifier must be alphabet (underscore is also
allowed)

• Identifier can only contain letters, digits and underscores


Rules for • Maximum length of identifier allowed is 31 characters
writing an
• White space is not allowed
identifier
• Keywords cannot be used as identifier
▪ 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.
▪ Let's see the syntax to declare a variable:
type variable_list;
▪ The example of declaring the variable is given below:
1. int a;
Variables in C 2. float b;
3. char c;
▪ Here, a, b, c are variables. The int, float, char are the data types.
▪ We can also provide values while declaring the variables as given below:
1. int a=10,b=20;//declaring 2 variable of integer type
2. float f=20.8;
3. char c='A';
• 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.
Rules for int, float, etc.

defining Valid variable Invalid variable


variables names:
int a;
names:
int 2;
int _ab; int a b;
int a30; int long;
▪ There are many types of variables in c:
1. Local variable
2. Global variable
3. Static variable
Types of 4. Automatic variable
Variables in C 5. External variable
▪ A variable that is declared inside the function or block is
called a local variable.

▪ It must be declared at the start of the block.


void function1()
{
Local int x=10;//local variable
Variable }
▪ You must have to initialize the local variable before it is
used.
▪ A variable that is declared outside the function or
block is called a global variable. Any function can
change the value of the global variable. It is
available to all the functions.
▪ It must be declared at the start of the block.
Global int value=20;//global variable
Variable void function1(){
int x=10;//local variable
}
▪ A variable that is declared with the static keyword is called
static variable.
▪ It retains its value between multiple function calls.
void function1(){
int x=10;//local variable
static int y=10;//static variable
Static x=x+1;
y=y+1;
Variable printf("%d,%d",x,y);
}
▪ If you call this function many times, the local variable will print the
same value for each function call, e.g, 11,11,11 and so on. But the static
variable will print the incremented value in each function call, e.g. 11,
12, 13 and so on.
▪ All variables in C that are declared inside the block,
are automatic variables by default. We can
explicitly declare an automatic variable using auto
keyword.
void main()
Automatic {
Variable int x=10;//local variable (also automatic)
auto int y=20;//automatic variable
}
▪ We can share a variable in multiple C source files by
using an external variable. To declare an external
variable, you need to use extern keyword.
▪ myfile.h
extern int x=10;//external variable (also global)

External ▪ program1.c
Variable #include "myfile.h"
#include <stdio.h>
void printValue(){
printf("Global variable: %d", x);
}
▪ A data type specifies the type of data that a variable can store
such as integer, floating, character, etc.
There are the following data types in C language.

Datatypes in
C
programmin
g 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
▪ The basic data types are integer-based and floating-point based. C
language supports both signed and unsigned literals.
▪ The memory size of the basic data types may change according to 32
or 64-bit operating system.
▪ Let's see the basic data types. Its size is given according to 32-bit
architecture.
Data Types Memory Size Range

Basic Data char


signed char
1 byte
1 byte
−128 to 127
−128 to 127
Types unsigned char 1 byte 0 to 255

short 2 byte −32,768 to 32,767


signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
Data Types Memory Size Range

short int 2 byte −32,768 to 32,767


signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to 2,147,483,647
signed long int 4 byte -2,147,483,648 to 2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
Basic Data float 4 byte
Types double 8 byte
long double 10 byte
▪ Integers are entire numbers without any fractional or decimal
parts, and the int data type is used to represent them.

▪ It is frequently applied to variables that include values, such


as counts, indices, or other numerical numbers. The int data
type may represent both positive and negative
Int: numbers because it is signed by default.

▪ An int takes up 4 bytes of memory on most devices, allowing


it to store values between around -2 billion and +2 billion.
▪ Individual characters are represented by
the char data type. Typically used to
hold ASCII or UTF-8 encoding scheme
characters, such as letters, numbers, symbols,
Char: or commas. There are 256 characters that can
be represented by a single char, which takes up
one byte of memory. Characters such as 'A',
'b', '5', or '$' are enclosed in single quotes.
▪ To represent integers, use the floating data type. Floating
numbers can be used to represent fractional units or
numbers with decimal places.

▪ The float type is usually used for variables that require


Float: very good precision but may not be very precise. It can
store values with an accuracy of about 6 decimal
places and a range of about 3.4 x 1038 in 4 bytes of
memory.
▪ Use two data types to represent two floating integers. When
additional precision is needed, such as in scientific calculations or
financial applications, it provides greater accuracy compared to float.
▪ Double type, which uses 8 bytes of memory and has an accuracy of
about 15 decimal places, yields larger values. C treats floating point
numbers as doubles by default if no explicit type is supplied.
int age = 25;
char grade = 'A';
Double: float temperature = 98.6;
double pi = 3.14159265359;
▪ In the example above, we declare four variables: an int variable for
the person's age, a char variable for the student's grade, a float
variable for the temperature reading, and two variables for
the number pi.
▪ Beyond the fundamental data types, C also
supports derived data types, including arrays,
Derived Data pointers, structures, and unions. These data types
give programmers the ability to handle
Type heterogeneous data, directly modify memory, and
build complicated data structures.
▪ An array, a derived data type, lets you store a
sequence of fixed-size elements of the same type. It
provides a mechanism for joining multiple targets of
the same data under the same name.
▪ The index is used to access the elements of the array,

Array: with a 0 index for the first entry. The size of the
array is fixed at declaration time and cannot be
changed during program execution. The array
components are placed in adjacent memory regions.
Here is an example of declaring and utilizing an array:
#include <stdio.h>

int main() {
int numbers[5]; // Declares an integer array with a size
of 5 elements
// Assign values to the array elements
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
Array: numbers[4] = 50;

// Display the values stored in the array


printf("Values in the array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0; Output:
}
Values in the array: 10 20 30 40 50
▪ A pointer is a derived data type that keeps track of another data type's
memory address. When a pointer is declared, the data type it refers to
is stated first, and then the variable name is preceded by an asterisk (*).

▪ You can have incorrect access and change the value of variable using
pointers by specifying the memory address of the variable. Pointers are
commonly used in tasks such as function pointers, data structures,
and dynamic memory allocation.

▪ Here is an example of declaring and employing a pointer:


#include <stdio.h>
Pointer: int main() {
int num = 42; // An integer variable
int *ptr; // Declares a pointer to an integer

ptr = &num; // Assigns the address of 'num' to the pointer

// Accessing the value of 'num' using the pointer


printf("Value of num: %d\n", *ptr);
Output:
return 0;
Value of num: 42
}
A structure is a derived data type that enables the creation of composite
data types by allowing the grouping of many data types under a single
name. It gives you the ability to create your own unique data structures
by fusing together variables of various sorts.
1. A structure's members or fields are used to refer to each variable
within it.
Structure: 2. Any data type, including different structures, can be a member
of a structure.
3. A structure's members can be accessed by using the dot (.)
operator.
A declaration and use of a structure is demonstrated here:
#include <stdio.h>
#include <string.h>
// Define a structure representing a person
struct Person {
char name[50];
int age;
float height;
};

int main() {
// Declare a variable of type struct Person
struct Person person1;
Structure: // Assign values to the structure members
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 1.8;

// Accessing the structure members


printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age); Output:
printf("Height: %.2f\n", person1.height);
Name: John Doe
return 0;
Age: 30
}
Height: 1.80
▪ A derived data type called a union enables you to store
various data types in the same memory address. In
contrast to structures, where each member has a
separate memory space, members of a union all share a
single memory space. A value can only be held by one
member of a union at any given moment. When you
Union: need to represent many data types interchangeably,
unions come in handy. Like structures, you can access
the members of a union by using the dot (.) operator.
▪ Here is an example of a union being declared and used:
#include <stdio.h>
// Define a union representing a numeric value
union NumericValue {
int intValue;
float floatValue;
char stringValue[20];
};
int main() {
// Declare a variable of type union NumericValue
union NumericValue value;
// Assign a value to the union
value.intValue = 42;
Union: // Accessing the union members
printf("Integer Value: %d\n", value.intValue);
// Assigning a different value to the union
value.floatValue = 3.14;
// Accessing the union members
printf("Float Value: %.2f\n", value.floatValue);

return 0; Output:
}
Integer Value: 42
Float Value: 3.14
▪ A set of named constants or enumerators that represent a
collection of connected values can be defined in C using
the enumeration data type (enum). Enumerations give you
the means to give names that make sense to a group of integral
values, which makes your code easier to read and maintain.
Enumeration ▪ Here is an example of how to define and use an enumeration
Data Type in C:
#include <stdio.h>
// Define an enumeration for days of the week
enum DaysOfWeek {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
Enumeration int main() {
// Declare a variable of type enum DaysOfWeek
Data Type enum DaysOfWeek today;

// Assign a value from the enumeration


today = Wednesday;

// Accessing the enumeration value


printf("Today is %d\n", today);
Output:
return 0;
} Today is 2
The void data type in the C language is used to denote the lack of a particular
type. Function return types, function parameters, and pointers are three
situations where it is frequently utilized.

▪ Function Return Type:


▪ A void return type function does not produce a value. A void function executes
a task or action and ends rather than returning a value.

▪ Example:
Void Data
Type void printHello() { printf("Hello, world!\n"); }

▪ Function Parameters:
▪ The parameter void can be used to indicate that a function accepts no
arguments.

▪ Example:
void processInput(void) { /* Function logic */ }
▪ Any address can be stored in a pointer of type void*, making it a
universal pointer. It offers a method for working with pointers to
ambiguous or atypical types.

▪ Example:

void* dataPtr;
Pointers: ▪ The void data type is helpful for defining functions that don't accept any
arguments when working with generic pointers or when you wish to
signal that a function doesn't return a value. It is significant to note that
while void* can be used to build generic pointers, void itself cannot be
declared as a variable type.
Here is a sample of code that shows how to utilize void in various situations:
#include <stdio.h>
// Function with void return type
void printHello() {
printf("Hello, world!\n");
}
// Function with void parameter
void processInput(void) {
printf("Processing input...\n");
}

int main() {
Pointers: // Calling a void function
printHello();

// Calling a function with void parameter


processInput();

// Using a void pointer


Output: int number = 10;
void* dataPtr = &number;
Hello, world! printf("Value of number: %d\n", *(int*)dataPtr);
Processing input... return 0;
Value of number: 10 }
THANK YOU
THE END FOR ANOTHER RESPONSIVE
SESSION

You might also like