0% found this document useful (0 votes)
11 views107 pages

CPP Module 2

Module II covers essential concepts of computer programming, focusing on the C programming language, character sets, data types, and operators. It discusses the evolution of programming languages, the structure of C programs, and key components such as keywords, identifiers, and variables. Additionally, it explains the process of creating and executing C programs, including variable declaration, initialization, and the use of literals and constants.

Uploaded by

hinalpatel.1103
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)
11 views107 pages

CPP Module 2

Module II covers essential concepts of computer programming, focusing on the C programming language, character sets, data types, and operators. It discusses the evolution of programming languages, the structure of C programs, and key components such as keywords, identifiers, and variables. Additionally, it explains the process of creating and executing C programs, including variable declaration, initialization, and the use of literals and constants.

Uploaded by

hinalpatel.1103
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

Module II:

Character Sets, Data


Types and Operators

By MRC
Road Map

• Computer Programming • Strings

• The C Programming Language • Operators

• Character Set • Comments

• Keywords • Type Casting

• Identifiers • Pre-processor

• Variables • Structure of a C Program

• Literals

• Constants

• Data Types

By MRC
Computer Programming

By
ByMRC
MT
Computer Programming

• Programming is a process of instructing computers how to solve a problem.

• The computer executes instructions to complete the task.

• A set of instructions is called a Program.

• Instructions are given in form of a Programming Language.

• These instructions must be very specific. If the instructions are incomplete or in


wrong order, the output will be completely unexpected.

By MRC
Computer Programming

• A program generally requires input data which is processed by the program


and an output is produced.

• Computers understand only one language called the Binary Language or


Machine Language and it only consists of 0’s and 1’s.

• In the very early days of programming, Binary was used but it was too hard to
remember and understand.

By MRC
Computer Programming

• To simplify this, a symbolic language was introduced called the Assembly


Language.

• Instructions written in Assembly were converted into binary code by a program


called Assembler.

• However, Assembly is also very hard to learn, read, understand and maintain.
Assembly is not portable, meaning it can only run on the computer it’s written
on.

• Both Assembly and Binary are low-level languages.

• This created a need for a higher-level, readable and understandable languages.

• So, along with many other languages, came the C Programming Language.

By MRC
The C Programming Language

By
ByMRC
MT
C Programming Language: Overview

• C is a middle-level language. Compared to assembly, its code is portable,


smaller, easier to read, understand and maintain.

• C also allows direct access to lower-level components such as registers and


operations such as memory manipulation.

• C is statically typed, meaning variable types must be explicitly specified before


being used.

• C is a compiled language, meaning its code is compiled (translated) into


Binary code before it is executed by the computer.

• C programs are stored with the extension .c

By MRC
C Programming Language: Application

• Today, C is widely used for System Programming.

• Various software or their parts are written in C

• Operating Systems (Windows, Linux, macOS, Android, iOS)

• Embedded Systems (Printers, Microwaves, Robots, Washing Machines)

• Compilers and Interpreters

By MRC
C Programming Language: History

• C Language was created by Dennis Ritchie at Bell Laboratories, USA in the


year 1972.

• C was evolved from other programming languages at the time such as ALGOL,
BCPL and B.

• C was developed along with the UNIX operating system. In fact, UNIX was almost
entirely developed in C.

• In the late 80’s, C language was standardized by the American National


Standards Institute (ANSI).

• In the 90’s, C would be approved by the International Standards


Organization (ISO).

By MRC
C Programming Language: History

• As time went on, the language was improved further and further introducing
many new and improved features.

• Many higher level programming languages were inspired by C such as C++, C#,
Java and many more.

• Currently the latest version of C language is C17, released in 2018.

By MRC
C Programming Language

Setting up the environment for developing C programs

• To write C Programs, a Compiler and an IDE (Integrated Development


Environment) are required.

• In the slides, we’ll be using MinGW compiler and Visual Studio Code IDE.

By MRC
C Programming Language

