Variables and Data Types
Variables and Data Types
C standard types
int, double, char, float, short, long, long double
unsigned long
VARIABLE DEFINITIONS
• A declaration introduces a variable’s name into a program and
specifies its type
• A definition is a declaration which also sets asides memory for
that variable (which is usually the case)
• In C it is possible to initialize a variable at the same
time it is defined, in the same way one assigns a value
to an already defined variable.
int main()
declares the main function. Every C-program must have a function named
main somewhere in the code
{ ... }
The symbols { and } mark the beginning and end of a block of code.
printf(”Hello World\n”)
Sends output to the screen. The portion in quotes ”...” is the format
strings and describes how the data is formatted.
return 0;
This causes the function main to return an error code of 0 (no error) to the
shell that started the program.
COMPILATION & LINKING
header file
source file stdio.h
helloworld.c
#include <stdio.h>
compiler
object file
helloworld.o
linker
executable file
helloworld
CONSTANTS
• constants can be specified using the preprocessor
directive #define
example:
#define PI 3.14159
• the preprocessor replaces the identifier PI by the text
3.14159 throughout the program
• the major drawback of #define is that the data type of
the constant is not specified
• the preprocessor merely replaces all occurrences of the
string PI with 3.14159
#include <stdio.h>
int main()
{
int a;
double x;
char c;
printf(“Enter integer:”);
scanf(“%d”,&a);
printf(“\nEnter double:”);
scanf(“%lf”,&x);
printf(“\nEnter character:”);
scanf(“%c”,&c);
printf(“\nThe value of a is %d, of x is %lf, of c is %c\n”,a,x,c);
}
HEADER FILES
• a header file contains the declaration of functions
you want to use in your code
• the preprocessor directive #include takes care of
incorporating a header file into your source file
• example:
#include <math.h>
#include ”myprog.h”
• the brackets <> indicate that the compiler first searches
the standard include directory which contains the
standard C header files first
• the quotation marks indicate that the compiler first searches
for header files in the local directory
• if you do not include the appropriate header file
you get an error message from the compiler
HEADER AND LIBRARY FILES
library header file
#include <math.h>
math.h
myprog.c
#include ”myprog.h” user header file
myprog.h
compiler
object file library file
myprog.o libm.a
linker
executable file
myprog