In the last chapters, we have looked into some of the import terms in C programming and setting up the environment for writing C programs. In this section, we will be looking into how to write basic C programs like printing texts on the screen. The basics in C programming includes understanding the basic structure of the program and the concepts of operators. The C programming basics that I will be showing here consists examples that you can try executing it yourself in your environment.
Hello World Program
Let us look into a simple program. Type the following lines using your text editor and save it with the extension .c. All the C programs have the extension .c
Let us name this program HelloWorld.c
/* My Hello World Program */
What does the above program mean?
The preprocessor command #include instructs the C compiler to include stdio.h file before compiling the program.
The second line /* My Hello World Program */ is called a comment. Comments are ignored by the compiler and are used by programmers to add comments.
void main() is called the main function of the program. You will understand it more as you begin writing more and more codes.
printf(“…”); it is a function that instructs the compiler to print the text “Hello World”.
Semicolon ‘;’ indicates the end of a single logical entity and is called a terminator.
Compile and Execute the Program
After you are done saving your program in your desired directory, it is time for compiling and executing it. To do it,
Using the Command Prompt go to the directory where you saved the program.
Type gcc HelloWorld.c and press Enter to compile the code.
If there are errors in your program the Command Prompt would show the errors else take you to the new line and generate an executable file.
To execute type a.out and press Enter.
You have looked into how really a C programs look like. And it is time for understanding it furthermore. Therefore, let us look into some more terms.
1. Tokens: Tokens in C programming language is either a keyword, an identifier, a constant, a string literal or a symbol.
2. Variables: These are the name given to a storage area that can be used by the program. In C programming we cannot assign any storage without first declaring it as a variable.
3. Identifiers: It can be defined as a name or identity is given to a variable, a function or any user-defined items.
4. Keywords: These are reserved words in the C compiler and these name cannot be used for variables or functions. Some of the important keywords are:
Note: 1. Identifiers begin with an alphabet and can contain numbers and underscore _ characters.
There are 4 basic types of constants.
They are integer constants, floating point constants, character constants and string constants.
Integer Constants: It is an integer valued number, written in different number systems.
A decimal constant consists of 0, 1… 9.
An octal constant consists of 0, 1, …, 7.
A Hexadecimal constant of 0, 1… 9, A, B, C, D, E, F.
Floating-point Constant: A floating-point constant has an integer part, a decimal point, a fractional point, and an exponential part.
Character Constants: These are enclosed in single quotes.
We now understand some of the most important terms of C programming. Let us write the program to check whether the number is even or not. (Refer Example 1 to understand the program’s algorithm).
/*to check a number is even or not by bhutanio.com*/
*this is called a multi-line comment
* int defines the data type of the variable number
printf("Enter the number");
//this is a single-line comment
//scanf function is used to GET an input
printf("\nThe number is even");
printf("\nThe number is odd.");
Operators in C Programming
In mathematics, we use different operators to do calculations like “+” operator to find the sum and “-” operator for difference of two operands. In computer science, there are such operators that are responsible for manipulating data by performing specific mathematical and logical operators. In C programming there are several types of operators. Let us look into them one by one.
These operators are the operators that perform mathematical calculations like addition and subtraction. These operators and their descriptions are shown in the table given below (assume variable A and B):
Increment and decrement operators mentioned in the arithmetic operators are also known as unary operators.
Note: We can use ++x in place of x++ and –-x in place of x—but there are slight variations. Let us discuss this in later parts.
Because we know how to write simple C programs, let us try another program that performs various arithmetic operations viz. addition, subtraction, division, and multiplication for two integer variable A and B given by the user.
//arithmetic.c by bhutanio.com
int A, B;
int sum, diff, pro, divi;
sum = A+B;
diff = A-B;
pro = A*B;
divi = A/B;
printf("A+B = %d\n", sum);
printf("A-B = %d\n", diff);
printf("A*B = %d\n", pro);
printf("A/B = %d\n", divi);
Note: The above code is not efficient enough. Guess why?
These operators return Boolean values true or false / 1 or 0. Some of these operators are shown in the table given below:
These are also called the Boolean operators which take the input in the form of true or false and results in the output in true or false value. The table given below shows the list of logical operators and their description.
These operators are used for assigning a value of the expression to another identifier. =, +=, -=, *=, /= and %= are assignment operators.
a = b+c; results in storing the value of b+c in a
a+=b; results in increasing the value of a by b
To understand this concept, one would require some knowledge about the logic gates concepts. These are the operators that deal with operations at the bit level, bit by bit. The following is the list of bit operators supported by C programming language.
Example 4: Write a program to perform bitwise AND, OR and XOR operator where a = 20, b = 10 and assign each value after the operation to variable c.
//bitwise.c by bhutanio.com
unsigned int a = 20;
//binary equivalent of 20 = 10100
unsigned int b = 10;
//binary equivalent of 10 = 01010
int c = 0;
/*implenting AND */
c = a & b;
//c = 00000 (dec = 0)
printf("\nThe value of c after AND Operator = %d", c);
/*implenting OR */
c = a | b;
//c = 11110 (dec = 30)
printf("\nThe value of c after OR Operator= %d", c);
/*implenting XOR */
c = a ^ b;
//c = 11110 (dec = 30)
printf("\nThe value of c after XOR Operator= %d", c);
Here we have learned the basic structure of a C program and some of the important operators.This much is enough to understand the C programming basics. In the next chapter, we will be studying the control statements. Find the next chapter Here:
Kuzu Zangpo la! I am Sonam Dargay. I am a full-time software developer. Apart from 9-5 office works, I am a tech enthusiast, blogger, and dreamer.
I graduated from the College of Science and Technology(CST), affiliated with the Royal University of Bhutan.