Creating your first C program

• Open a Folder in VS Code

• Right-click in Explorer and create a new file with the extension .c

By MRC
C Programming Language

Creating your first C program

• Code in hello.c

By MRC
C Programming Language

Creating your first C program: Code explained

• Some lines in the code have their own separate topics and will be explained in
detail in upcoming chapters.

• Here, we’re including stdio.h header file for using features required in our
program.

• stdio stands for Standard input/output.

• .h indicates it’s a header file.

• There are many header files in C which provide variety of features.

By MRC
C Programming Language

Creating your first C program: Code explained

• The main( ) function is the Entry Point of any C program.

• main( ) tells the computer where to start the program execution.

• int is what’s known as Data Type which will be explained in future chapters.

• The { (opening brace) marks the beginning and the } (closing brace) marks
the end of the main function.

• A pair of curly braces which contains code is known as a block of code.

By MRC
C Programming Language

Creating your first C program: Code explained

• printf is a function that displays characters on the screen.

• The return statement will be explained in a future topic.

• In this specific case, returning 0 means the program is telling the computer
that it has completed its execution successfully.

By MRC
C Programming Language

Executing your first C program

• In VS Code, open Terminal (View -> Terminal).

• Make sure Command Prompt is selected as the default profile


(this needs to be done only once after installing VS Code).

By MRC
C Programming Language

Executing your first C program

• To compile the program, type in the Terminal:

gcc filename.c –o exename

• This command will compile the C program into an executable file which contains
the Binary code. This executable file will have the extension .exe

• To execute the program, type in the Terminal simply:

exename

• For our example, the commands would be:

By MRC
C Programming Language

Creating your first C program: Code explained

• Some important points

• Most lines of code end with a semicolon. It’s the full stop of C language.
Forgetting the semicolon will result in compilation error.

• Some statements which require a block of code do not generally end with a
semicolon, for example, the main function.

• Function names are always followed by parenthesis, for example:


main( ), printf( ), getch( )

By MRC
Character Set

By
ByMRC
MT
Character Set

• A C program consists of Statements. A statement is a line or a sentence that


consist of different characters from the C character set.

• C provides the following sets of characters:

• Alphabets

• Uppercase letters (A to Z)

• Lowercase letters (a to z)

• Digits

• 0 to 9

• Special Symbols

• ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > , < \ | tab newline space


backspace vertical-tab NULL and many more…

By MRC
Character Set

• Each character in the character set has an ASCII value associated with it which
represent how the characters are stored internally in the computer.

• ASCII stands for American Standard Code for Information Interchange.

• ASCII values of some common characters:

Character ASCII Value


A to Z 65 to 90
a to z 97 to 122
0 to 9 48 to 57
Space 32
Enter/return 13
NULL 0

By MRC
Character Set

Escape Sequence Characters

• Escape sequence characters are prepended with a backward slash (\) sign.

• These characters are converted into another character when the output is
displayed.

• For example:

This line will display the following output when executed:

By MRC
Character Set

Escape Sequence Characters

• There are many escape sequence characters available in C:

Character Meaning
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\b Backspace
\0 NULL
\\ \
\’ ‘
\” “

By MRC
Keywords

By
ByMRC
MT
Keywords

• Keywords are reserved words that have some predefined meaning.

• Keywords are part of the language, they cannot be used for any other purposes.

• There are 32 keywords in C.

auto break case char

const continue default do

double else enum extern

float for goto if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while

By MRC
Identifiers

By
ByMRC
MT
Identifiers

• Identifiers are names given by the programmer to various programming


elements such as variables, constants, functions and many others.

• When creating an identifier, these rules must be followed:

• Can only contain alphabets, numbers and underscores (_).

• Can only start with an alphabet or an underscore (_).

• Identifiers are case-sensitive. The identifiers number, NUMBER, Number


are all considered different.

• Keywords cannot be used as identifiers.

By MRC
Identifiers

• For example,

Here, marks is an identifier.

