Module 1.1 Character Set, Constants, Identifiers, Keywords, Basic Data Types, Variables
Module 1.1 Character Set, Constants, Identifiers, Keywords, Basic Data Types, Variables
Page 2
Module 1
► Control Statements - if, if-else, nested if, switch, while, do-while, for, break & continue,
nested loops.
Page 3
Module 1
Page 4
C Programming
Page 5
C Programming
Page 6
C Fundamentals
Page 7
Getting Started with C
Page 8
Character Set
► The character set in C programming refers to the set of valid characters that can
be used in the source code.
Uppercase Alphabets A to Z A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
Digits 0 to 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Characters – `~@!$#^*%&()[]{}<>+=_–|/\;:‘“,.?
Page 9
Character Representation in C
► In C, identifiers and keywords are fundamental concepts for defining and using
various elements of the language.
Page 11 VISAKH
Identifiers
Page 12 VISAKH
Identifiers
Page 13 VISAKH
Keywords
► Keywords are predefined, reserved words in C that have specific meanings and
purposes. They cannot be used as identifiers.
► Characteristics of Keywords
► Reserved: Cannot be used for any other purpose.
► Lowercase Only: All C keywords are in lowercase.
► Fixed Meaning: The meaning of a keyword cannot be changed by the user.
Examples of Keywords
Category Keywords
Data Types int, float, char, double, void, long, short
Control Flow if, else, switch, case, default, for, while, do, break, continue, return
Modifiers signed, unsigned, const, volatile, static, auto, extern, register
Storage Classes static, extern, auto, register
Others sizeof, typedef, goto, enum, struct, union, typedef, inline
Page 14 VISAKH
Differences Between Identifiers and Keywords
Page 15 VISAKH
Constants in C
10 // Decimal
012 // Octal (Decimal 10)
0xA // Hexadecimal (Decimal 10)
3.14
2.71828
Page 16 VISAKH
Constants in C
Types of Constants in C
► Character Constants: A single character enclosed in single quotes
'A'
► String Constants: A sequence of characters enclosed in double quotes.
"Hello, World!"
► Symbolic Constants: Defined using the #define preprocessor directive or the const keyword.
#define PI 3.14159
const int MAX = 100;
Page 17 VISAKH
Variables in C
IT Park - 1
► An entity that may change its value during a
program execution.
► Why variables?
#453
► In computers,the data is stored in one or many
memory locations in the primary memory.
► Each memory location has a unique address. To
refer this data a name is assigned in a program. No 453 and IT Park – 1 refers to the same building
► This name assigned to a particular memory
location is called variable.
Page 18 VISAKH
Variable Naming Conventions
► studentName
► rollNumber
► employeeName
► simpleInterest
► dateOfBirth
Page 19 VISAKH
Data Types in C
► Data type determines the type of value that can be stored in variable
► C language has some predefined set of data types to handle various kinds o
data that we can use in our program.
► C language supports 2 different type of data types:
► Primary data types:
► These are fundamental data types in C namely integer(int), floating point(float),
character(char) and void.
► Derived data types:
► Derived data types are nothing but primary datatypes but a little twisted or grouped
together like array, structure, union and pointers. These are discussed in details late
Page 20 VISAKH
Integer Types (int)
► Integers are those numbers that do not contain a decimal point and
decimal digit.
► Examples : 17, 7890
► There are 3 integer data types supported by C language
► Short Integer(short)
► Integer(int)
► Long Integer(long)
► Each integer can be signed or unsigned
► Unsigned integer can store only positive values
Page 21 VISAKH
Integer Types (int)
Page 22 VISAKH
Integer Types (int)
► Examples
► short number1; unsigned long employeeId; int number2; unsigned int countPurchased;
Memory (RAM)
int a;
a =12 204
00001100
1210 = 1100
203
2
202 00000000
00000000 00000000 00000000 00001100201
00000000
200 00000000
byte 4 byte 3 byte 2 byte 1
Page 23 VISAKH
Floating-Point Types (float and double)
Page 24 VISAKH
Floating-Point Types (float and double)
► Number that can have a fractional part
► Example
► 34.5, 7856.781
► In memory, a floating point number is stored as mantissa and exponent.
Sign – 1 bit
Exponent – 8 bit
Mantissa – 23 bit
Page 25 VISAKH
Character Types (char)
Page 26 VISAKH
How character is stored in Computer
Page 27 VISAKH
Escape Sequences
Page 28 VISAKH
Module 1
► Control Statements - if, if-else, nested if, switch, while, do-while, for, break & continue,
nested loops.
Page 3
The First C Program
Page 4
Do you Know?
Brief summary of what the code and file does, who created it, when etc….
► A header file is a file with extension .h which contains C function declarations and
macro definitions to be shared between several source files
► You request to use a header file in your program by including it with the C
preprocessing directive #include
► stdio.h - file contains standard library functions for input and output operations
Pre-Processor
Loader
Intermediate Code
Compiler
Run time
Libraries
Developer
Object Files
(.obj)
Linker Executable File
Page 12
Basic Input and Output in C
Syntax:scanf("format_specifier", &variable);
► format_specifier: Specifies the type of input (e.g., %d for integers, %f for floats).
► &variable: The address-of operator (&) is used to store the input at the variable's memory location.
► Output is the process of displaying information to the user.
The most commonly used function
for output is printf().
► Using printf()
► The printf() function writes output to the standard output (console).It also uses format specifiers
to specify the type of data to print.
#include <stdio.h>
int main() { Format Descriptio Example Example
int age; Specifier n Input Output
float height; Integer
%d 42 42
(decimal)
printf("Enter your age: ");
scanf("%d", &age); Float/Dou
%f 3.14 3.140000
ble
printf("Enter your height: "); Float (2
%.2f 3.14 3.14
scanf("%f", &height); decimals)
%c Character A A
printf("You are %d years old and %.2f
meters tall.\n", age, height); %s String Hello Hello
Long
%ld 123456789 123456789
return 0; integer
} %lf Double 3.14159 3.141590
#include <stdio.h>
► Reading and Writing Characters: For int main() {
character-level input and output, char ch;
getchar(), putchar(), gets(), and
puts() can be used. printf("Enter a character: ");
ch = getchar(); // Read a single charact
► Using getchar() and putchar()
► getchar(): Reads a single character from printf("You entered: ");
the user. putchar(ch); // Print the character
► putchar():Prints a single character to printf("\n");
the console.
return 0;
}
return 0;
}
► Control Statements - if, if-else, nested if, switch, while, do-while, for, break & continue,
nested loops.
Page 3
Expressions
► They are symbols that instruct the compiler to carry out specific computations or
manipulations.
► C programming language supports a rich set of operators that are classified as follows.
► Arithmetic Operators
► Relational Operators
► Logical Operators
► Increment & Decrement Operators
► Assignment Operators
► Bitwise Operators
► Conditional Operator
► Special Operators
► The arithmetic operators are the symbols that are used to perform basic
mathematical operations like addition, subtraction, multiplication,
division and percentage modulo.
► The following table provides information about arithmetic operators.
Operator Meaning Example
+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division 10 / 5 = 2
% Remainder of the Division 5 % 2 = 1
Output
Program
Page 7 VISAKH MESCET
Relational Operators (<, >, <=, >=, ==, !=)
► The relational operators are the symbols that are used to compare two values.
► That means the relational operators are used to check the relationship
between two values.
► Every relational operator has two results TRUE or FALSE.
Output
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
^ Bitwise XOR
~ Bitwise complement
► Program to Find the minimum of two numbers using the conditional operator.
► Write a C Program to find largest among three numbers using the conditional
operator.
#include <stdio.h>
int main() {
int a, b, result;
return 0;
}
x = 34 + 12 / 4 - 45
► Operator precedence determines the grouping of terms in an
expression and decides how an expression is evaluated.
► Certain operators have higher precedence than others
► Control Statements - if, if-else, nested if, switch, while, do-while, for, break & continue,
nested loops.
Page 3
Control Flow Statements
Page 4
Control Structures
► The way in which the programmer specifies the order of execution of the
statements.
► Different set of statements can be executed based on a condition.
► Sequential
► Selection
► Iteration (Repetition)
Page 5
Selection Control Structures
Page 6
Simple If Statement
if (Condition) { True
Condition
statement(s);
}
Next Statement; False Set of Statement(s)
Next Statement
Page 7
Simple If Statement - Example
if (duration>=3) {
rateOfInterest = 6.0;
}
Page 8
Simple If Statement
► Points to Remember:
► The if statement does not end with a semicolon
► Only statements follows the if, should end with a semicolon
Page 9
If…… Else Statement
if (Condition) {
True statement(s);
} False Statement(s) True Statement(s)
else{
False statement(s);
Next Statement
}
Next Statement;
Page 10
If…… Else Statement- Example
if (duration>=3) {
rateOfInterest = 6.0;
}
False Duration> True
else{ =3
rateOfInterest = 5.5;
} rateOfInterest = 6.0
rateOfInterest = 5.5
Next Statement
Page 11
Else… If Statement
► The else..if statement is to check for a if (test - expression 1) {
sequence of conditions. statement1;
} else if (test - expression 2){
► When one condition is false, it checks for the Statement2;
next condition and so on. } else if (test - expression 3){
► When all conditions are false the ‘else’
block Statement3;
} else if (test - expression n){
is executed. Statement n;
} else {
default;
False False False
condition -1 condition -2 condition -n
}
Statement x;
True True
True
Page 12
Else… If Statement
Page 13
Else… If Statement
Page 14
Nested if
► If an ‘if’statement embedded within another statement
‘if’ is called nested if
► Syntax
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Page 15
switch case statement
► Switch statement is a controlstatement that switch(expression)
allows us to choose only one choice among {
case value-1:
the many given choices.
block-1;
► The expression in switch evaluates to return break;
an integral value,which is then compared to case value-2:
the values present in different cases. block-2;
► It executes that block of code which matches break;
case value-n:
the case value.
block-n;
► If there is no match, then default block is break;
executed(if present). The general form of default:
switch statement is, default-block;
break;
}
Page 16
Rules for using switch statement
► The expression (after switch keyword)must yield an integer value i.e the
expression should be an integer or a variable or an expression that
evaluates to an integer.
► The case label values must be unique.
► The case label must end with a colon(:)
► The next line, after the case statement, can be any valid C statement.
Page 17
switch statement Example
Page 18
switch case statement
► Points to Remember:
► We don't use those expressions to evaluate switch case,which may
return floating point values or strings or characters.
► break statements are used to exit the switch block.
► It isn't necessary to use break after each block, but if you do not use it,
then all the consecutive blocks of code will get executed after the
matching block.
► default case is executed when none of the mentioned case matches
the switch expression. The default case can be placed anywhere in the
switch case. Even if we don't include the default case, switch statement
works.
Page 19
switch case statement
► Points to Remember:
► if statements can evaluate float conditions.
switch statements cannot
evaluate float conditions.
► if statement can evaluate relational operators. switch statement cannot
evaluate relational operators i.e they are not allowed in switch
statement.
Page 20
switch statement
Page 21