C-Programming Language
History, Types, Variables and Constants
Brief History
2
10/31/2022
History …
3
10/31/2022
C program
4
10/31/2022
C Standard Library
5
10/31/2022
A Simple C Program
/* This program Prints Hello word */
#include <stdio.h>
main()
{
printf(“Hello.\n”);
} /* End of main */
6
10/31/2022
7
10/31/2022
/* This program finds the area & perimeter
of a rectangle */
#include <stdio.h>
main()
{
int l, b, area, perimeter;
b = 4;
l = 6;
area = l*b;
perimeter = 2*(l+b);
printf(“Area = %d \n”, area);
printf (“Perimeter = %d \n”, perimeter);
} /* End of main */
test.c
8
10/31/2022
Output of the program
Area = 24
Perimeter = 20
cc test.c compile program test.c
a.out execute the program
Variables and Constants
9
10/31/2022
Names (Identifiers) …
10
10/31/2022
Names …
11
10/31/2022
Constants
12
10/31/2022
Data Types
13
10/31/2022
Basic data types
(double) 🡺 a real valued number with higher precision.
14
10/31/2022
Declarations
15
10/31/2022
Declarations …
16
10/31/2022
Declarations …
type var_1, var_2, …,var_n;
E.g: int i, j,k;
Or, write three separate lines like: int i;
int j;
int k;
17
10/31/2022
Declarations …
18
10/31/2022
Declarations …
char 🡺 1 byte
int 🡺 4 bytes
float 🡺 normally 4 bytes
double🡺 double the size of float
19
10/31/2022
Declarations …
🡺 long int
20
10/31/2022
How to find the size?
E.g:
-------------------------
#include<stdio.h>
main()
{
int s;
s = sizeof( int );
printf(“The size of an int is: %d”, s);
}
21
10/31/2022
sizeof
j = sizeof i; /* but, j = sizeof int; is wrong */
22
10/31/2022
Declarations …
unsigned int
23
10/31/2022
Declarations…
if short int is of size 1 byte, what is the range of values that sum can hold ?
24
10/31/2022
Modifiers in C
25
10/31/2022
Constants
26
10/31/2022
Constants …
l or L indicate a long double.
27
10/31/2022
Constants …
= 0x1f = 0X1f (hexa)
28
10/31/2022
Constants …
29
10/31/2022
Constants
‘\t’ 🡪 tab; ‘\0’ 🡪 null
‘\’’ 🡪 single quote; ‘\”’ 🡪 double quote;
30
10/31/2022
Constants … (Named constants)
31
10/31/2022
Input and Output
int scanf (input)
int i;
char ch;
scanf(“%d”, &i); /* read an integer from stdin into i */
scanf(“%c”, &ch); /* read a char from stdin
into ch */
scanf (formatted input)
/* both can be read in a single scanf */
scanf(“%d %c”, &i, &ch);
Format string
int variable
char variable
Why there is ‘&’ character before i and ch ?
Why ‘&’ in scanf ?
scanf
int printf (formatted output)
int i; char ch;
i = 125;
ch = ‘a’;
printf(“%d %c \n”, i, ch);
printf(“%c\n%d”,ch,i);
Output on the screen
$./a.out
125 a
a
125
$
printf
char ch = ‘a’;
printf(“%d”, ch);
What will be the output?
How to read or write a float?
%f 🡪 float
float num;
scanf(“%f ”, &num);
printf(“%f ”, num);