Wednesday, November 13, 2013

C Programming Basics - Lesson 2 (Variables & Data Types)


C Programming Basics - Lesson 2 (Variables & Data Types)


Variables and Data Types என்றால் என்ன ?

                    இந்த Data Types பல்வேறு வகையான எண்கள், எழுத்துக்கள் போன்றவற்றை program இல் பயன்படுத்துவதற்கு இது உதவுகின்றன. Variables என்றால் memory இல் நாம் இதனை store செய்வதற்காக நாம் ஒரு பெயரை கொடுப்பது. இதில் நாம் அடிக்கடி பயன்படுத்தக் கூடிய Data Types கள் கொடுக்கப்பட்டுள்ளன .

Integer - இது முழு எண்களை நாம் program இல் உபயோக்கிக்கும் போது அதை represent செய்வதற்காக இந்த data type பயன்படுத்தப்படுகிறது. உதாரணமாக program 2 இல் a =10, இப்போது a  இன் மதிப்பு (value) 10. பத்து  என்பது முழு எண்  என்பதால்  integer பயன்படுத்தப்படுகிறது. இந்த Integer என்பது program இல் பயன்படுத்தும்போது  int  என்று கொடுக்க வேண்டும். இதற்கு control string %d.

Float - இந்த float என்பது floating point numbers ஐக் குறிக்கிறது. அதாவது புள்ளி வைத்த எண்கள். 12.5, 78.23, 124.12 என்பது போன்றவை . இந்த Float என்பது program இல் பயன்படுத்தும்போது  float  என்று கொடுக்க வேண்டும்.இதற்கு control string %f.

Character -  charater data type எழுத்துக்களை உபயோக்கிக்கும் போது பயன்படுத்தபடுகிறது. உதாரணமாக A என்ற எழுத்தை கொடுக்க வேண்டும் என்றால் அதோட declaration இவ்வாறு இருக்க வேண்டும் அதாவது char a = 'A '; இந்த char datatype க்கு ' ' என்ற குறிஈடுக்குள் ஒரு எழுத்து மட்டுமே கொடுக்க வேண்டும். இதற்கு control string %c. ஒன்றுக்கும் மேற்ப்பட்ட எழுத்துக்கள் இருந்தால் variable க்கு முன்னாள் * என்பதை இடைவெளி இல்லாமல் சேர்த்து கொள்ள வேண்டும். உதாரணமாக,  Mohamed என்பது ஒன்றுக்கும் மேற்பட்ட எழுத்துகள் உள்ளதால் " " எந்த இரண்டு குறியீடுகளுக்குள் கொடுக்க வேண்டும்.அதாவது,  char *a = "Mohamed"; இதற்கு control string %s.

int a,b; இதுவும் ஒரு statement. எனவே முடிவில் ; (semicolon ) இடப்பட்டிருக்க வேண்டும். int a,b; இதுபோல் நாம் ஒவ்வொரு variable ஐயும் அதற்கு தகுந்த data type வைத்து declare செய்வதை variable declaration என்று கூறுவர்.

இன்னும் இதுபோல் பல data type கள் c இல் உள்ளன. அதைப்பற்றி இன்ஷா அல்லாஹ்.. இனி வரும் பாடங்களில் பார்ப்போம்.

Control Strings என்றால் என்ன ?

கீழ்க்கண்ட program களில் printf() என்ற function இல் %d %f %c என்று பயன்படுத்தப்பட்டுள்ளது. இதை control string என்பர். அதாவது, output எந்த வடிவில் print செய்ய வேண்டும் என்பதை குறிக்கும். integer variable ஆக இருந்தால் %d  (decimal integer) எனவும், float variable ஆக இருந்தால் %f (float) எனவும் character variable ஆக இருந்தால் %c (character) எனவும் கொடுக்கப்பட வேண்டும்.

உதாரணமாக, program 4 இல் printf("The Value of c is %d",c ); இதன் output எப்படி இருக்கும் என்றால் The Value of c is 30. இங்கு %d என்பது control string . c என்பது c  value ஐ print செய்ய வேண்டும் என்பது.


Program - 2 (இரண்டு variable இல் உள்ள எண்களை காட்டுவதற்காக எழுதப்படும் c program )

#include <stdio.h>
void main() 
{
int a,b; 
a=10;
b=20;
printf ("The value of a is %d",a);
printf("The value of b is %d",b);
}

Program 3 (மூன்று வகையான variables களின் value ஐ காட்டுவதற்காக எழுதப்படும் c  program )

#include <stdio.h>
void main()
{
 int a;
 float b;
 char c;
 a=10;
 b=3.7;
 c='A';
 printf("%d",a);
 printf("%f",b);
 printf("%c",c);
}

Program-4 (இரண்டு எண்களின் கூட்டுத்தொகையை (addition of two numbers ) காட்டுவதற்காக எழுதப்படும் c  program)

#include <stdio.h>
void main()
{
int a,b,c;
a=10;
b=20;
c=a+b;
printf("The value of c is %d",c);
}


Exercises 

Program 5) Write a c program to find the subtraction of two numbers.
Program 6) Write a c program to find the division of two numbers
Program 7) Write a c program to find the multipication of two numbers.


No comments:

Post a Comment