Programming in C - 05 - Variables
-
Upload
jonathan-mayamiko-nsapato -
Category
Documents
-
view
223 -
download
0
Transcript of Programming in C - 05 - Variables
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 1/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Programming V
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 2/31
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
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 3/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
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!”);
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 4/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Signed Vs. Unsigned
Signed: # cpositive or n
Unsigned: #positi
Signed is typically default signed int n1 = -100;
Same as:
int n1 = -100;
unsigned int n2 = 122;
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 5/31Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Size Qualifiers
Specifies size requirements for Integers• In standard C integers are 16-bits… Although, default sizes of data type
are different between systems based on OS, system architecture, com
etc…
• C contains libraries that standardize this.. We will discuss this later!
Qualifier Description Declaration
short 16-bit integerUnsigned Range: [0 to 65,535]Signed Range: [-32,768 to 32,767]
short int n1 = 0;
short n2 = 0;
long 32-bit integerUnsigned Range: [0 to 4,294,967,295]
Signed Range: [-2,147,483,648 to 2,147,483,647]
long int n1 = 9999;
long n2 = 10000;
long long 64-bit integerUnsigned 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]
long long int n1 =
long long n2 = 1000
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 6/31Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Size Qualifiers
Can also be used with Signed or Unsigned
keywords
unsigned long int n = 1234567890;
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 7/31Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Constants
Variable that retains its value throughout the
execution of a program (read only)
const float pi = 3.14;
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 8/31Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Symbolic Constants
Constant variable that is declared as a
preprocessor directive
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 9/31Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Symbolic Constants
Constant variable that is declared as a
preprocessor directive
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 10/31Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Symbolic Constants
Can also be used to store portions of code
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 11/31Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Variable Constraints: Symbolic Constants
Can also be used to store portions of code
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 12/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Typecasting
Converting one data type to another
int x = 4;
int y = 1;
float z = y / x;
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 13/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Typecasting
Converting one data type to another
int x = 4;
int y = 1;
float z = y / x;
T i
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 14/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Typecasting
Converting one data type to another
int x = 4;
int y = 1;
float z = (float)y / (float)x;
T ti
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 15/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Typecasting
Converting one data type to another
int x = 4;
int y = 1;
float z = (float)y / (float)x;
T ti
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 16/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Typecasting
Converting one data type to another
char ch = ‘A’;
int c = (int)ch;
T ti
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 17/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Typecasting
Converting one data type to another
char ch = ‘A’;
int c = (int)ch;
D t T C i
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 18/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Data Type Conversion
Must include stdlib.h
ASCII to Integerint n = atoi(“123456”);
ASCII to Float
float f = atof(“123456.12”);
Data Type Conversion: sprintf
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 19/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Data Type Conversion: sprintf
Converts any data type to a Cstring using a formatted
#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Data Type Conversion: sprintf
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 20/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Data Type Conversion: sprintf
Converts any data type to a Cstring using a formatted
#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Data Type Conversion: sprintf
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 21/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Data Type Conversion: sprintf
Converts any data type to a Cstring using a formatted
#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Data Type Conversion: sprintf
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 22/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Data Type Conversion: sprintf
Converts any data type to a Cstring using a formatted
#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
Data Type Conversion: sprintf
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 23/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Data Type Conversion: sprintf
Converts any data type to a Cstring using a formatted
#include <stdio.h>
int main()
{
char ch[256];
int num = 12345;
sprintf(ch, "%d", num);
printf("%s\n", ch);
return 0;
}
12345
Gotcha!!!!
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 24/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Gotcha!!!!
Pay attention to this…
#include <stdio.h>int main()
{
char c;
printf("%c\n", c);
return 0;
}
Gotcha!!!!
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 25/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Gotcha!!!!
Pay attention to this…
#include <stdio.h>int main()
{
char c;
printf("%c\n", c);
return 0;
}
Gotcha!!!!
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 26/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Gotcha!!!!
Pay attention to this…
#include <stdio.h>int main()
{
char c;
printf("%c\n", c);
return 0;
}
10101
10111
00111
10101
10111
00111
1010110111
00111
10101
10111
00111
10101
10111
00111
10101
10111
00111
Gotcha!!!!
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 27/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Gotcha!!!!
Pay attention to this…
#include <stdio.h>int main()
{
char c;
printf("%c\n", c);
return 0;
}
10101
10111
00111
10101
10111
00111
1010110111
00111
10101
10111
00111
10101
10111
00111
10101
10111
00111
Gotcha!!!!
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 28/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Gotcha!!!!
Pay attention to this…
#include <stdio.h>int main()
{
char c;
printf("%c\n", c);return 0;
}
10101
10111
00111
10101
10111
00111
1010110111
00111
10101
10111
00111
10101
10111
00111
10101
10111
00111
µ
Gotcha!!!!
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 29/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Gotcha!!!!
Pay attention to this…
#include <stdio.h>int main()
{
int i = 0;
printf("%d\n", i);return 0;
}
10101
10111
00111
10101
10111
00111
1010110111
00111
10101
10111
00111
10101
10111
00111
10101
10111
00111
Gotcha!!!!
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 30/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Gotcha!!!!
Pay attention to this…
#include <stdio.h>int main()
{
int i = 0;
printf("%d\n", i);return 0;
}
10101
10111
00111
10101
10111
00111
0000000000
00000
10101
10111
00111
10101
10111
00111
10101
10111
00111
8/17/2019 Programming in C - 05 - Variables
http://slidepdf.com/reader/full/programming-in-c-05-variables 31/31
Copyright © BlueSignet LLC. All rights reserved. For more visit WiBit.Net
Programming
Thanks for