You are on page 1of 31

Programming in C

Variables

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Variable Types
Integer

int

Character

char

Floating Point

float
double

C String

char*
char[]

Boolean

bool

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Types

Variable Type

Declaration

char

char c = a;

int

int n = 10;

Bool (Not Standard More on this later)

bool b = false;

float

float f = 10.22;

double

double d = 10.1123456;

char*

char* ch = Hey, Buddy!;

char[]
Requires string.h

char buffer[50];
strcpy(buffer, Hey, Buddy!);

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Signed Vs. Unsigned

Signed is typically default


signed int n1 = -100;
Same as:
int n1 = -100;

Signed: # can be
positive or negative

unsigned int n2 = 122;


Unsigned: # must be
positive

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Size Qualifiers


Specifies size requirements for Integers

In standard C integers are 16-bits Although, default sizes of data types


are different between systems based on OS, system architecture, compiler,
etc
C contains libraries that standardize this.. We will discuss this later!

Qualifier

Description

Declaration

short

16-bit integer

short int n1 = 0;
short n2 = 0;

Unsigned Range: [0 to 65,535]


Signed Range: [-32,768 to 32,767]

long

32-bit integer

long int n1 = 9999;


long n2 = 10000;

64-bit integer

long long int n1 = 99999;


long long n2 = 100000;

Unsigned Range: [0 to 4,294,967,295]


Signed Range: [-2,147,483,648 to 2,147,483,647]

long long

Unsigned Range: [0 to 18,446,744,073,709,551,615]


Signed Range: [-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807]

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Size Qualifiers


Can also be used with Signed or Unsigned

keywords
unsigned long int n = 1234567890;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Constants


Variable that retains its value throughout the

execution of a program (read only)


const float pi = 3.14;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Symbolic Constants

Constant variable that is declared as a

preprocessor directive

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Symbolic Constants

Constant variable that is declared as a

preprocessor directive

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Symbolic Constants

Can also be used to store portions of code

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Variable Constraints: Symbolic Constants

Can also be used to store portions of code

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Typecasting
Converting one data type to another

int x = 4;
int y = 1;
float z = y / x;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Typecasting
Converting one data type to another

int x = 4;
int y = 1;
float z = y / x;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Typecasting
Converting one data type to another

int x = 4;
int y = 1;
float z = (float)y / (float)x;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Typecasting
Converting one data type to another

int x = 4;
int y = 1;
float z = (float)y / (float)x;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Typecasting
Converting one data type to another

char ch = A;
int c = (int)ch;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Typecasting
Converting one data type to another

char ch = A;
int c = (int)ch;

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Data Type Conversion


Must include stdlib.h
ASCII to Integer

int n = atoi(123456);
ASCII to Float

float f = atof(123456.12);

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Data Type Conversion: sprintf


Converts any data type to a Cstring using a formatted string

#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Data Type Conversion: sprintf


Converts any data type to a Cstring using a formatted string

#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Data Type Conversion: sprintf


Converts any data type to a Cstring using a formatted string

#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Data Type Conversion: sprintf


Converts any data type to a Cstring using a formatted string

#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Data Type Conversion: sprintf


Converts any data type to a Cstring using a formatted string

#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

12345

Programming in C

Gotcha!!!!
Pay attention to this

#include <stdio.h>
int main()
{
char c;
printf("%c\n", c);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Gotcha!!!!
Pay attention to this

#include <stdio.h>
int main()
{
char c;
printf("%c\n", c);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

Programming in C

Gotcha!!!!
Pay attention to this

#include <stdio.h>
int main()
{
char c;
printf("%c\n", c);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

Programming in C

Gotcha!!!!
Pay attention to this

#include <stdio.h>
int main()
{
char c;
printf("%c\n", c);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

Programming in C

Gotcha!!!!
Pay attention to this

#include <stdio.h>
int main()
{
char c;
printf("%c\n", c);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

Programming in C

Gotcha!!!!
Pay attention to this

#include <stdio.h>
int main()
{
int i = 0;
printf("%d\n", i);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

Programming in C

Gotcha!!!!
Pay attention to this

#include <stdio.h>
int main()
{
int i = 0;
printf("%d\n", i);
return 0;
}
Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

10101
10111
00111

10101
10111
00111

00000
00000
00000

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

10101
10111
00111

Programming in C

Programming in C
The End
Thanks for watching!

Copyright BlueSignet LLC. All rights reserved. For more visit WiBit.Net

You might also like