Character set in C We should use only the following characters in writing a C program. These characters can be combined to create C words. Alphabet: A, B, C, D.. Z, a, b, c, d..z Numeric digits: 0, 1, 2, 3,4,5,6,7,8,9 Special Characters: + * / %? &! = \ [ ] ( ) { } < > @ # ^. _, : ; ~ Words in C We can divide the words in C into three groups: 1. Constants 2. Variables 3. Reserved words or key words. 4. Constants Constant represent fixed values. That means, their value will not change in the program. For example, the numbers used by us will come under constants as their value will not change. Similarly, names of persons will also come under constants. 5, -125, 255 are called Integer constant. 12.5, -3.14159 are called real constants. A, M, S are called Character constants. Rajesh, Software are called string constants. In the preceding example we should note that the character constants should be enclose in single quotes and the string should be enclosed in double quotes. 2. Variables
Variables represent words which store varying (changing) values. For example, Int x; Here, x is the name of a variable whose type is int. It means we can store an integer number in this variable x. Internally, a variable represents a memory location. So, if we store an integer in a variable, then we should understand that it is store in a memory location. If we store an integer number in a variable, then it is called Integer Type variable. If we store a real number in a variable, then it is called Float Type variable. If we store a single character in a variable, then it is called Character Type variable. int empcode; float emp_sal; char gender; In the preceding examples, empcode, emp_sal and gender are the names of the variables. We can use these variables to store values as shown here: empcode = 1001; emp_sal = 7890.50; gender = M ; Please observe that in the empcode variable, we are storing 1001 which is an Integer constant. In emp_sal variable, we are storing 7890.50 which is a Real constant. In gender variable, we are storing M which is Character constant. A string represents a group of characters. Strings are character type array in C. to store a string, we need a characters type array. We will learn about character array in a later chapter. While writing the names of variables, we should start the name with an alphabet. Generally, we should not use any blanks or any special characters in the variable
names, except underscore symbol (_). The main point to remember is that a variable should be declare first in a C program only once. After that it can be used at any time. For example, int num; Here we are declaring the variable num as int type. After declaring the variable, we can use it. For example, now we can store integer number into the variable, as num = 100; It is not possible to use a variable without declaring it. 3. Reserved words Variable names are in our hands. We can give any names we wish. But there are some words which cannot be used as variables. They are used in some special meaning only. They are called reserved words or key words. There are a total of 32 Reserved words in C which are shown here: auto double int struct Break else long switch Case enum register typedef Char extern return union Const float sizeof unsigned Continue for short void Default goto signed volatile Do if static while
C Statements There are two type of statements input statement and output statements. The data given to a program is called input and the result given by the program is called output. To provide data to a program, we need input statement. Similarly, to display the result, we use output statement. We should understand one important point in C. To do any work we need a function in C. A function can be imagined as a set of statements aimed to perform a task. There are various functions provided in the form of header file in C library. See the figure 2 C standard Library Header Files Functions Figure 2: C standard library contains header files and functions. If want to perform any task, we need a function. So, in writing any statements, we would generally function. In input and output statements also, we need a function. The following table gives the list of basic input and output functions which are available in the part of the header file; stdio.h, i.e. standard input output header file. Input Functions Output functions getchar( ) putchar( ) gets( ) puts( ) scanf( ) printf( )
Table 1: Basic Input and Output functions in stdio.h. getchar() function Using getchar() function, we can give a single character as input. getchar() function takes the character and store it into a variable. ch= getchar(); Here, getchar() accepts a single character from the keyboard and stores into the variable ch. Here ch represents a character type variable. We should note that any function name in C end with a pair of simple braces, for example: getch() or putch(). putch() function Using putch() function, we can display a single character as output on the monitor. putch(ch); Here, putch() function, we can taking a variable ch and it sends the content of that variable to the monitor. putchar( A );
Here, putchar() will display the character A directly on the monitor. Program Program 1: Let us write a program to understand how to input a single character from the keyboard and how to display the same character on the monitor. /* To accept and display a single character */ #include<stdio.h> void main() { char ch; ch = getch(); putch(ch); } Let understand this program clearly. The first line represents comment line. /* To accept and display a single character */
Comments describe the aim of a program or any statement(s) in the program. Comment are written inside /* and */. Comments are not executed by the compiler. So they are called non executable statements. They are for increasing the readability (understandability) of the program. Important interview Question Can you nest a comment inside anther comment in C? No, it is not possible to write a comment inside another comment. # include<stdio.h> This represents an instruction to the C compiler to include the header file: stdio.h. Since we want to use getch() and putch() functions in the program, and those functions are available in stdio.h, it is compulsory to include this header file/ then only we can use any of the functions from that header file in our program. void main() Here, main() is the function name from where a C program execution starts. Before this main(), we can see void. It means no value. So, main() function will no return any value. It simply executes and display the result. In every C program, we should write this main() function.
Important Interview Question What is returned by main() function, by default? main() function or any other function in C by default returns an integer type value. By writing void before main() function, we are indicating that main() function is not returning any value. If we do not write void before main(), then it returns an integer value. After main() function, we should write { which represent the beginning of the main() function and } which represent ending of the function. The statements inside this function are executed by the compiler. char ch; Here, ch is a variable declare as char type. So, it is possible to store a single character in ch. ch = getch(); Here, getch() function waits for input of a single character. When we enter a character from the keyboard, it reads it and then stores it into the variable ch. Storage is done by the symbol = which is called assignment operator. putchar(ch);
Here, putchar() function is displaying the content of ch on the monitor. Important Interview Question What is the use of the main() function in C? main() function is the starting point of execution of a C program. Every C program should have a main() function. gets() function This function is useful to accept a string from the keyboard and store it into a variable. A string represents a group of characters. gets(str); In this statement, gets() function will accept a string from the keyboard and stores into the variable str. puts() function this function displays a string on the monitor. puts(str)