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

What Are Primitive Data Types

Uploaded by

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

What Are Primitive Data Types

Uploaded by

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

What are Primitive Data Types?

 Content:
o Basic building blocks in C programming.

o Predefined data types provided by the C

language.
o Used to store simple values like numbers,

characters, etc.
o Common primitive data types in C:

 int

 float

 double

 char

Integer Data Type (int)


 Content:
o Stores whole numbers (positive, negative, or

zero).
o Size: Typically 4 bytes (depends on system

architecture).
o Range: -2,147,483,648 to 2,147,483,647 (for

32-bit systems).
o Example Code:
c
#include <stdio.h>
int main() {
int age = 25;
printf("Age: %d\n", age);
return 0;
}
o Output: Age: 25

Float Data Type (float)


 Content:
o Stores single-precision floating-point

numbers (decimals).
o Size: Typically 4 bytes.

o Precision: Approximately 6-7 decimal

digits.
o Example Code:

c
#include <stdio.h>
int main() {
float temperature = 36.6;
printf("Temperature: %.1f\n",
temperature);
return 0;
}
o Output: Temperature: 36.6

Double Data Type (double)


 Content:
o Stores double-precision floating-point

numbers.
o Size: Typically 8 bytes.

o Precision: Approximately 15-16 decimal

digits.
o Example Code:

c
#include <stdio.h>
int main() {
double pi = 3.14159265359;
printf("Pi: %.10f\n", pi);
return 0;
}
o Output: Pi: 3.1415926536
Character Data Type (char)
 Content:
o Stores a single character (e.g., 'A', 'b', '9').

o Size: 1 byte.

o Can also store ASCII values (e.g., 65 for

'A').
o Example Code:

c
#include <stdio.h>
int main() {
char grade = 'A';
printf("Grade: %c\n", grade);
return 0;
}
o Output: Grade: A

 Title: Constants, Variables, and Reserved Words


in C
Overview
 Content:
o Variables: Named storage locations that

hold changeable data.


o Constants: Fixed values that cannot be

altered during program execution.


o Reserved Words: Predefined keywords in

C with special meanings; cannot be used as


identifiers.

What are Variables?


 Content:
o Definition: A variable is a named memory

location used to store data that can be


modified during program runtime.
o Deep Explanation:

 Must be declared before use: Specifies

data type (e.g., int, char) and allocates


memory.
Variable Declaration and Initialization
(Example)
 Content:
o Deep Explanation: Declaration reserves
memory (e.g., int x;). Initialization assigns a
value (e.g., int x = 10;). Uninitialized vars
hold garbage; always initialize for safety.
o Example Code:
c
#include <stdio.h>
int main() {
int age; // Declaration (garbage
value possible)
age = 25; // Assignment
float height = 5.9; // Declaration +
Initialization
printf("Age: %d, Height: %.1f\n", age,
height);
return 0;
}
o Output: Age: 25, Height: 5.9

Slide 5: Variable Scope and Lifetime (Deep Dive)


 Content:
o Local Variables: Declared inside a
function/block; accessible only there;
destroyed on exit.
o Global Variables: Declared outside
functions; accessible everywhere; lifetime =
program duration.
o Static Variables: Use static keyword;
retains value across function calls.
o Example Code (Static vs. Local):
c
#include <stdio.h>
void func() {
static int count = 0; // Retains value
count++;
printf("Count: %d\n", count);
}
int main() {
func(); // Output: 1
func(); // Output: 2
return 0;
}
 Notes: Use a table comparing scope types:
Type Scope Lifetime
Local Block Block duration
Global Program Program duration
Static Function Program duration

What are Constants?


 Content:
o Definition: Fixed values that remain

unchanged throughout the program.


o Deep Explanation:

 Types: Literal (direct values like 10,

'A'), Symbolic (named constants via


const or #define).
 Purpose: Improve readability,

maintainability (change once, affects


everywhere), prevent accidental
modifications.
 const Keyword: Compiler-enforced;

attempts to modify cause errors.


 #define: Preprocessor macro; text

replacement before compilation; no


memory allocation.
 Differences: const uses memory and
type-checking; #define is faster but
lacks type safety.
 Best Practices: Use uppercase names
(e.g., PI), prefer const for type safety.

Constant Examples (Literal and Symbolic)


 Content:
o Literal Constants: Integers (10), Floats

(3.14), Characters ('A'), Strings ("Hello").


o Symbolic Constants:

 Using const: const int MAX = 100;

 Using #define: #define PI 3.14159

o Example Code:

c
#include <stdio.h>
#define PI 3.14159 // Macro constant
int main() {
const int MAX_AGE = 100; // const
constant
printf("Pi: %.5f\n", PI);
printf("Max Age: %d\n", MAX_AGE);
// MAX_AGE = 101; // Error: Cannot
modify const
return 0;
}
o Output: Pi: 3.14159
o Max Age: 100

Differences Between Variables and Constants


 Content:
o Deep Comparison (Use a table):

Aspect Variables Constants


Mutability Can be changed Cannot be
changed
Declaration Data type + name const/ #define +
name
Memory Allocates storage const allocates;
#define doesn't
Use Case Dynamic data Fixed values (e.g.,
(e.g., user input) math constants)
Scope Local/Global Global (typically)

What are Reserved Words?


 Content:
o Definition: Also called keywords;

predefined words in C with specific


meanings (e.g., control program flow,
declare types).
o Deep Explanation:

 Cannot be used as variable/constant

names (e.g., can't name a var "int").


 Categories: Data types (int, float),

Control (if, else, switch), Loops (for,


while), Others (return, void, struct).
 Total in C99: 32 keywords; C11 adds

more (e.g., _Alignas).


 Case-sensitive: All lowercase.

 Purpose: Standardize syntax; compiler

recognizes them for instructions.


 Best Practices: Avoid similar names

(e.g., don't use "Int" to prevent


confusion).
 Notes: List all 32 keywords in a grid for visual
reference.

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

Reserved Words Examples


 Content:
o Examples:

 Data Type: int, char

 Control: if, else

 Loop: for, while

o Example Code (Using Keywords):

c
#include <stdio.h>
int main() {
int num = 10; // 'int' is reserved for type
declaration
if (num > 0) { // 'if' is reserved for
conditional
printf("Positive\n");
} else { // 'else' is reserved
printf("Non-positive\n");
}
// int if = 5; // Error: 'if' is reserved,
cannot use as variable
return 0;
}
o Output: Positive

You might also like