1 of 39

C-Programming Language

History, Types, Variables and Constants

2 of 39

Brief History

  • C evolved from two previous languages, BCPL and B.
  • BCPL was developed in 1967 by Martin Richards.
  • B, in 1970 by Ken Thompson.
  • Both BCPL and B were β€œtypeless” languages.
  • C, in 1972 by Dennis Ritchie at Bell Labs.
  • C is widely used with UNIX operating system.

2

10/31/2022

3 of 39

History …

  • Many versions of the language C was created by many people.
  • This created problems with portability.
  • In 1999, ANSI (American National Standards Committee) standardized the language and is called ANSI-C.

3

10/31/2022

4 of 39

C program

  • C program is a set of functions.
  • Every C program should have a function with name main( )
  • Execution of the program starts with main( )
  • Some functions to do common tasks comes along with the C language. These functions are kept in libraries.
    • For e.g. the functions to do input and output are in the library called stdio.h

4

10/31/2022

5 of 39

C Standard Library

  • Libraries that comes along with C are called standard libraries.
  • So, you need to learn C language and also how to use the standard libraries.

5

10/31/2022

6 of 39

A Simple C Program

/* This program Prints Hello word */

#include <stdio.h>

main()

{

printf(β€œHello.\n”);

} /* End of main */

6

10/31/2022

7 of 39

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 of 39

8

10/31/2022

  • Complier ignores comments
  • #include line must not end with semicolon
  • Every statement is terminated by semicolon

Output of the program

Area = 24

Perimeter = 20

cc test.c compile program test.c

a.out execute the program

9 of 39

Variables and Constants

  • Basic data objects manipulated in a program.
  • Variable should have a name (identifier) and a value.
  • Names are made up of letters and digits; the first character must be a letter.
  • E.g.: total, n, sum1, sum2
  • β€œ_” (underscore) is also a letter.
  • E.g.: val_1 is a valid name.
  • But don’t begin variable names with underscore, since library functions often uses such names.

9

10/31/2022

10 of 39

Names (Identifiers) …

  • Upper case and lower case letters are distinct. That is x and X are two different letters.
  • There are reserve words in the C language, like if, else, int, float, etc., which should not be used as variable names.

10

10/31/2022

11 of 39

Names …

  • Invalid names :
    • $total
    • 1st
    • no.
    • case
  • Are the following valid?
    • _total
    • end

11

10/31/2022

12 of 39

Constants

  • A data item whose value does not change.
  • Constants also can have names following the same rules as that for variable names.
  • Simple numbers like 123, 0.24 are of course constants (which are without name).

    • More about constants follows later …

12

10/31/2022

13 of 39

Data Types

  • There are various categories of data items, like integers, real numbers, character strings, etc.
  • Each category is called a type.
  • There are inbuilt types (basic types) and user defined types.

13

10/31/2022

14 of 39

Basic data types

  • character (char) 👺 single character

  • integer (int) 👺 an integer number

  • real (float) 👺 a real valued number

(double) 👺 a real valued number with higher precision.

14

10/31/2022

15 of 39

Declarations

  • An identifier which you want to use should be declared first.
  • A declaration specifies type and identifiers name.
  • Optionally a declaration for a variable can contain its initial value also.

15

10/31/2022

16 of 39

Declarations …

  • int count; /* declares that count is a variable which can hold integer values */
  • char s, c; /* s and c are variables which can hold a single character each */
  • float sum_1 = 0.5;
  • double d, total, sum;

    • Declaration is a statement and every statement in C should end with a semicolon ;

16

10/31/2022

17 of 39

Declarations …

  • Syntax for a declaration is:

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;

  • All the variables that you are using should be declared first.

17

10/31/2022

18 of 39

Declarations …

  • Declaration specifies type and also the size (the number of bits that should be allocated to store the declared variable).
  • These sizes are machine (Hardware) dependant.
  • Size determines the range of values you can use.

18

10/31/2022

19 of 39

Declarations …

  • For example, if size of char is 1 byte (8 bits) then there can be only 28 different characters that you can use.
  • Sizes

char 👺 1 byte

int 👺 4 bytes

float 👺 normally 4 bytes

double👺 double the size of float

19

10/31/2022

20 of 39

