0% found this document useful (0 votes)
30 views

Lesson 2-Character Set and Keywords

The document discusses character sets and keywords in C programming language. It provides details about valid characters, constants, variables, keywords and escape sequences in C. It also explains the use of variables in C including declaration, initialization, assigning values, and getting input from the user. Some key points are: 1) Characters in C include uppercase letters, lowercase letters, digits, and special symbols. Keywords are predefined words with specific meanings. 2) Variables in C must be declared before use, specifying their name, type, and optionally initialized with a value. Values can be assigned later using assignment statements. 3) The scanf function is used to get input from the user and store it in a variable

Uploaded by

janlixalmendral
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Lesson 2-Character Set and Keywords

The document discusses character sets and keywords in C programming language. It provides details about valid characters, constants, variables, keywords and escape sequences in C. It also explains the use of variables in C including declaration, initialization, assigning values, and getting input from the user. Some key points are: 1) Characters in C include uppercase letters, lowercase letters, digits, and special symbols. Keywords are predefined words with specific meanings. 2) Variables in C must be declared before use, specifying their name, type, and optionally initialized with a value. Values can be assigned later using assignment statements. 3) The scanf function is used to get input from the user and store it in a variable

Uploaded by

janlixalmendral
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Character set and Keywords

A character denotes any alphabet, digit or special


symbol used to represent information.
Valid alphabets, numbers and special symbols allowed in
C are:

Uppercase characters A to Z
Lowercase characters a to z
Digits 0 to 9
Mathematical symbol +, - , = and some more special characters
are used in C for building the program elements, like identifiers,
constants, variable, operators, expressions, etc.
The alphabets, numbers and special symbols when properly
combined form constants, variables and keywords.

C also uses some combination called escape sequence which we


have introduced in the previous discussion.

Certain words in C have definite meaning. These are called


keywords. They can be used only for those specified purpose. The
words have to be spelt exactly as specified. Keywords are all in the
lowercase. The 32 keywords of C language are:
Use of Variables

Variable is a data name which is used to store some data value


or symbolic names for storing program computations and
results. The value of the variable can be change during the
execution.

The rule for naming the variables is same as the naming


identifier. Before used in the program it must be declared.

Declaration of variables specifies its name, data types and


range of the value that variables can store depends upon its
data types.
Example 1

Let us study another C program.

Create the program to print 5 is the value.

#include<stdio.h>
main()
{
int x;
x=5;
printf(“%d is the value”, x);
}

We already know the use of the first lines. The first line includes the library
function into our program. The second line declares the main function of our
program. With the curly braces in the third line the body of the main () function
starts. The fourth line int x; is a declaration of variable x. In C, a variable has to
be declared before it can be used.
The variable is always declared by mentioning its type. Here x
is an integer type of a variable. It means its value can only be
a whole number. The next line assigns value 5 to the variable
x.

