0% found this document useful (0 votes)
13 views

CSE287JUL2021 - LectureSlide - 03 (Variables+Datatypes+IO)

This document provides an overview of variables, data types, and input/output in C programming. It discusses that variables are used to store values in memory and must be declared before use. The main data types in C include integers, floats, characters, and void. It also demonstrates how to declare variables, find the size and range of data types, print variables using format specifiers, and take input from the keyboard. Global and local variables are also explained.

Uploaded by

Khaled Mahmud
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CSE287JUL2021 - LectureSlide - 03 (Variables+Datatypes+IO)

This document provides an overview of variables, data types, and input/output in C programming. It discusses that variables are used to store values in memory and must be declared before use. The main data types in C include integers, floats, characters, and void. It also demonstrates how to declare variables, find the size and range of data types, print variables using format specifiers, and take input from the keyboard. Global and local variables are also explained.

Uploaded by

Khaled Mahmud
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CSE287

Computer Programming

Lecture 3:
Variables, Datatypes and I/O in C

Khaled Mahmud Shahriar


Assistant Professor
Department of CSE, BUET Offered to: Department of MME, L-2, T-1
Variables
• Variables are placeholders
• They can hold values
• Each variable takes up some memory space
• The values can be assigned, changed, read etc.
• Variables must be defined before using them
Variable Declaration
• First write the keyword for datatype
• Then write the name of the variable
• Example
• int num=10;//initialization
• char c=‘a’;
• int i, j, k;
• char esc=‘\\’;
• float exp=3.2e-5;
Variables
• Name of variable
• Case sensitive
• Count, count & COUNT are different
• Can be of any length, but only first 31 characters are important
• Can contain letters, digits and the ‘_’
• But first character must be a letter or ‘_’
• Variable name cannot be same as a keyword
• For example –
correct: abcd, abcd2, abcd_3, Abcd
incorrect: ab cd, 2abcd, abcd…3, ab!cd
Keywords
• C has some words that has a special meaning for the compiler
• These words can not be used to name variables, functions etc.
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Variables
• Name of variables
• Should be clear and meaningful
• If two or more words are needed then either separate them
using a ‘_’ or keep them together, but start each word except the
first one with a capital
• For Example –
student_no average_age
dateOfBirth averageAge
• Second way is recommended
Variables
• Global variables
• Outside all function
• Can be accessed by any function
• Local variables/ automatic variables
• Inside a block/function
• Can be declared at the start of a block
Global Variable
# include <stdio.h>
int a; Global variable
int main(void)
{
a=5;
printf(“This is a short C program”);
return 0;
}

• 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;
}

Local variables/ automatic variables


Inside a block/function
Datatypes
• C has basically these data types-
• int (integer / whole number)
• Single precision (float) gives you 23 bits of
• float (floating point / fraction) significant, 8 bits of exponent, and 1 sign bit.
• double (double precision float) • Double precision (double) gives you 52 bits of
• significant, 11 bits of exponent, and 1 sign bit.
char (character)
• void (empty / no value )
• enum (enumeration)

• 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);

See for details:


https://en.wikipedia.org/wiki/Printf_format_string
More Examples on printf
#include <stdio.h>
int main() {
printf ("Characters: %c %c \n", 'a', 65);
printf ("Decimals: %d %ld\n", 1977, 650000L);
printf ("Preceding with blanks: %10d \n", 1977);
printf ("Preceding with zeros: %010d \n", 1977);
printf ("Some different radices: %d %x %o %#x %#o \n", 100,
100, 100, 100, 100);
printf ("floats: %4.2f %+.0e %E \n", 3.1416, 3.1416,
3.1416);
printf ("Width trick: %*d \n", 5, 10);
printf ("%s \n", "A string");
return 0;
}
Input numbers from keyboard
#include <stdio.h> & (ampersand) : ADDRESS
int main(void) OPERATOR
{
int num1, num2, num3;
scanf("%d", &num1);
scanf("%d %d", &num2, &num3;
printf("sum=%d", num1+num2+num3);
return 0;
}

• Input must be seperated by blank, tab or newline


• Common programming errors:
• Forgetting address operator (&) before variable name in scanf
• Placing commas (when none are needed) between conversion
specifiers
Acknowledgement
All the slides of this course have been prepared by taking help from numerous resources.
The notable contributors are listed below.
1. Content and organization of many pages have been taken from the lecture slides and
codes of the course CSE110 offered to the Department of EEE that were -
i. primarily created by Johra Muhammad Moosa, Assistant Professor (on leave),
CSE, BUET and
ii. later modified by Madhusudan Basak, Assistant Professor, CSE, BUET
2. Most of the wonderful coding examples have been taken from the course CSE281
offered to the Department of BME instructed by Rifat Shahriyar, Professor, CSE,
BUET (course link).
3. search and all the sites that it made available in response to the course
related queries. Some of the sites are: https://geeksforgeeks.org/,
https://www.tutorialspoint.com, https://www.w3schools.com and the list goes on …
Thank You

19

You might also like