Getting started with c programming (step-by-step) explanation


In the last session, we learned about what is C now; we will be learning some basics of c programming 
so get your notebook and pen, and let's get started.

This content is also available in audio file

Communicating with a computer involves speaking the computer's language, which rules out English as a language of communication with the computer. However, there is an immediate difference between learning English and learning the c language. The best learning method is to understand the first alphabet, the alphabet combined to form a word, a word combined to form a sentence, and a sentence combined from the paragraph.

                                                               figure 1

The c character set

A character denotes any alphabet, digit, or special symbol used to represent information. Figure 1.2 shows valid variable alphabets, numbers, and special symbols allowed in c.

Alphabets

 

 

Digits

 

Special symbol

A, B, C, ……Y, Z.

A, b, c…….. y, z.

 

0,1,2,3,4,5,6,7,8,9

 

[ ] : ! @ # $ % ^ & * () _ - + = / \ { } : ; “ ‘ <> , . ? /

                                                               figure 1.2

Constant, variable, and keyword

 

The alphabets, digits, and special symbols, when properly combined, form constants, variables, and keywords. Let us understand the meaning of each of them. A constant is an entity that may change. A keyword is a word that carries a special meaning.

 In programming, we typically do many calculations. The result of these calculations is stored in the computer's memory. Like human memory, the computer's memory also consists
of millions of cells. The calculated values are stored in these memory cells. To make the retrieval and usage of these values are stored in the memory cells. To make the retrieval and usage values easy, these memory cells (also called memory location) are given names. Since
the value stored in each location may change, the names given to these to this location are called variable names.

Consider the memory locations shown in figure 1.3. Here three are stored in a memory location, and the name x is given to it. Then we have assigned a new value of 5 to the memory location. 
x. this would overwrite the earlier value three since a memory location can hold only one weight at a time.
                                             figure 1.3
since the location whose name is X can hold the different values of different times, X is known as a variable. An against this, 3 or 5 do not change, hence are known as constants.

The constant is often called literals in the C programming language, whereas the variable is called identifiers.

Now, let us understand the constant and their variable. But, first, let us see what is different types of constants and variables exist in C. 

Types of C constants

C constants can be divided into two major categories:
  1. primary constants
  2. Secondary constants
These constants are further categorized as shown In figure 1.4



Figure 1.6
 
At this stage, we would restrict our discussion to the only primary constants, namely, integer, natural, and character constants. Let us see the details of each of these constants. For constructing these constants. For creating these different types of constants, specific rules have been laid down. These rules are as under:

Rules for creating Integer constants 

  1. A constant integer must have at least one digit.
  2. It must not have a decimal point.
  3. It can be either positive or negative.
  4. If no sign precedes an integer constant, it is assumed to be positive.
  5. No commas or blanks are allowed within an integer constant.
  6. The allowable range for integer constants is -21474836483648 to +2147483647.
The range of an integer constant depends upon the compiler. For compiler like visual studio, GCC, it is -2147483648 to  +21474836, whereas, for compilers like turbo c or turbo c++, the range is -32768 to +32767

Ex:  426
     +782
     -8000
     -7605
    

Rules for constructing real constants

Real constants are often called floating-point constants. The real constants could be written in two forms- Fractional form and exponential form.

Following rules must be observed while constructing real constants expressed in fractional 
forms:

a) A real constant must have at least one digit.
b) It must have a decimal point.
c) It could be either positive or negative.
d) default sign is positive.
e) No commands or blanks are allowed within all real constant.

Ex. +325.34
         426.0
         -32.76
       -48.5792


The exponential form is usually used if the value of the constants is either too small or too large. However, it doesn't restrict us from using the exponent form for other real constants.

In the exponential form, the real constants are represented in two parts. The part appearing
before 'e.' is called Manistisaa, whereas the following to "e" is called the exponent.

Following are rules that must be observed while constructing real constants expressed in exponential form:
(a) The mantissa part and the exponential part should be separated by the letter e or E.
(b) The mantissa part may have a positive or negative sign.
(c) Default sign of mantissa part is positive.
(d) The exponent must have at least one digit, which must be a positive or negative sign.
(e) range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

Ex
+3.2e-5
 4.1e8
-0.2E+3
-3.2e-5

Rules for constructing character constants

(a) A character construct is a single alphabet, a single-digit digit or a single digit, or a single special symbol enclosed within the single inverted command.
 (b) Both the inverted commas should point to the left. For example, 'A' is a valid character constant, whereas 'A' is not.
Ex  'A'
       'I'
       '5'
       '='

Types of C variable

 

A particular type of variable can hold only the same kind of constant—for example, an integer variable can have only an integer constant, a real variable can hold only a real constant, and a character variable can hold only character constant. Therefore, the rules for constructing different types of constants are different. However, for building the variable name of all kinds, the same set of rules applies. These rules are given below.

Rules for construction variable Names

  1. A variable name is any combination f 1 to 30 alphabets, digits, or underscore. Some compilers allow variable names whose length could be 247 characters. Still, it would be safer to stick to the rule of 31 characters. Don't create unnecessarily long variable names as it adds to your typing efforts.
  2. The first character in character in the variable name must be an alphabet or underscore      ( _  ).
  3. No commas or blanks are allowed within a variable name.
  4. No special symbol other than an underscore ( as in grass_sal ) can be used in a variable name.
Since the maximum allowed length of a variable name is 31 characters, many variable names can be constructed using meaningful variable names built using the rules mentioned above.

C keywords

Keywords are the words whose meaning had already been explained to the c compiler. There are only 32 keywords available in c programming. The below table gives a list of these keywords for your reference. A detailed explanation will be taken up in later chapters, whereas their use is relevant.

auto

double

int

struct

break

else

long

switch

Case

enum

register

typedef

char

extern

return

union

const

float

short

unsigned

continue

for

signed

void

default

goto

sizeof

volatile

do

if

static

while




The keywords cannot be used as a variable name because if we do so, we are trying to assign a new meaning to the keyword, which is not allowed. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it is safer not to mix up the variable names and keywords. 

In the next post, we will be learning the first c program.


Comments

Popular posts from this blog

22620 Network and Information Security NIS solved lab manual pdf

Object Oriented Programming in C++(OOP) 22316 MCQ with answer pdf I-scheme msbte