• It’s recommended to give meaningful and self-explanatory names to identifiers.

By MRC
Variables

By
ByMRC
MT
Variables

• In computer programming, variables are used for storing data.

• A variable is a named storage location in the primary memory.

• A variable generally stores data that may change when the program is running,
hence the name “variable”.

• For example,

• Here, marks is a variable of the int (integer) type.

• A variable’s value can be changed by re-assigning another value.


For example,

By MRC
Variables

• Variable names are identifiers, so the rules for identifiers apply when naming a
variable.

• Creating variable is also referred to as Variable Declaration.

• When creating a variable, its Data Type must be specified. That is, what type of
data the variable will store.

• Data type of a variable cannot be changed once created. For example,

By MRC
Variables

Declaration

• Creating a variable without a value is known as variable declaration.

• Syntax:

• Example:

Assignment

• Putting a value in a variable that has already been declared is called Assignment.

• Syntax:

• Example:

By MRC
Variables

Initialization

• Creating a variable and putting a value in it at the same time is called


Initialization.

• Syntax:

• Example:

By MRC
Literals

By
ByMRC
MT
Literals

• Literals are fixed values. They can be used directly in code.

• For example, 1, 2.5, ‘A’, “Hello” are all literals.

By MRC
Constants

By
ByMRC
MT
Constants

• Constants are variables whose values cannot be changed.

• Constants can be created using two ways:

• The const keyword

• Trying to change value will result in a compiler error.

• Must be initialized when it’s declared.

• Example:

• The #define directive

• #define gives an alternative name to a literal

• When the program is compiled, the compiler replaces the name with the
value defined in the directive.

• Example:

By MRC
Data Types

By
ByMRC
MT
Data Types

• Data types specify what type of data a variable will store.

• Variables of different types will occupy different number of bytes on the primary
memory.

• C data types are divided into 3 categories:

• Primary / Built-in / Fundamental types

• Derived types

• User-defined types

By MRC
Data Types

By MRC
Data Types: Primary Types

• Primary types allow storing atomic or fundamental values in variables.

• Different primary types have different ranges of values that are allowed.

• C provides the following primary types

• Integer (int)

• Floating point (float)

• Double-precision floating point (double)

• Character (char)

• Void (void)

By MRC
Data Types: Primary Types

Data Type Modifiers

• Some of the primary types can be modified to increase and decrease their
ranges and sizes.

• Size-based Modifiers

• short: Decreases the range and size of a data type.

• long: Increases the range and size of a data type.

• long long: Increases the range and size, even larger than long.

• Sign-based Modifiers

• signed: Allows storing both positive and negative values.

• unsigned: Allows storing only 0 and positive values.

Note: size-based and sign-based modifiers can be combined together.

By MRC
Data Types: Primary Types

Integer (int)

• Integer type allows storing complete/non-fractional numbers.

• Keyword for Integer is int.

• Example:

Type Size in Size Range (16-bit) Range (32-bit)


bytes in
(16- bytes
bit) (32-
bit)
int 2 4 -32768 to 32767 -2,147,483,648 to
2,147,483,647
singed int 2 4 -32768 to 32767 -2,147,483,648 to
2,147,483,647
unsinged 2 4 0 to 65535 0 to
int 4,294,967,295
By MRC
Data Types: Primary Types

Integer (int)

Type Size Size Range (16-bit) Range (32-bit)


in in
bytes bytes
(16- (32-
bit) bit)
short int 2 2 -32768 to 32767 -32768 to 32767
singed 2 2 -32768 to 32767 -32768 to 32767
short int
unsigned 2 2 0 to 65535 0 to 65535
short int

By MRC
Data Types: Primary Types

Integer (int)

Type Size Size Range (16-bit) Range (32-bit)