Declarations …

  • For some types, you can increase (or decrease) the size relatively by adding some optional qualifiers to the type.
  • For int 👺 short int

👺 long int

  • For double 👺 long double

20

10/31/2022

21 of 39

How to find the size?

  • sizeof is an operator which you can use.

E.g:

-------------------------

#include<stdio.h>

main()

{

int s;

s = sizeof( int );

printf(β€œThe size of an int is: %d”, s);

}

21

10/31/2022

22 of 39

sizeof

  • sizeof is an unary operator.
  • The operand is either an expression, which is not evaluated, or a paranthesized type name.

  • int i,j;

j = sizeof i; /* but, j = sizeof int; is wrong */

22

10/31/2022

23 of 39

Declarations …

  • Some other qualifiers can be used to specify the range …

  • E.g: int 👺 signed int

unsigned int

  • char can also be signed or unsigned, but this discussion is deferred.

23

10/31/2022

24 of 39

Declarations…

  • Some examples:
      • unsigned short int sum;

if short int is of size 1 byte, what is the range of values that sum can hold ?

      • long double avg;

24

10/31/2022

25 of 39

Modifiers in C

  • The amount of memory space to be allocated for a variable is derived by modifiers.
  • Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space allocated to a variable.
  • For example, storage space for int data type is 4 byte for 32 bit processor.Β We can increase the range by using long int which is 8 byte.Β We can decrease the range by using short int which is 2 byte
  • There are 5 modifiers available in C language. They are,
    • short
    • long
    • signed
    • unsigned
    • long long

25

10/31/2022

26 of 39

Constants

  • Constants also has type.
  • 1234 is an int
  • A long int constant is written with a terminal l or L. Eg: 123456789L
  • An integer too big to fit into an int will also be taken as long.
  • Unsigned constants are written with a terminal u or U, and the suffix ul or UL indicates unsigned long.

26

10/31/2022

27 of 39

Constants …

  • Floating point constants contain a decimal point (123.4) or an exponent (1e-2) or both; and their default type is double, unless suffixed.
  • The suffixes f or F indicate a float constant;

l or L indicate a long double.

27

10/31/2022

28 of 39

Constants …

  • An integer can be specified in octal or hexadecimal instead of decimal.
  • A leading 0 (zero) on an integer constant means octal; a leading 0x or 0X means hexadecimal.
  • Eg: decimal 31 = 037 (octal)

= 0x1f = 0X1f (hexa)

  • What is 0XFUL in decimal?

28

10/31/2022

29 of 39

Constants …

  • A character constant is written within a single quotes, like β€˜x’
  • Actually a character is stored as an integer.
  • The integer value of a character is often called as its ASCII value. (In ASCII character set each character is given an integer value.)
  • β€˜0’ πŸ‘ͺ character zero πŸ‘ͺ ASCII value is 48
  • β€˜0’ is unrelated to the numeric value 0

29

10/31/2022

30 of 39

Constants

  • Some characters can not be written normally, hence they have special codes called escape sequences.
  • β€˜\n’ πŸ‘ͺ newline; β€˜\a’ πŸ‘ͺ bell; β€˜\\’ πŸ‘ͺ backslash

β€˜\t’ πŸ‘ͺ tab; β€˜\0’ πŸ‘ͺ null

β€˜\’’ πŸ‘ͺ single quote; β€˜\”’ πŸ‘ͺ double quote;

  • There are some more escape characters which you should read in the book as an exercise.

30

10/31/2022

31 of 39

Constants … (Named constants)

  • The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed.
  • Eg: const int sum = 100;

      • Some other way is through #define directive whose discussion is deferred.

31

10/31/2022

32 of 39

Input and Output

33 of 39

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 */

34 of 39

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 ?

35 of 39

Why β€˜&’ in scanf ?

  • Every variable should have a location in the memory.
  • If i is variable’s name then &i is its address in the memory.
  • scanf should be given the addresses of the locations where it should store the read values.

36 of 39

scanf

  • Different values should be separated with a space or newline.
    • More about this is deferred.

37 of 39

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

$

38 of 39

printf

char ch = β€˜a’;

printf(β€œ%d”, ch);

What will be the output?

39 of 39

How to read or write a float?

%f πŸ‘ͺ float

float num;

scanf(β€œ%f ”, &num);

printf(β€œ%f ”, num);