C programming language
Getting started with C programming language
Hey folks, it has been a while since our last post. Sorry for the long delay.
But as they say, it is never too late to start something new. So, we will take a step forward towards learning a programming language. It will help you create unique products and automate some tasks. We will be learning many languages during this journey, but chosen C to start with, as it has changed the computer industry since its inception.
Introduction
C is a general purpose, imperative programming language, developed in 1972 by Dennis Ritchie at Bell Labs(now AT & T Bell Labs) as an outgrowth of an earlier language called B, which was also developed at Bell Labs. And after this there was no looking back, as more and more professional switched over to C. Today, there are compilers available under every conceivable environment from DOS to Windows to Unix, and on all sizes of computers from micros to mainframes. The most important features of C includes:
- Power
- Performance
- Portability
- Flexibility
Variables and Constants in C
Simply put, a variable is a tool to reserve space in computer's memory. The reserved space is given a name, which we call a variable name. Constants are stored in the space reserved by the variable may vary from time to time, hence the name variable. The name given to a location, however, remains unchanged.
Thus, in the statement,
i = 5
5 is a constant which is being stored in a location which has been given a name i. So, i is a variable just as a, x, note or str are. They all designate memory locations that will hold different values assigned to these variables from time to time. Depending on the purpose for which you want to utilize memory, C allows you to decide how much memory to allocate to a variable. Accordingly memory comes in three convenient versions: char, int and float, which occupy 1,2 and 4 locations ( bytes ) respectively. A char can store values in the range -128 to +127, whereas an int can store values in the range -32768 to +32767. When do we use float? When we want to store numbers like 3.1415, or 0.0005672 in
memory. A float can store values ranging from -3.4e+38to +3.4e+38.
Now that is certainly a very impressive range!
Now that is certainly a very impressive range!
The memory occupied by each data type can be found out using an operator called sizeof. For example, sizeof ( int ) would yield 2. Similarly, sizeof ( float ) would yield 4.
The char and int can also be expressed in hexadecimal and octal notations. For example, 16 can be expressed in hex as 0x10, and as 020 in octal. Similarly, 22 can be expressed in hex as 0x16, and as 026 in octal. A hex number is always preceded by Ox or OX whereas an octal is preceded by 0 ( zero, not o or O ). C doesn't accept floats in hex or octal.
With constants out of the way, let us now take a look at the variables.
Certain rules have been framed to create variable names. A variable name can be any combination of alphabets, digits and an underscore, with a proviso that the first character is an alphabet. The length of the variable is a compiler dependent feature. Turbo C accepts a variable name upto 32 characters.
For example, +, -, *, / are binary operators since they operate on two operands that they add, subtract, multiply or divide. Except for % the other operators are as usual. % is read as 'modulus' operator, and it returns the remainder on dividing an int with another. As against this, a / returns the quotient. While evaluating an expression, C gives priorities to some operators above the others. A unary minus has the highest priority, followed by arithmetic operators and lastly the assignment operator. Within arithmetic operators, *, / and % enjoy a higher priority than + and -.6
The general form of printf() looks like this.
printf ("Format string", list of variables);
And here are a few examples...
printf ("%c %d %f, name, age, sal);
printf ("name = %c age = %d salary = %f, name, age, sal);
printf ("name = %c\nage = %d\nsalary = %f, name, age, sal);
Assuming the value of name as 'A', that of age as 23, and that of sal as 1500.00, the output of the above printf( )s would be
A 23 1500.000000
name = A age = 23 salary = 1500.000000
name=A
age = 23
salary = 1500.000000
The first printf() prints the values of the variables name, age and sal. As against this, the second would print messages against each value displayed. In the third printf(), the \n is used to send the cursor to the next line, hence the three values are printed on three different lines.
Sometimes the list of variables may be dropped, as in, printf ("Enter values of a and b");
In this case everything enclosed within the pair of double quotes would be printed as it is on the screen.
Having imbibed those details, now let us switch over to scanf().
scanf( ) is once again a standard library function. It is used to receive values of variables from the keyboard. For example, the following scanf() would let you supply the values of variables a and b.
scanf ("%d%f,&a,&b);
The general form of scanf() is almost similar to printf( ), except for
two important differences:
The char and int can also be expressed in hexadecimal and octal notations. For example, 16 can be expressed in hex as 0x10, and as 020 in octal. Similarly, 22 can be expressed in hex as 0x16, and as 026 in octal. A hex number is always preceded by Ox or OX whereas an octal is preceded by 0 ( zero, not o or O ). C doesn't accept floats in hex or octal.
With constants out of the way, let us now take a look at the variables.
Certain rules have been framed to create variable names. A variable name can be any combination of alphabets, digits and an underscore, with a proviso that the first character is an alphabet. The length of the variable is a compiler dependent feature. Turbo C accepts a variable name upto 32 characters.
Operations on Data
C programs are concise partly because of the large number of operators available with it. There are as many as 45 operators available in C. For starters we will concentrate only on arithmetic and assignment operators. Operators are classified as unary, binary or ternary depending on the number of operands they operate upon.For example, +, -, *, / are binary operators since they operate on two operands that they add, subtract, multiply or divide. Except for % the other operators are as usual. % is read as 'modulus' operator, and it returns the remainder on dividing an int with another. As against this, a / returns the quotient. While evaluating an expression, C gives priorities to some operators above the others. A unary minus has the highest priority, followed by arithmetic operators and lastly the assignment operator. Within arithmetic operators, *, / and % enjoy a higher priority than + and -.6
Integer and Float Conversions
It is important to understand the rules that govern the conversion of floating point and integer values in C. These are mentioned below.- An arithmetic operation between an integer and integer always yields an integer result.
- An arithmetic operation between a float and float always yields a float result.
- In an arithmetic operation between an integer and float, the integer is first promoted to float and then the operation is carried out. And hence it always yields a float result.
- On assigning a float to an integer ( using the = operator ) the float is demoted to an integer.
- On assigning an integer to a float, it is promoted to a float.
printf() and scanf()
printf() is one of the most versatile statements in C. In fact, it is a standard library function used to display the output on the screen.The general form of printf() looks like this.
printf ("Format string", list of variables);
And here are a few examples...
printf ("%c %d %f, name, age, sal);
printf ("name = %c age = %d salary = %f, name, age, sal);
printf ("name = %c\nage = %d\nsalary = %f, name, age, sal);
Assuming the value of name as 'A', that of age as 23, and that of sal as 1500.00, the output of the above printf( )s would be
A 23 1500.000000
name = A age = 23 salary = 1500.000000
name=A
age = 23
salary = 1500.000000
The first printf() prints the values of the variables name, age and sal. As against this, the second would print messages against each value displayed. In the third printf(), the \n is used to send the cursor to the next line, hence the three values are printed on three different lines.
Sometimes the list of variables may be dropped, as in, printf ("Enter values of a and b");
In this case everything enclosed within the pair of double quotes would be printed as it is on the screen.
Having imbibed those details, now let us switch over to scanf().
scanf( ) is once again a standard library function. It is used to receive values of variables from the keyboard. For example, the following scanf() would let you supply the values of variables a and b.
scanf ("%d%f,&a,&b);
The general form of scanf() is almost similar to printf( ), except for
two important differences:
- Within the pair of double quotes there should occur only format
- specifications like %c, %d, %f etc.
- The variable names must always be preceded by the 'address
- of operator &.
Comments
Post a Comment