CSE287JUL2021 - LectureSlide - 03 (Variables+Datatypes+IO)
CSE287JUL2021 - LectureSlide - 03 (Variables+Datatypes+IO)
Computer Programming
Lecture 3:
Variables, Datatypes and I/O in C
• Global Variable
Outside all function
Can be accessed by any function
Local Variable
# include <stdio.h>
int main(void)
{
int a; Local variable
a=5;
printf(“This is a short C program”);
return 0;
}
• Modifiers • Except type void, the basic data types may have various
• long modifiers preceding them.
• short • Multiple modifiers can be used in a declaration
• unsigned • Example
• signed • int
• long double
• unsigned long int
Datatypes
Finding Sizes and Ranges of Datatypes
#include<stdio.h>
#include <limits.h>
#include <float.h>
int main(){
printf("char=%lu, short=%lu, int=%lu, long=%lu, long long=%lu\n",sizeof(char),
sizeof(short), sizeof(int), sizeof(long), sizeof(long long));
printf("Range of signed char %d to %d\n", SCHAR_MIN, SCHAR_MAX);
printf("Range of unsigned char 0 to %d\n\n", UCHAR_MAX);
printf("Range of signed short int %d to %d\n", SHRT_MIN, SHRT_MAX);
printf("Range of unsigned short int 0 to %d\n\n", USHRT_MAX);
printf("Range of signed int %d to %d\n", INT_MIN, INT_MAX);
printf("Range of unsigned int 0 to %lu\n\n", UINT_MAX);
printf("Range of signed long int %ld to %ld\n", LONG_MIN, LONG_MAX);
printf("Range of unsigned long int 0 to %lu\n\n", ULONG_MAX);
// In some compilers LLONG_MIN, LLONG_MAX
printf("Range of signed long long int %lld to %lld\n", LONG_LONG_MIN, LONG_LONG_MAX);
// In some compilers ULLONG_MAX
printf("Range of unsigned long long int 0 to %llu\n\n", ULONG_LONG_MAX);
printf("Range of float %e to %e\n", FLT_MIN, FLT_MAX);
printf("Range of double %e to %e\n", DBL_MIN, DBL_MAX);
printf("Range of long double %e to %e\n", LDBL_MIN, LDBL_MAX);
return 0;
}
Printing Variables
Conversion Specifiers
#include <stdio.h> • Character : %c
int main(void)
{ • Integer: %d
int num=10; • Long Integer: %ld
printf(“The value is=%d", num);
return 0; • Long long Integer: %lld
}
• Float : %f, %g, %e, %E, %a
• Double : %lf
• Long Double: %Lf
See for details: • Hexadecimal : %x and %X
https://en.wikipedia.org/wiki/C_data_types • Octal : %o
Examples on printf
#include<stdio.h>
#define AREA length*width Alphabets in the number representation
will be shown in capital letter
int main(void)
{
printf("%d %o %x %X\n", 90, 90, 90, 90);
printf("%e %E\n", 99.231, 99.231);
return 0;
}
Output:
Format Specifier
• printf ("Preceding with blanks: %10d \n", 1977);
• print as a decimal integer with a width of at least 10 wide with space padded to
left
• printf ("Preceding with zeros: %010d \n", 1977);
• print as a decimal integer with a width of at least 10 wide with zero padded to
left
• printf("%0.2f\n",d);
19