C Program Learning Part-2 - Engineer Simple

Search This Blog

C Program Learning Part-2

 CONSTANTS, VARIABLES AND DATA TYPES

CHARACTER SET 

C Program Exercise Part-02

1. Letters 

2. Digits 

3. Special Characters

4. White Space 

C TOKENS 

In a passage of text, individual words and punctuation marks are called tokens. In C program the smallest individual units are known as C tokens. Six types of C tokens are used to write program and syntax of the language.

C TOKENS 

Keywords - float, while

Constants - 15.5, 200

Special Symbols - {}, []

Identifiers - main, amount

Strings - “ABC”, “YEAR”

Operators - +, -, *, /

Keywords and Identifiers 

Keywords

*Fixed meaning and can not be changed.

*Basic building block for program statement.

ANSI C Keyword

auto

double

int

struct

break

else

long

switch

case

enum

register

typedef

char

extern

return

union

const

for

signed

void

continue

float

short

unsigned

default

goto

sizeof

volatile

do

if

static

while


Identifiers 

*Refers to the names of variables, functions and arrays. 
*User defined names 
*Consists of a sequence of letters and digits.

Rules for Identifiers 

o First character must be an alphabet (or underscore). 
o Consists of only letters, digits and underscore. 
o Only first 31 characters are significant. 
o Cannot be keyword. 
o Must not contain white space.

Variables 


*A variable is a data name that may be used to store data value 
*A Constant remains unchanged during the execution of the program. 
* A variables may take different values at different times during execution. 

Conditions of Variables 


o First character must be an alphabet (or underscore). 
o Consists of only letters, digits and underscore. 
o Only first 31 characters are significant. 
o Cannot be keyword. 
o Must not contain white space 
o Uppercase and Lowercase are significant.

Data types 
ANSI C supports three classes of data type: 
1. Primary (or Fundamental) Data Type 
2. Derived Data Type 
3. User-defined Data Type


Primary Data Type

Signed

Range

Unsigned

Range

int or signed int (16 bit)

-32768 to 32767

unsigned int (16 bit)

0 to 65535

short int or signed short int (8 bit)

-128 to 127

unsigned int (8 bit)

0 to 255

long int or signed long int (32bit)

-214,74,83,648 to 214,74,83,647

unsigned long int (32 bit)

0 to 4294967295

float (32 bit)

3.4E-38 to 3.4E+38

double (64 bit)

1.7E-308 to 1.7E+308

long double (80 bit)

3.4E-4932 to 1.1E+4932

 

 

char or signed 1/2/2020 char (8 bit)

-128 to 127

unsigned char (8 bit)

0 to 255


Declaration of Variables 


*Tell the compiler what the variable name is 
*What type of data the variable will hold. 

The Syntax for declaring a variable is as follows

data-type V1, V2 ,…….., Vn; 
valid declarations are int count; 
int number, total; 

Assigning Values to Variables 


Values can be assigned to variables using the assignment operator = as follows: 
variable name = constant; 
balance = 2000; 
sum = 500;  

Reading data from Keyboard 


Giving values to variables is to input data through keyboard using the scanf function. 
The general format of scanf is as follows:
scanf(“control string”, &variable1, &variable2,…….);
scanf(“%d”,&number); for integer type
scanf(“%f”,&number); for float type
scanf(“%c”,&name); for character type

OVERFLOW OF DATA 


Problem of data overflow occurs when the value of a variable is either too big or too small for the data type to hold. When the addition result is bigger than the biggest number of bits can hold. For integer type overflow occurs when the variable exceeds the highest positive range value. Highest integer type variable value is 65535, when the value assigned to 65536 it occurs overflow. 

UNDERFLOW OF DATA 


When the value assigned to variable is less than the minimum allowable limit of an integer like -32768, integer underflow occurs.

You Can read our article about "C Program Overview" by clicking here...






Next Post Previous Post
No Comment
Add Comment
comment url