Computer Fundamentals with C and Unix
(Data types)
Lecture 02: Dec. 17, 2022 Prof. K.R. Chowdhary : Professor of CS

Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.

2.1 Introduction

Through the program we do computations on data, which may be of various types depending on the requirements of the application. For ordinary arithmetic we need integers, for real life applications there are real numbers, called float, e.g., Rs. 30.5/kg onion, and the names are represented as character strings, and individual characters are are char types. The following example demonstrates this.


PIC

Figure 2.1: Edited program: Basic data types


The above programs gives many things to learn:

2.2 Compiling and Running the Program

The program types.c we discussed above is a simple program, where statements of C are executed in sequential order and each one is executed once only. There is no loop to repeat any statement either there is any conditional statement to select a statement for execution.


PIC

Figure 2.2: Running of program: Basic data types


The data declaration part is for restricting the size and type of data that can be stored in the variables. This binds the type with the variables, hence in the future statements in the program only that type of data can be assigned to that variable, else there will be error.

The errors in the program are checked during the compiling. The source program is types.c and the compiled program is a.out. We note that size of source program is only 328 bytes, while the size of compiled and linked program a.out is 16104 bytes. The latter is far bigger, because the for every statement in the source language (C here) there ar hundred of machine language statements. Imagine, if we are asked to write a program in machine language, it will be too tedious and time consuming process. So, let us thank to the people who wrote compilers, which have simplified the job for us, cheers to them !!


PIC

Figure 2.3: Listing the files


2.3 Some file Commands

Remember that, in the informal talk, we are learning Linux1 also, but don’t be afraid, it will not be difficult. The Unix is OS for programmers, and there is lot to learn from Unix for CS.

The Unix has directory structure, i.e., tree of folders and files, with a root. Following are some commands:

Some of the common commands for file operations are:


Table 2.1: Basic file related Linux commands



S.no.

Command

Description




1.

$ cat hello.c

displays the contents of hello.c file

2.

$ cp hello.c ok.c

copies first file into second

3.

$ mv hello.c ok.c

renames the first as second file

4.

$ more hello.c

displays hello.c page wise

5.

$ head hello.c

displays top 10 lines of hello.c

6.

$ tail hello.c

displays last 10 lines of hello.c

7.

$ wc hello.c

displays number of lines, words and characters in hello.c

8.

$ rm filename

removes the file, i.e., deletes it from the storage





2.4 Other data types

There are more data types in C:


PIC

Figure 2.4: More variables declaration


Smallest size memory you can allocate is one byte, and memory is always 8, 16, 32, 64 bytes sizes and no in between, in all the modern computers.

Different types of variables consume different amount of space in memory (i.e., Random Access Memory (RAM)) of the computer.

Short: 1 byte

Character: 1 byte

int : 2 bytes

long : 4 bytes

double: 8 bytes

long double: 16 bytes

Examples of bytes are: 0000 0000, 0000 0001, ..., 1111 1111. Note that each one is of fixed length 8 bits.

Though we can do computations using hexadecimal, octal, and binary as well as print the in their formats, but there are no data types like binary, octal and hexadecimal.


PIC

Figure 2.5: Running the more variables program



PIC

Figure 2.6: Sizes of data types



PIC

Figure 2.7: Running Sizes program


2.5 Syntax and Semantics

The syntax is name of grammatical rules of writing C program statements. Like, correct syntax of a sentence is “Student is in School”, and not “School in is student”. Similarly, correct syntax are:

basic = 10000; da = 5000; hra = 5500;
pay = basic + da + hra;
printf(“%f\n”, pay);

and not

10000=basic; 5000=da; ....
printf(pay);

Semantic is the meaning associated with a statement, say:

pay = basic + da + hra;

means pick up the data corresponding to variables basic, da, hra from the respective memory locations, bring them into cpu, do addition, and store the result in the memory location designated by variable “pay”.

The compiler performs mostly the syntax checking, while semantics is checked (mostly) during the running of the program.