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

2 C Data Types, Constants, Variables, Expressions

This document discusses basic data types, variables, constants, and expressions in C programming. It covers: - The basic data types in C like char, int, float, and double. - How variables are declared and named in C. - How constants can be defined for different data types. - What expressions are and how they combine variables, constants, and operators to perform calculations in C.

Uploaded by

Asrar Tamim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

2 C Data Types, Constants, Variables, Expressions

This document discusses basic data types, variables, constants, and expressions in C programming. It covers: - The basic data types in C like char, int, float, and double. - How variables are declared and named in C. - How constants can be defined for different data types. - What expressions are and how they combine variables, constants, and operators to perform calculations in C.

Uploaded by

Asrar Tamim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Data types,

Constants,
Variables and Expressions
Introduction
• A programming language is designed to help
process certain kinds of data consisting of
numbers, characters, and strings to provide
useful output known as INFORMATION.
• The task of processing of data is accomplished
by executing a sequence of precise
instructions called a PROGRAM.
C Character Set
Letters digits White spaces
Uppercase A… Z, Lowercase all decimal digits 0… 9 Blank space, Horizontal
a…z tab, new line

Special Characters
Data Types
Basic data types:

 character : char
hold ASCII character or any quantity.

 integer : int
Hold integer quantity

 float : float
 double : double
Both hold real numbers
All data types defined by ANSI C
Type Typical size in bits Minimal Range
char 8 -128 to 127
unsigned char 8 0 to 255
signed char 8 -128 to 127
int 16 or 32 -32,768 to 32,767
unsigned int 16 to 32 0 to 35,535
signed int 16 or 32 same as int
short int 16 same as int
unsigned short int 16 0 to 65,535
signed short int 16 same as short int
long int 32 -2,147,483,648 to
2,147,483,647
signed long int 32 same as long int
unsigned long int 32 0 to 4,294,967,295
float 32 six digits of precision
double 64 ten digits of precision
long double 80 ten digits of precision
Data Type Modifier
Data Types
char
int
float
double
Type Modifiers
long : long int i; long double d; long i
short :
signed : positive/negative ; signed char
Unsigned : unsigned char; unsigned int i; unsigned i
short int: shorter then int
long int: larger than int
16-bit machine: int 16-bit, long int 32-bit, short int
16-bit.
32-bit machine: int 32-bit, long int 32-bit.
Constants
Constants refer to fixed values that the
program cannot alter. Constants can be of any
of the basic data types. The way each constant
is represented depends upon its type.
Constants are also called literals.
Data types Constant Examples

int 124

float 123.23

char ‘a’
Variables
A variable is a data name that may be used to store a data value
that may be modified by the program. All variables must be
declared before they can be used. For instance, amount, average,
sum, Total etc.
Variable names must comply the following conditions
 They must begin with a letter. Some systems permit underscore as
the first character.
 Length should not be normally more than 8 characters.
 Uppercase and lowercase are significant. That is, the variable
Total is not the same as total or TOTAL.
 White space is not allowed.
 It should not be a keyword, but keyword can be a part of variable,
such as int_type.
Declaration of Variables
The general form of a declaration is
type variable_list;

Here, type must be a valid data type, and variable_list


may consist of one or more identifier names separated by
commas. Here are some declarations:
int i,j,;
or you could write using two statement,
int i;
int j;
float f1, f2;
Variables
As you probably know, a variable is a named location in memory that is
used to hold a value that may be modified by the program. All variables
must be declared before they can be used. The general form of a
declaration is
type variable_list;

Here, type must be a valid data type, and variable_list may consist of one
or more identifier names separated by commas. Here are some
declarations:
int i,j,;
or you could write two statement,
int i;
int j;

float f1, f2;


Expressions
Operators, constants, and variables are the
constituents of expressions. An expression in
C/C++ is any valid combination of these
elements. Because most expressions tend to
follow the general rules of algebra, they are
often taken for granted.
For example, the expression
x = f1 + f2;
 C token (a keyword, an identifier, a constant, a string
literal, or a symbol)

 keyword (reserved words may not be used as constant or


variable or any other identifier names)

 Identifiers (used to identify a variable, function)


C program first inputs an integer and then
prints it.
 Input is given using scanf library function.
 Output is printed on screen using printf library
function.

Input Program Output


Input scanf ( ) and format specifier

