2 C Data Types, Constants, Variables, Expressions
2 C Data Types, Constants, Variables, Expressions
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 two statement,
int i;
int j;
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
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
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;
Output:
18 14 12 11
C program first inputs an integer and then prints
it.
#include <stdio.h>
main()
{
int a; /*Variable definition: */
}
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;
}
Enter the temperature in Celsius and
convert that into Fahrenheit
#include<stdio.h>
int main()
{
float cent, farn;
return 0;
}
Float a = 5; Output:
Printf (“%f”, a); 5.0000000
Float a = 5; Output:
Printf (“% .2f”, a); 5.00
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 */