0% found this document useful (0 votes)
19 views

Variable& Data Type Operator in C

Variable& data type operator in c

Uploaded by

royalcommunity43
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Variable& Data Type Operator in C

Variable& data type operator in c

Uploaded by

royalcommunity43
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Variable & Datatypes in C programming

Variable:
In C programming, a variable is like a named container that holds data during your
program'

What they store:

 Variables can store different types of data, like


numbers (integers, floats), characters (letters,
symbols), or even text strings.

How they work:

 Declaring a variable: You tell the compiler, "Hey,


I'm going to use a space in memory to store
something." You choose a name (following naming
rules) and a data type (e.g., int age; for an integer).
 Assigning a value: Imagine putting something in
the box. You use the variable name followed by an
equal sign and the value you want to store
(e.g., age = 25;).

Syntax of Variable :

syntax for declaring a variable in C programming


involves two parts:
1.Data type: This keyword specifies the kind
of data the variable can hold. Here are some
common data types:
o int: Stores whole numbers (integers) like

10, -5, etc.


o float: Stores numbers with decimals like

3.14, -12.98, etc.


o char:
Stores single characters enclosed in
single quotes, like 'a', 'Z', '&'.
o There are other data types for more

complex data structures.


2.Variable name: This is a user-defined name
that identifies the variable. Here are the rules
for naming variables:
o Must start with a letter (a-z, A-Z) or an

underscore (_).
o Can contain letters, numbers, and

underscores.
o Cannot be a reserved keyword in C

(like int, float, if, else, etc.).


Putting it together:
The basic syntax for declaring a variable is:

data_type variable_name;
int num ;

What is the Data type in C programming ?


C programming, a data type acts like a blueprint that
defines the kind of information a variable can hold. It
essentially determines
Primitive Data Types: These are the fundamental
building blocks, the basic data types predefined in the
C language.

int (integer):

 What it stores: Whole numbers (positive,


negative, or zero).
 Example: int age = 25; (stores the age as 25)
 Size: The size of an int can vary depending on the
system architecture (typically 2 or 4 bytes).
 Use cases: This is the most widely used data type
for storing whole numbers like counts, identifiers,
or loop counters.
 Format specifier: %d (Example: printf("Your age
is %d\n", age);)

2. float (floating-point):

 What it stores: Numbers with decimal points.


 Example: float pi = 3.14159; (stores an
approximation of pi)
 Precision: float offers less precision than double.
 Memory usage: float takes up less memory
compared to double.
 Use cases: Suitable for calculations where
extreme precision isn't critical, like scientific
calculations that don't require high accuracy for
every decimal place.
 Format specifier: %f (Example: printf("The value
of pi is %f\n", pi);) Note: Due to limitations
in float precision, the output might show a slightly
different value for pi.

3. double (floating-point):

 What it stores: Numbers with decimal points.


 Example: double distance = 123.456789; (stores
a distance with more precision)
 Precision: double provides higher precision
than float, meaning it can represent a wider range
of decimal values with more accurate decimal
places.
 Memory usage: double uses twice the memory
compared to float.
 Use cases: Ideal for situations where high
precision is essential, such as financial
calculations, scientific computations requiring a
high degree of accuracy, or 3D graphics
calculations.
 Format specifier: %lf (Example: printf("The
distance is %lf meters\n", distance);)

4. char (character):

 What it stores: A single character enclosed in


single quotes.
 Example: char initial = 'A'; (stores the character
'A')
 Size: Typically, a char occupies 1 byte of memory,
which can hold any character from the character set,
including letters, symbols, and punctuation marks.
 Use cases: Used for storing individual characters,
text manipulation (by storing characters in arrays), or
representing user input from the keyboard.
pen_spark

What is the Format Specifiers in C


programming ?
Format specifiers in C programming are special
characters that tell the printf or scanf functions how
to interpret and display or read values,
respectively. They act like instructions within
formatted strings, guiding these functions on how
to present or obtain data.:
 %d or %i: A decimal integer
 %c: A character
 %f: A floating-point number

Operator in C programming :

Operators in C programming are symbols that perform


specific operations on variables, values, or expressions.

. Arithmetic Operators:

These operators perform mathematical calculations on


numeric data (integers or floating-point numbers).
Common examples include:

 + (addition)
 - (subtraction)
 * (multiplication)
 / (division)
 % (modulo - remainder after division)
Assignment Operators:

These operators are used to assign values to variables.


The most common one is =, but there are also
compound assignment operators that combine
assignment with other operations:

 = (assignment)
 += (add and assign)
 -= (subtract and assign)
 *= (multiply and assign)
 /= (divide and assign)
 %= (modulo and assign)

. Comparison Operators:
These operators compare values and return
either 1 (true) or 0 (false) based on the
comparison result. Common examples include:
 == (equal to)
 != (not equal to)
 < (less than)
 > (greater than)
 <= (less than or equal to)
 >= (greater than or equal to)

Logical Operators:
These operators combine conditional expressions and are used
for making decisions in your program. Common examples
include:

 && (logical AND)


 || (logical OR)
 ! (logical NOT)

Just we Give Simple Example :

the logical NOT operator (!) in C is used to


reverse the logical state of its operand (usually a
boolean expression). Here's a simple example to
illustrate its functionality:
Scenario: You want to check if someone is not
eligible to vote, which means their age is less
than 18.

condition ? expression_if_true : expression_if_false

Breakdown:
 condition:This is a boolean expression that
evaluates to either true or false.
 expression_if_true: This is the expression that
gets evaluated and returned if
the condition is true.
 expression_if_false: This is the expression that
gets evaluated and returned if
the condition is false.
Lets go through one Example :
#include <stdio.h>

int main() {
int age = 25;
int isAdult = (age >= 18) ? 1 : 0; // Ternary
operator usage

if (isAdult) {
printf("You are an adult.\n");
} else {
printf("You are not an adult.\n");
}

return 0;
}

You might also like