in in
bytes bytes
(16- (32-
bit) bit)
long int 4 8 -2,147,483,648 to -2,147,483,648 to
2,147,483,647 2,147,483,647
signed 4 8 -2,147,483,648 to -2,147,483,648 to
long int 2,147,483,647 2,147,483,647
unsigned 4 8 0 to 4,294,967,295 0 to 4,294,967,295
long int

By MRC
Data Types: Primary Types

Floating point (float)

• Allows storing fractional numbers up to the precision of 6 digits.

• Keyword for Floating point is float.

• Example:

• The suffix “F” (or “f”) is necessary, if it’s not specified then it’ll be considered a
double floating-point number

Type Size in Size in Range Digits of


bytes bytes Precision
(16-bit) (32-bit)
float 4 4 3.4e-38 to 7
3.4e+38

By MRC
Data Types: Primary Types

Double-Precision Floating point (double)

• Allows storing fractional numbers up to the precision of 15 digits.

• Keyword for Double Floating point is double.

• Example:

Type Size in Size in Range Digits of


bytes bytes Precision
(16-bit) (32-bit)
doubl 8 8 1.7e-308 to 15
e 1.7e+308
long 10 16 3.4e-4932 to 18
doubl 1.1e+4932
e

By MRC
Data Types: Primary Types

Character (char)

• Allows storing a single character enclosed in single inverted commas.

• Keyword for Character is char.

• Example:

Type Size in Size in Range


bytes bytes
(16-bit) (32-bit)
char 1 1 -128 to 127
signed char 1 1 -128 to 127
unsigned char 1 1 0 to 255

By MRC
Data Types: Primary Types

Void

• Void represents ‘nothing’ or ‘absent’ value.

• Keyword for Void is void.

• void type variables cannot be created, however void has other uses which will
be covered in future topics.

By MRC
Data Types

Format Specifiers

• All primary types and some derived types have Format Specifiers associated
which them that allows inputting and outputting the value.

Type Format Specifier


int %d
float %f
double %lf
long int %ld
char %c
string %s

By MRC
Strings

By
ByMRC
MT
Strings

• A string is a sequence of characters enclosed in double quotes.

• String variables can be created using character array type, for example:

• A string is always, by default, terminated with a NULL (\0) character at the end.
The msg variable in the above example contains the following value:

• It’s possible to declare a string variable without a value, but the expected size of
the string must be specified:

By MRC
Strings

• The format specifier %s can be used with printf( ) and scanf( ) functions to
output and input strings respectively:

• When using scanf( ), the ampersand sign (&) before the variable name is not
required.

• Also, scanf( ) does not accept string values with space.

By MRC
C Stream
• The initial programmers had a very hard time handling the I/O operations.

• The programs created had to explicitly identify the input device where the input
would be taken from and the output device where the output would be sent to.

• The programmer thus, instead of concentrating on the solution of problem, also


had to look for hardware-specific problems that could arise from the I/O
operations.

• The Unix operating system provided a concept of abstract devices.

• This concept removed the need for a program to identify the kind of devices
used.

• A data stream is an ordered sequence of data bytes that can be read until the
end of the file.

• Three standard streams:: standard input, standard output, and standard error.

By MRC
Standard Input (stdin)

By MRC
Standard Output (stdout)

By MRC
Standard Error (stderr)

• Standard error, more commonly known as stderr, is another output stream.

• This data stream is used to output error messages or diagnostics by programs.

• The stream stderr is independent of the standard output and can be redirected
separately.

• A classic usage of this stream is done when the user tries to open a file on a
secondary storage.

By MRC
Input and Output Functions

By MRC
General Syntax of
Formatted Functions

By MRC
Input Function
● Used as
scanf(“format string”, address list of identifiers);

By MRC
Program to illustrate the use of
scanf
#include<stdio.h> int main()
{
int first;
float second;
double third;
char choice;
/* Read and print the values */
printf("\nEnter the values :");
scanf("%d%f%lf%c",&first, &second, &third, &choice);
printf("\nValues of the variables are %d %f %lf %c\n ",first,second,third,choice);
return 0;
}

By MRC
Output Function

