C Programming - Lecture 2 - Constants Variables Datatypes
C Programming - Lecture 2 - Constants Variables Datatypes
10/27/2020
Characterset
• Letters
• A…Z
• a..z
• Digits
• 0…9
• Special characters
• ,.:;&*-|/\%(){}[]^+’~”<>#@
• White spaces
• Blank space, horizontal tab, carriage return, new line, form feed
Variable Names
• Names may contain letters, digits and underscores
• The first character of a variable name must be a letter or an underscore.
• the underscore can be used but don’t do.
• int main()
• {
• char ch1 = 120, ch2 = 10;
• ch1 = ch1 + ch2;
• printf("%d\n", ch1);
• printf("%c\n", ch1 - ch2 - 4);
ch1=65;
printf(“%c”,ch1);
printf(“%c”,char(ch1+32));
• return 0;
• }
Dr. Muhammad Aun Abbas 10/27/2020
#define
• #define is a preprocessor directive.
• Things defined by #define are replaced by the preprocessor
before compilation begins.
• The big advantage of const over #define is type checking.
• #include<stdio.h>
#define val 10
#define floatVal 4.5
#define charVal 'G'
int main()
{
printf("Integer Constant: %d\n",val);
printf("Floating point Constant: %.1f\n",floatVal);
printf("Character Constant: %c\n",charVal);
return 0;
}
Dr. Muhammad Aun Abbas 10/27/2020
Tokens in C
• There are a number of qualifiers which can be applied to the basic types
• length of data
• short int: "shorter" int, <= number of bits in an int
can also just write "short"
• long int: "longer int", >= number of bits in an int
often the same number of bits as an int
can also just write "long"
• long double generally extended precision floating point
• signed and unsigned
• unsigned int an int type with no sign
if int has 32-bits, range from 0..2 32-1
also works with long and short
• unsigned char a number from 0 to 255
• signed char a number from –128 to 127 (8-bit signed value)
• These numbers are highly variable between C compilers and computer architectures.
• Programs that rely on these figures must be very careful to make their code portable (ie. Try
not to avoid relying on the size of the predefined types)
• Generic Form
typename varname1, varname2, ...;
• Examples:
int count;
float a;
double percent, total;
unsigned char x,y,z;
long int aLongInt;
long AnotherLongInt
unsigned long a_1, a_2, a_3;
unsigned long int b_1, b_2, b_3;
typedef long int32;
int32 n;
• Where declarations appear affects their scope and visibility
• Rules are similar to those in Java
• Declaration outside of any function are for global variables
• e.g., just before the main routine
Initialization
• ALWAYS initialize a variable before using it
• Failure to do so in C is asking for trouble
• The value of an uninitialized variables is undefined in the C standards
• Examples:
int count; /* Set aside storage space for count */
count = 0; /* Store 0 in count */
• This can be done at definition:
int count = 0;
double percent = 10.0, rate = 0.56;
• Warning: be careful about “out of range errors”
unsigned int value = -2500;
• The C compiler does not detect this as an error
• What do you suspect it does?
Constants
Note: simple computed
• You can also declare variables as being constants
values are allowed
• Use the const qualifier: • must be able to evaluate
const double pi=3.1415926; at compile time
const int maxlength=2356;
const int val=(3*7+6)*5;
• Constants are useful for a number of reasons
• Tells the reader of the code that a value does not change
• Makes reading large pieces of code easier
• Tells the compiler that a value does not change
• The compiler can potentially compile faster code
Preprocessor Constants
• These are an older form of constant which you still see
• There is a potential for problems, so be careful using them!
• Generic Form:
#define CONSTNAME literal
• Generally make pre-processor constants all upper case (convention).
• Example:
#define PI 3.14159
• What really happens
• The C preprocessor runs before the compiler.
• Every time it sees the token PI, it substitutes the value 3.14159.
• The compiler is then run with this “pre-processed” C code.
• Why this is dangerous?
• Hard to determine the value of a multiply-defined constant (which you are allowed to create)