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