● Used as
printf( “format string”, list of identifiers);
printf(“message string”);

● Program to illustrate the use of printf

#include <stdio.h>
int main( )
{
int i_number;
printf("\n Enter the value of number : ");
scanf("%d",&i_number);
printf(" \n Value in base 10 is : %d :: in base 16 is %x\n", i_number, i_number);
printf(" \n Value in base 8 is : %o :: in character is : %c \n", i_number, i_number);
return 0;
}

By MRC
Unformatted Functions

gets(identifier name);

puts(identifier name);

By MRC
Program to illustrate unformatted
functions
#include<stdio.h>
int main()
{
char data, temp, name[25];
printf("\nEnter data :");
data=getchar();
temp=getc(stdin);
printf("\nOutput of data using putchar:");
putchar(data);
printf("\nOutput of data using putc:");
putc(data,stdout);

printf("\nEnter String :");


gets(name);
printf("\nOutput of string: ");
puts(name);
return 0;
}

By MRC
Operators

By
ByMRC
MT
Operators

• Operators are special symbols used for performing specific operations.

• These operations are performed on one or more values. These values are known
as operands.

• Based on number of operands, each operator can be classified as one of the


following:

• Unary operators

• Binary operators

• Ternary operators

By MRC
Operators

• Unary operators:

• It has only one operand.

• Examples: -5, a++

• Binary operators:

• It has two operands.

• Example: a + b

• Ternary operators:

• It has three operands.

• There is only one ternary operator in C, that is, ? :.

• Example:

By MRC
Operators

• There are 8 categories of operators in C:

• Arithmetic operators

• Relational/comparison operators

• Logical operators

• Conditional operator

• Bitwise operators

• Assignment operators

• Increment and decrement operators

• Special operators

By MRC
Operators

Arithmetic Operators

• Performs arithmetic operations on operands and produces a number as result.

• Assume we have two variables: A = 20 and B = 10

Operator Description Example


+ Adds two operands A + B = 30
- Subtracts second from the first A - B = 10
operand
* Multiplies the operands A * B = 200
/ Division A/B=2
% Gets the remainder of a division A%B=0

• Note: % (modulus) operator cannot be used on fractional numbers.

By MRC
Operators

Relational/comparison Operators

• Performs comparison between two operands and produces a Boolean value


(true/false) as result.

• Assume we have two variables: A = 20 and B = 10

Operator Description Example


> Checks first operand is greater than (A > B) is
second. true.
>= Checks first operand is greater than (A >= B) is
or equal to second. true.
< Checks first operand is less than (A < B) is
second. false.

By MRC
Operators

Relational/comparison Operators

Operator Description Example


<= Checks first operand is less than or (A <= B) is
equal to second. false.
== Checks whether both operands have (A == B) is
the same values. false.
!= Checks whether both operands do (A != B) is
not have the same values. true.

By MRC
Operators

Logical Operators

• There are three Logical operators:

• Logical AND (&&)

• Logical OR (||)

• Logical NOT (!)

• The Logical AND (&&) and Logical OR (||) operators are used for combining two
conditions and getting a single result.

• The Logical NOT operator simply inverts the result of a condition.

By MRC
Operators

Logical Operators

• Logical AND (&&):

• Returns true when both conditions evaluates to true.

• Example:

• Assume a variable:

• So, the condition returns true.

By MRC
Operators

Logical Operators

• Logical OR (||):

• Returns true when one of the two conditions evaluates to true.

• Example:

• Assume a variable:

• So, the condition would returns true.

By MRC
Operators

Logical Operators

• Logical NOT (!):

• Inverts the result of a condition.

• Example:

• Assume a variable:

• So, the condition returns true.

By MRC
Operators

Conditional Operator

• The operator ? : is known as the conditional operator.

• It is also known as Ternary Operator because it operates on three operands.

• Example:

• Note there are three operands in the above example:

• The condition (before ?)

• The true part (between ? and :)

• The false part (after :)

