0% found this document useful (0 votes)
8 views25 pages

C Programming: Variables and Operations

Uploaded by

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

C Programming: Variables and Operations

Uploaded by

kabirsameer2008
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming

Variables and Operations

J. K. Deka
Dept of Comp. Sc. & Engg.
Indian Institute of Technology Guwahati

1
Basic C Programming
• Variable Name and Declaration
• Data types and sizes
• Constants
• Declarations of variable
• Operators in C
• Examples : Basic operations

2
C Programming : Sum of A and B
#include <stdio.h>
Header file:
Standard Input/Output
int main(){
int A,B, S; Variable Declarations
printf(“Enter two
numbers ”); Printing message
scanf(“%d %d”,&A,&B);
Asking for inputs
S=A+B;
Compute
printf(“Res=%d”, S);
return 0; Output Result
}
3
Identifier in C : Name
• Is a unique name
• That simply references to memory locations,
which can hold values (data).
• Identifiers give unique names
• To various objects in a program
• To variable and function
• Are formed by
• Combining letters (both upper and lowercase)
• Digits (0–9)
• Underscore ( _ ).
Rules for naming Identifier in C

• First character of an identifier


• Must be a letter (non-digit) including underscore ( _ ).
• Space: Blank or white space character is not
permitted - Spacebar, Tab
• linefeed, carriage-return, form-feed, vertical-tab, and
newline characters
• Length
• Can be any length but implementation dependent
• Reserved words/keywords cannot be used.
Identifier in C : Variable Name
Examples
Correct Wrong
secondName 2ndName
/* starts with a digit */
_addNumber %calculateSum
/* contains invalid character */
charAndNum char /* reserved word */
Identifier in C : Variable Name
Examples
Correct Wrong
annual_rate annual rate
/* contains a space */
stage4mark my\nName
/* contains new line
character, \n */
What Are Variables in C?
• Variables in C have the same meaning as
variables in algebra. That is, they represent
some unknown, or variable, value.

x=a+b
z + 2 = 3(y - 5)
• Remember that variables in algebra are
represented by a single alphabetic character.
Variables in C
• Variables are
– Named blocks of memory
– Valid identifier.
• Variable have two properties in syntax:
– Name — a unique identifier
– Type — what kind of value is stored.
• It is identifier, that
– Value may change during the program execution.
• Every variable stored in the computer’s
memory
– Has a name, a value and a type.
C Variable : Example
Correct
int x, y, z, my_data = 4;;
short number_one;
long TypeofCar;
unsigned int positive_number;
char Title;
float commission, yield = 4.52;
char the_initial = 'M'; //A char
char studentName[20]=“India“;//A string
C Variable : Example
Wrong Comments
int 3a, 1, -p; Start with
digit or -
short number+one; Have +
long #number; Start with #
Variable Naming Conventions
• C programmers generally agree on the
following conventions for naming
variables.
– Begin variable names with lowercase letters
– Use meaningful identifiers
– Separate “words” within identifiers with
underscores or mixed upper and lower case.
– Examples: surfaceArea,
surface_Area, surface_area
– Be consistent!
Case Sensitivity
• C is case sensitive
– It matters whether an identifier, such as a variable
name, is uppercase or lowercase.
– Example:
area
Area
AREA
ArEa
are all seen as different variables by the compiler.
Which Are Legal Identifiers?
AREA area_under_the_curve
3D num45
Last-Chance #values
x_yt3 pi
num$ %done
lucky***
Which Are Legal Identifiers?
AREA area_under_the_curve
3D num45
Last-Chance #values
x_yt3 pi
num$ %done
lucky***
Naming Conventions
• Use all uppercase for symbolic constants (used
in #define preprocessor directives).
• Examples:
 #define PI 3.14159
 #define AGE 52
C Programming : Sum of A and B
#include <stdio.h>
Header file:
Standard Input/Output
int main(){
int A,B, S; Variable Declarations
printf(“Enter two
numbers ”); Printing message
scanf(“%d %d”,&A,&B);
Asking for inputs
S=A+B;
Compute
printf(“Res=%d”, S);
return 0; Output Result
}
17
C Keywords/Reserve Words
 Reserved words are : not available for redefinition
 Can not be used as variable name
 Have special meaning in C
auto extern sizeof for
break float static goto
case inline struct if
char int switch else
const long typedef enum
continue register union do
default restrict unsigned void
double return volatile short
while signed
Data type and Size
• In C, data type categorized as:
– Primitive Types in ANSI C (C89)/ISO C (C90) –
• char, short, int, float and double.
– Primitive Types added to ISO C (C99) –
• long int, long double
– User Defined Types
• struct, union, enum and typedef (will be
discussed later).
– Derived Types
• pointer, array and function
pointer (will be discussed later).
19
Numeric Data Type
• char, short, int, long int
– char : 8 bit number (1 byte=1B)
– short: 16 bit number (2 byte)
– int : 32 bit number (4B)
– long int : 64 bit number (8B)
• float, double, long double
– float : 32 bit number (4B)
– double : 64 bit number (8B)
– long double : 128 bit number (16B)
20
Numeric Data Type
unsigned char
char
unsigned short
short

Unsigned int

int
21
Testing size of Numeric Data
#include<stdio.h>
int main(){
printf("size of char %d\n", sizeof(char)); //1
printf("size of short %d\n",sizeof(short)); //2
printf("size of int %d\n",sizeof(int)); //4
printf("size of long int %d\n",sizeof(long int)); //8

printf("size of float \n",sizeof(float)); //4


printf("size of double %d\n",sizeof(double));//8
printf("size of long double %d\n",
sizeof(long double));//16
return 0;
} 22
Numeric Data Type
• char, short, int, long int
– We have : Signed and unsigned version
– char (8 bit)
• char : -128 to 127
• unsigned char: 0 to 255
– int : -231 to 231-1
– unsigned int : 0 to 232-1
• float, double, long double
– For fractional, real number data
– All these numbered are signed and get stored in
different format
23
Sign bit Numeric Data Type

Exponent Mantissa
float

Exponent Mantiss-1

Mantissa-2
double
24
Thanks

25

You might also like