Format Meaning
%c means print a single character
%d means print a decimal integer
%f means print a floating point value without exponent
%e means print a floating point value with exponent
%s means print null terminated character string

scanf (“format-specifier”, &variable_name);


Output printf ( ) and format specifier

Format Meaning
%c means print a single character
%d means print a decimal integer
%f means print a floating point value without exponent
%e means print a floating point value with exponent
%s means print null terminated character string

Printf (“format-specifier”, variable_name);


USE of Constant values
# include <stdio.h>
Use of Semicolon ;
To separate one main ()
statement from another
{
int a;
int b;
int c;

a=5;
b= 15;
c= a+b;
printf ("c = %d", c );
}
USE of Constant values
#include<stdio.h>

main()
{
int h_age, t_age, k_age, a_age;

h_age = 18;
t_age = 14;
k_age = 12;
a_age = 11;

printf("%d %d %d %d ", h_age, t_age, k_age, a_age);

Output:
18 14 12 11
C program first inputs an integer and then prints
it.
#include <stdio.h>

main()
{
int a; /*Variable definition: */

printf("Enter an integer\n"); /*Print message to show this text*/


scanf("%d", &a); /*Input value*/

printf("Integer that you have entered is %d\n", a); /*output value*/

}
Operator
 An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations. Operators are
used in programs to manipulate data and variables.
 C operators can be classified into a number of categories.
 Arithmetic operators
 Relational operators
 Logical operators
 Assignment operators
 Increment and decrement operators
 Conditional operators
 Bitwise operators
 Special operators
Arithmetic operators
 The arithmetic operators used for numeric calculations. They
are two types:
 Unary Arithmetic Operators
+x -y , ++,--,!, ~
 Binary Arithmetic Operators

Operator meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulus division
Arithmetic operators
 Note:,
 The % operator cannot be applied to a float or double.
 The precedence of arithmetic operators
Unary + or -
* / %
+ -
Operator

a3 = a * a * a
Operand
Arithmetic expressions
An arithmetic expression is a combination of variables,
constants, and operators.

For example,
 a*b-c  a*b-c
 (m+n)(x+y)  (m+n)*(x+y)
 ax2+bx+c  a*x*x+b*x+c
Arithmetic expressions
 Integer Arithmetic: When both operands are integers
for example if a=17 and b=4

Expression Result
a+b 21
a-b 13
a*b 68
a/b 4
a%b 1
Arithmetic expressions
 Real Arithmetic: When both operands are float types
for example if a=12.4 and b=3.1
Expression Result
a+b 15.5
a-b 9.3
a*b 38.44
a/b 4.0
a%b Not applicable
Arithmetic expressions
 Mixed-mode Arithmetic: When one operand is integers
and another is real .for example if a=12 and b=2.5

Expression Result
a+b 14.5
a-b 9.5
a*b 30.0
a/b 4.8
C program to add two numbers
#include<stdio.h>

main()
{
int a, b, c;

printf("Enter first number to add\n");


scanf("%d", &a); /* store the first value in a*/

printf("Enter second number to add\n");


scanf("%d", &b);

c = a + b; /* store a+b into c*/

printf("Sum of entered numbers = %d\n", c); /* print the value of c*/

}
Enter the temperature in Celsius and
convert that into Fahrenheit

#include<stdio.h>

int main()
{
float cent, farn;

printf("Enter temperature in centigrade\n");


scanf("%f", &cent);

farn = 1.8 * cent + 32;

printf(“Farenheit equivalent is = %.2f\n", farn);

return 0;
}
Float a = 5; Output:
Printf (“%f”, a); 5.0000000

Float a = 5; Output:
Printf (“% .2f”, a); 5.00

Float a = 5.88989; Output:


Printf (“% .3f”, a); 5.890
#include<stdio.h>
main()
{
int months , days ;
printf ( "Enter days \n “ ) ;
scanf ( "%d“ , &days ) ;
months = days / 30 ;
days = days % 30 ;
printf ( "Months =%d Days= %d \n", months, days);
}
Assigning values to char
char letter; /* declare variable letter of type char */

letter = ‘A'; /* OK */
letter = A; /* NO! Compiler thinks A is a variable */
letter = “A"; /* NO! Compiler thinks “A" is a string */
letter = 65; /* ok because characters are really
stored as numeric values (ASCII code),
but poor style */

String constant Character constant


“ HELLO ’’ ‘a’

You might also like