By MRC
Operators

Bitwise Operators

• Bitwise operators perform logical operation on the bits of specified operands.

• For example, two variables A = 60, B = 13.


Their binary: A = 0011 1100, B = 0000 1101

Expression Result
A&B 0000 1100

A|B 0011 1101

A^B 0011 0001

~A 1100 0011

A >> 2 0000 1111


A << 2 1111 0000

By MRC
Operators

Assignment Operators

• Used for assigning values into variables.

Operator Description Example


= Assigns the value on the right side to A = 10
the variable on the left side.
+= Adds the right operand to the left one A += 10
and assigns it to the left operand. means
A = A + 10
-= Subtracts the right operand from the A -= 10
left one and assigns it to the left means
operand. A = A – 10
*= Multiplies the right operand with the A *= 10
left one and assigns it to the left means
operand. A = A * 10
/= Divides the left operand with the right A /= 10
one and assigns it to the left operand. means
A = A / 10 By MRC
Operators

Assignment Operators

Operator Description Example


%= Gets the remainder of the division of A %= 10
left and right operand and assigns It to means
the left operand. A = A % 10
&= Performs the Bitwise AND operation on A &= 2
both operands and assigns the result to means
the left operand. A=A&2
|= Performs the Bitwise OR operation on A |= 2 means
both operands and assigns the result to A=A|2
the left operand.

By MRC
Operators

Assignment Operators

Operator Description Example


^= Performs the Bitwise XOR operation on A ^= 2
both operands and assigns the result to means
the left operand. A=A^2
<<= Performs the Bitwise Left Shift A <<= 2
operation on both operands and means
assigns the result to the left operand. A = A << 2
>>= Performs the Bitwise Right Shift A >>= 2
operation on both operands and means
assigns the result to the left operand. A = A >> 2

• The operators from += to >>= are also known as Shorthand Operators.

By MRC
Operators

Increment and Decrement Operators

• They’re unary operators.

• The Increment operator (++) increases its operand’s value by 1.

• The Decrement operator (--) decreases its operand’s value by 1.

• There are two ways these operators can be used:

• Prefix

• Postfix

By MRC
Operators

Increment and Decrement Operators

• Prefix Increment/Decrement:

• When using prefix increment or decrement, the value of the operand is first
incremented or decremented then its value is returned.

• Example:

• In above example,

• a will be first incremented by 1 then assigned to variable b.

• a will be first decremented by 1 then assigned to variable c.

• The final values will be: a = 10, b = 11 and c = 10

By MRC
Operators

Increment and Decrement Operators

• Postfix Increment/Decrement:

• When using postfix increment or decrement, the value of the operand is first
returned and then incremented or decremented.

• Example:

• In above example,

• a will be first assigned to variable b then incremented by 1.

• a will be first assigned to variable c then decremented by 1.

• The final values will be: a = 10, b = 10 and c = 11

By MRC
Operators

Special Operators

• These operators do not belong in any other category.

Operator Description Example


sizeof( ) Returns the size of a data type or a sizeof(int)
variable.
& Returns the address of a variable &num
* Used for creating a pointer. int *ptr;
. Used for accessing structure and union -
members.
-> Used for accessing structure and union -
members through a pointer to structure

By MRC
Operators

Operator Precedence

• Each operator in C has a precedence or priority.

• For arithmetic operators and brackets, it’s the same as mathematics:

• An Expression in C is always executed from left to right, while respecting the


BODMAS rule.

• An Expression is a statement containing variables, constants and operators


arranged as per language rules.

By MRC
Operators

Operator Precedence

• Each operator in C has a precedence or priority.

• For arithmetic operators and brackets, it’s the same as mathematics:

• An Expression in C is always executed from left to right, while respecting the


BODMAS rule.

• An Expression is a statement containing variables, constants and operators


arranged as per language rules.

By MRC
Comments

By
ByMRC
MT
Comments

• Comments are text or description written by the developer.