(try to change this line to some fraction like 5.3, you will find
that the program accepts but prints only the integer part so
the output is 5 is the value not 5.3.

Now let’s focus on the printf statement which is already used


by us.
The output will be 5 is the value.
In the control string, we have introduced %d before the
text. After control string ends with a comma, the name
of the variable x is mentioned.

The printf statement inserts the value of the variable


where %d is written within the printf control string.

This facilitates to print the value of a variable


interspersed with messages. The following printf
statement, if inserted in the above example, would print
x=5.
printf(“x=%d”,x);

The printing of a variable needs the format to be used fitting


of the type of the original variable. Here %d stands for a
format of an integer value. Any number of variables can be
printed out by a single prinft statement.

For all variables a format specification should be present in


the control statement of the printf command and all the
variables should be listed with a comma in between,
e.g.,printf(“%d %f %d”,x,y,z); where x and z are integer
variables and y is a real variable. We will use other format as
a necessary.

The list of format specification is given in the next section.


Declaration of a Variable

The programmer can choose any name to a variable within some


limits. The names are formally called identifiers.

Identifiers consist of letters, numbers and underscores. Name


must begin with a letter. Theoretically, names can be as long as the
programmer wants, but extremely long names are tedious to type
and difficult to keep track of.

Some compilers may have a limit on the length of the variable


identifiers. Any extra characters may be discarded by the
compiler. So two long names differing only in the extreme right
character may be taken as same by the compiler.
The use of upper case and lower case character stands for
different names in C. The variable names length, LENGTH
or lenGTH are all distinct.

Conventionally, programmers write normal variable and


function names in lowercase and constant name in uppercase.

Another restriction on names of any identifier (name of


variables, functions etc.) is that you should not use
keywords, such as int, float, etc.

The keywords form the part of syntax of the language and


they have special interpretation of their functions by the
compiler.
Let’s go back to the program Example 1
After the start bracket {, there is a statement:
int x;
As already mentioned this is called variable declaration
statement. Every variable used in a program must be
declared before it is used. While declaring variable in C we
have to first specify its type.
The following types are available in C.
Data Types
Declaration examples are:

int i,j,k;

short int s;

unsigned int a,b,c;

double length, width;


Sometimes it is convenient to replace a standard Data type keyword
with an identifier of your choice.

For example, int is a standard data type, but while declaring age of
students you want to use the word age as a type of variable (which is
actually an integer type) rather than using the keyword int.

C allows a programmer to redefine the standard data types, with a


word of her choice.

This is done by using typedef statement. The typedef statement


structure is discussed further.

typedef type new definition;


e.g., typedef in age;
Example

Demonstrate the use of typedef in calculating the area of a rectangle.

/*
This program demonstrates the simple use of typedef statement.
It calculates the area of a rectangle. It is uses typedef to redefine the
word side as int specifier.
*/

#include<stdio.h>
main()
{
typedef int side;
side x=2,y=3;
printf(“the area of the rectangle is %d units\n”, x*y);
}
Assigning Value to a Variable

A statement x=5; sets the value 5 to the variable x. This is


called an assignment statement.

When a variable is declared the C compiler does not


automatically assign any value to it. The variable value
remains undefined till is assigned a value by some statement.

An assignment statement starts with the variable name on


the left side followed by an equal sign. The value to be
assigned is placed on the right. The value may be directly
placed or an expression may be used on the right side of the
equal sign. The expression is evaluated and the result is
assigned to the variable. To evaluate, the values of all
variables used in the expression must already be set.
The following statements are perfectly ok.

int x,y;
x=2;
y=x+3;

x has a value of 2 at this stage, so y gets a value of 3+2


that is 5.
However the following will be invalid.

int x,y,i;
x=2;
i=x+y;

y value is unknown as this stage so it cannot be used to


assign the value of i.
A group of variables may be assigned together if the
values (or expression) to be assigned happen to be same.
The following is valid.

int x, y, i;
x=y=i=2;

x value is 2 x=2;
y value is 2 y=2;
i value is also 2 i=2;
The following is also valid

int a, b, c;
c=5;
a=b= ((c=c+4) +4));

c will be equal to 5+4(c=c+4), a and b will be equal to


5+4+4 , a and b will be assigned a value of 13 and c will
be 9.
Initialization of Variables

The declaration and assignment can be clubbed together


as follows:

int x=2;

The declaration and assignment, in one statement, help


initialization of a variable right at the beginning of the
program.
Getting a Variable Value from the user

Let’s us write another c program.

This is a program to read a temperature in Celsius and


print its value in Fahrenheit. The relationship between
Celsius and Fahrenheit value is given by Fahrenheit value =
(Celsius value)*9/5+32.0.
#include<stdio.h>
int main()
{
float fahr,celsius;
printf("please type in a celsius value:\n");
scanf("%f", &celsius);

fahr=celsius *9/5 +32.0;


printf("%fcelsuis = %f Fahrenheit \n", celsius, fahr);

}
In the example above , we have used a variable type of float. To
print the variable value we have used a format %f which indicates
that variable is to be printed as float type.

A new statement we get in this example is scanf. The purpose of


this is to read an input value from the keyboard. It contains a
control string inside the parenthesis which gives the type of the
variable to be read.

The statement scanf(“%f”, &Celsius); means we intend to read a


variable called Celsius which is of float type. The name of the
variable to be input by scanf, must be prefixed with an ampersand
sign(&).

This requirement is because scanf is a library function designed to


work with the address of a variable. The address of the variable
Celsius is represent here as &Celsius. There will be more cases
where we will be using the address operator &.
The user of the program, while the program is being
executed, is supposed to type the value on the keyboard
and then press the return key.

The scanf statement waits indefinitely till the return key is


pressed by the user. Scanf cannot give any message what
it is waiting for.

A clever use of printf is to print an appropriate message,


before the scanf statement is issued. This will make the
user aware that a value is to be type in.
Another Example
// Create a program to subtract 2 Numbers.

#include<stdio.h>
int main()
{
// variable declaration
int x,y,answer;

// input
printf("\n Enter 1st number: “);
OUTPUT
scanf("%d",&x);

printf("\n Enter 2nd number:");


scanf("%d",&y);

//process
answer=x-y;

//output
printf("\n The answer is= %d\n", +answer);
}

You might also like