SDF I - Lecture 4 Variables Keywords Datatypes
SDF I - Lecture 4 Variables Keywords Datatypes
ODD 2020
3
C Programming – Introduction and General Structure
1. C is a structured programming language
2. C program consists functions (user defined functions and library functions) and variables
3. Functions contain instructions corresponding to the computations and operations to be performed,
whereas variables stores values and used during computations
4. main() in c programming is user defined as well as predefined. Because main() is programmer/user
implemented function, whose prototype is predefined in the compiler.
5. main() function is the entry and exit point of a C program & must be there in a C program
codeblocks-20.03mingw-setup.exe
http://www.codeblocks.org/downloads/26
Code Blocks – Brief User Manual
1 3
Code Blocks – Brief User Manual
3 5
1. Source File, main
2. Code Block editor
having C program
6 3. Compile / Build
4. Compilation / Run
message display
2 block
1 5. Run / Execute
6. Output popup
window
4
Data
• Data is a piece of information that we used in programming to execute any program.
• These data are used in the programming language to execute the programme.
9
Variables
• Variables are stored in memory and each variable has an unique address in memory
region. For each variables memory is reserve in the computer.
• It is difficult to remember the unique address, thus for convenience, you can assign
a name to the variables.
• In variables you can store different type of data such as integer, string, float,
characters. All these data are of different sizes in computer.
11
Variable declaration syntax:
datatype variablename;
14
• For example,
const double root_two = 1.414;
• We have added keyword const to make the variable constant. Here root_two, is a
constant variable. Its value can’t be change.
• When you try to change root_two from 1.414 to 2.9 it will give an error.
root_two= 2.9; // error= constant value can’t change.
• You can also define a constant using the #define pre-processor directive.
15
Keywords in C
• There are certain words that are reserved in C.
• These words are known as keywords and each keyword have some
meaning.
• Keywords are always written in lowercase.
• There are 32 keywords available in C which are given below:
auto extern sizeof break static case
for struct goto switch const if
typedef enum signed default
int union long continue
unsigned do register void
double return volatile else
short while float char
16
Format Specifiers
• Format specifiers are used with printf() statement for printing the data that are
stored in variable.
• To print the data stored in variable you need to use the format specifier.
#include <stdio.h>
#include <stdio.h>
int main()
int main()
{
{
char a[] = “JIIT";
char ch = 'A';
printf("%s\n", a);
printf("%c\n", ch);
return 0;
return 0;
}
}
#include <stdio.h>
int main()
{
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", x);
printf("%u\n", x);
return 0;
}
45
45
45
Difference between %d and %i format specifier in C
When you are printing using the printf function, there is no specific difference between the %i and %d
format specifiers. But both format specifiers behave differently with scanf function.
The %d format specifier takes the integer number as decimal but the %i format specifier takes the integer
number as decimal, hexadecimal or octal type. it means the %i automatically identified the base of the input
integer number.
#include <stdio.h>
int main()
{
int data1, data2, data3;
printf("Enter value in decimal format:");
scanf("%d",&data1);
printf("data1 = %i\n\n", data1);
printf("Enter value in hexadecimal format:");
scanf("%i",&data2);
printf("data2 = %i\n\n", data2);
printf("Enter value in octal format:");
scanf("%i",&data3);
printf("data2 = %i\n\n", data3);
return 0;
}
Integer format specifier : %d, %i Floating-point format specifier : %f, %e or %E
#include <stdio.h>
#include <stdio.h>
int main()
int main()
{
{
float a = 12.67;
int x = 45, y = 90;
printf("%f\n", a);
printf("%d\n", x);
printf("%e\n", a);
printf("%i\n", x);
return 0;
printf("%u\n", x);
}
return 0;
}
Output:
45 12.670000
45 12.67000+e01
45
Use of special elements with %f Format specifiers (octal number): %o
#include <stdio.h>
int main() #include <stdio.h>
{ int main()
float data = 6.276240; {
printf("%f\n", data); int data = 65;
printf("%0.2f\n", data); printf("%o\n", data);
printf("%0.4f\n", data); return 0;
return 0; }
}
Output:
6.276240
6.28 Output: 101
6.2762
Use of special elements with %s
#include <stdio.h>
int main()
{ • A minus(-) sign tells left alignment.
char str[] = "Welcometojiit128"; • A number after % specifies the minimum field width to be
printf("%s\n", str); printed if the characters are less than the size of width the
printf("%3s\n", str); remaining space is filled with space and if it is greater than it
printf("%20s\n", str); printed as it is without truncation.
printf("%-20s\n", str); • A period( . ) symbol separate field width with the precision.
printf("%.3s\n", str); • Number after . tells that the maximum number of character
printf("%8.3s\n", str); to be printed
return 0;
}
Output
Welcometojiit128
Welcometojiit128
Welcometojiit128
Welcometojiit128
Wel
Wel
Data Types in C
• C supports different types of data such as integer, character, float and string etc.
• C provides various types of data-types which allow the programmer to select the
appropriate type for the variable to set its value.
• Data types are used to define the type of variable when it declared.
• Data types are used to define the types of parameter expected by a function.
• C supports many data types out of which the basic types are:
26
Char Data type
• Char type variable store single character.
• All character have a numeric code associated with them, so while storing a
character variable its numeric value gets stored.
• The set of numeric code is called as “ASCII”, American Standard Code for
Information Interchange.
27
Source: http://www.haghish.com/statistics/stata-blog/stata programming/ascii_characters.php 28
Char data type
• Characters (char) – To store a character into a char variable, you must enclose it
with SINGLE QUOTE marks i.e. by ‘ ’.
• The double quotes are reserved for string (a collection of character).
• You can assign a char variable as an integer i.e. its integer value.
‘a’, ‘z’ , ‘A’ , ‘Z’ , ‘?’, ‘@’, ‘0’, ‘9’
Declaration of char variable:
char c; OR char c, k, l;
30
Program using character constants
#include<stdio.h>
int main()
{
char p, q, r, s; /* declare char variables*/
char m=‘e'; /* declare char variable*/
p=‘W'; /* initialize the rest */
q='e'; /* q=e is incorrect */
r='l'; /* s=“c” is incorrect*/
s=‘c’;
t=‘o’;
u= 109 /* the ASCII value of m is 109 */
printf("%c%c%c%c%c%c%c", p, q, r, s, t, u, m);
return 0;
} Output: Welcome
31
Integer Data Type
• Integer data types are allows to store numeric value in variables.
• Integer data type are represented as int.
• Size of int type variable is 2 or 4 byte depends on operating system
configuration.
• If you try to store float value in integer variable then integer part will be stored
in variable and the decimal part is ignored and the value assigned is rounded
down from the actual value.
• Also assigning a character constant to an int variable assigns the ASCII value.
32
• Declaration on Integer variable:
int student_id;
• Here, student_id is a variable of type int.
• You can declare multiple variables at once in C programming. For example,
int student_id, marks;
• Input through scanf() and printing through printf() of Integer variable will be
done by %d.
• The size of int is usually 2 bytes (32 bit OS). And, it can take 2^32 distinct
states from -2147483648 to 2147483647
33
Program to print different variables.
#include<stdio.h>
main()
{ int a,b,c,d,e;
a=5;
b=8.2;
c=5.3; Output:
d='A'; a=5
e= 5.3 +8.2; b=8.2
printf("\n a=%d",a); c=5.3
printf("\n b=%d",b); d=65
printf("\n c=%d",c); e=13
printf("\n d=%d",d); b+c=13
printf("\n e=%d",e);
printf(“ \n b+c=%d”,b+c);
return 0;
}
34
FLOATING POINT DATA TYPE:
• Floating point data type consists of 2 types. They are,
float
double
1. FLOAT:
• Float data type allows a variable to store decimal values.
• Storage size of float data type is 4. This also varies depend upon the processor in the CPU as “int” data type.
• We can use up-to 6 digits after decimal using float data type.
• For example, 10.456789 can be stored in a variable using float data type.
35
FLOATING POINT DATA TYPE:
• Floats are relatively easy to use but problems tend to occur when performing division
In general:
An int divided by an int returns an int.
An int divided by a float returns a float.
A float divided by an int returns a float.
A float divided by a float returns a float.
Declaration:
float f=3.4;
36
2. DOUBLE:
• Double data type is also same as float data type which allows up-to 15 digits after decimal.
• The range for double datatype is from 1E–37 to 1E+37.
• Declaration of double variable:
double price;
• The size of float (single precision float data type) is 4 bytes. and the size of double (double
precision float data type) is 8 bytes.
• Input using scanf() and printing using printf() of float data type will be done by %f.
37
int and float data types
#include<stdio.h>
main( )
{ Output:
float x=8.0; a=8.000000
float y= 8.00 + 7.00; b=15.000000
printf(“x=%f “,x); Here output is till 6 digit after decimal.
printf(“y=%f “,y); If you want to print till 3 digit the write %.3f
return 0; inplace of %f.
} Changing the statement:
printf(“x=%.3f" ,x);
printf(“y=%.3f" ,y);
Output:
a=8.000
b=15.000
38
• http://www.haghish.com/statistics/stata-blog/stata programming/ascii_characters.php
• https://www.programiz.com/c-programming/c-data-types
• https://www.w3schools.in/c-tutorial/format-specifiers/
39