• Comments are ignored by the compiler and have no effect on the workings of the
program.

• Comments generally serve as a description or a reminder of the code written by


the developer for future reference.

• There are two types of comments:

• Single-line

• Multi-line

By MRC
Comments

Single-line Comments

• Single-line comments are prepended with double slash signs.

By MRC
Comments

Multi-line Comments

• Multi-line comments can span over multiple lines and are enclosed in /* … */
signs.

By MRC
Type Casting

By
ByMRC
MT
Type Casting

• Type Casting or Type Conversion is the process of converting a variable or a


value from one data type to another.

• Each data type is either smaller or bigger compared to other types, as mentioned
in the Data Types chapter.

char ➤ int ➤ float ➤ double

• Converting a bigger to a smaller type may result in data loss.

• Type Casting can be performed in two ways:

• Implicit

• Explicit

By MRC
Type Casting

Implicit Type Casting

• A variable or a value of one data type is converted to another automatically.

• As shown in the above example, num1 (int) and its value will be converted to
float type automatically and assigned to num2 (float).

By MRC
Type Casting

Implicit Type Casting

• In the above example, num1 (float) will be converted to integer type


automatically and assigned to num2 (int).

• Since, float is bigger than integer, converting a bigger type to a smaller one
causes data loss here.

• After conversion, the value of num2 will be 10. The real number 10.5 will lose its
fractional part during conversion.

By MRC
Type Casting

Explicit Type Casting

• A variable or a value of one data type is converted to another manually.

• To perform explicit type casting, the target type is written in brackets before
variable or the value during assignment.

• As shown in the above example, num1 (int) and its value will be converted to
float type and assigned to num2 (float) manually.

By MRC
Type Casting

Explicit Type Casting

• In the above example, num1 (float) will be converted to integer type and
assigned into num2 (int) manually.

• Since float is bigger than integer, converting a bigger type to a smaller one
causes data loss here.

• After conversion, the value of num2 will be 10. The real number 10.5 will lose its
fractional part during conversion.

By MRC
Pre-processor

By
ByMRC
MT
Pre-processor

• As stated before, C programs are compiled by a compiler into binary code.

• Before the compiler compiles a program, the source code of the program is
processed by a tool called the Pre-processor.

• The Pre-processor looks for any Pre-processor Directives/Commands and


processes these given directives in the program code.

• Pre-processor Directives are generally prepended with a # sign.

• Some of the Pre-processor Directives of C:

• #include

• #define

By MRC
Pre-processor

#include

• Used for including a Header (.h) file or another C program (.c).

• When the Pre-processor encounters the #include directive, it includes the


content of the included file at the place of the directive in the program.

• #include can be used in two ways:

• #include <filename>

• Searches for the included file in the standard include directories.

• #include “filename”

• Searches for the included file first in the same directory as the program
then standard include directories.

By MRC
Pre-processor

#define

• Can be used for creating symbolic constants and macros.

• Syntax: #define <identifier> <value/expression>

• When the Pre-processor encounters a #define directive, it searches for the


specified identifier in the whole program and replaces it with the defined value.

• Examples:

By MRC
Structure of a C Program

By
ByMRC
MT
Structure of a C Program

• Each C Program follows a specific structure, each component of the structure can
be divided into sections.

• These are the possible sections in a C program:

• Documentation Section

• Contains comments that shows the author, date, description and any
other information about the program.

• Link Section

• Contains #include directive to include required header files.

• Definition Section

• Contains definitions of symbolic constants.

By MRC
Structure of a C Program

• These are the possible sections in a C program:

• Global Declaration Section

• Contains declaration of global variables and functions.

• Main Function Section

• Contains the main function, the entry point of any program.

• Inside the main function, there are two parts:

• Declaration part: contains declaration of local variables.

• Execution part: contains the actual code.

• Subprogram Section

• Contains function definitions declared the Global Declaration section.

By MRC
Structure of a C Program

By MRC
By MRC

You might also like