XII - C LANGUAGE PROGRAMMING NOTES
XII - C LANGUAGE PROGRAMMING NOTES
A character set is a set of alphabets, letters and some special characters that are valid in C language.
Alphabets:
Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
Digits:
0 1 2 3 4 5 6 7 8 9
Special Characters:
Special Characters in C Programming
, < > . --
( ) ; $ :
% [ ] # ?
‘ & { } “
^ ! * / |
- \ ~ +
C Keywords:
Keywords are predefined, reserved words used in programming that have special meanings to the compiler.
Keywords are part of the syntax and they cannot be used as an identifier. For example:
int money;
Here, int is a keyword that indicates money is a variable of type int (integer). As C is a case sensitive language, all
keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.
C Keywords
auto double int struct
do if static while
All these keywords, their syntax, and application will be discussed in their respective topics.
C Identifiers:
Identifier refers to name given to entities such as variables, functions, structures etc.
Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of
the program. For example:
int money;
double accountBalance;
Also remember, identifier names must be different from keywords. You cannot use int as
an identifier because int is a keyword. int money;
double accountBalance;
Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is
a keyword.
You can choose any name as an identifier if you follow the above rule, however, give meaningful names to identifiers
that make sense.
C Data Types:
In C programming, data types are declarations for variables. This determines the type and size of data associated
with variables. For example,
int myVar;
Here, myVar is a variable of int (integer) type. The size of int is 4 bytes.
Basic types:
Here's a table containing commonly used types in C programming for quick access.
signed char 1 %c
unsigned char 1 %c
Int:
Integers are whole numbers that can have both zero, positive and negative values but no decimal values. For
example, 0, -5, 10
We can use int for declaring an integer variable.
int id;
The size of int is usually 4 bytes (32 bits). And, it can take 232 distinct states from -2147483648 to 2147483647.
float salary;
double price;
The size of float (single precision float data type) is 4 bytes. And the size of double (double precision float data type)
is 8 bytes.
Char:
Keyword char is used for declaring character type variables. For example,
Void:
void is an incomplete type. It means "nothing" or "no type". You can think of void as absent.
For example, if a function is not returning anything, its return type should be void.
long a;
long long b;
long double c;
Here variables a and b can store integer values. And, c can store a floating-point number.
If you are sure, only a small integer ([−32,767, +32,767] range) will be used, you can use short.
short d;
You can always check the size of a variable using the sizeof() operator.
#include <stdio.h>
int main() {
short a;
long b;
long long c;
long double d;
For example,
// valid codes
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int
Here, the variables x and num can hold only zero and positive values because we have used the unsigned modifier.
Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1, whereas variable x can hold values
from 0 to 232-1.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the
symbolic representation of a memory location.
For example:
Here, playerScore is a variable of int type. Here, the variable is assigned an integer value 95.
char ch = 'a';
// some code
ch = 'l';
Note: You should always try to give meaningful names to variables. For example: firstName is a better variable name
than fn.
C is a strongly typed language. This means that the variable type cannot be changed once it is declared.
For example:
Constants:
If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a
constant. For example,
Escape Sequences:
Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C programming. For
example: newline(enter), tab, question mark etc.
Escape Sequences
Escape Sequence Character
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal Tab
\v Vertical Tab
\\ Backslash
\’ Single Quotation Mark
\? Question Mark
\0 Null Character
For example: \n is used for a newline. The backslash \ causes escape from the normal way the characters are
handled by the compiler.
C Input Output (I/O):
C Output:
In C programming, printf() is one of the main output function. The function sends formatted output to the screen.
For example,
#include <stdio.h>
int main()
{
// Displays the string inside quotations
printf("C Programming");
return 0;
}
Output:
C Programming
Output:
Number = 5
We use %d format specifier to print int types. Here, the %d inside the quotations will be replaced by the value of
testInteger.
Example 3: float and double Output:
#include <stdio.h>
int main()
{
float number1 = 13.5;
double number2 = 12.4;
Output:
number1 = 13.500000
number2 = 12.400000
To print float, we use %f format specifier. Similarly, we use %lf to print double values.
Output
character = a
C Input:
In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function
reads formatted input from the standard input such as keyboards.
Output:
Enter an integer: 4
Number = 4
Here, we have used %d format specifier inside the scanf() function to take int input from the user. When the user
enters an integer, it is stored in the testInteger variable.
Notice, that we have used &testInteger inside scanf(). It is because &testInteger gets the address of testInteger, and
the value entered by the user is stored in that address.
return 0;
}
Output:
We use %f and %lf format specifier for float and double respectively.
Output:
Enter a character: g
You entered g
When a character is entered by the user in the above program, the character itself is not stored. Instead, an integer
value (ASCII value) is stored.
And when we display that value using %c text format, the entered character is displayed. If we use %d to display the
character, it's ASCII value is printed.
Enter a character: g
You entered g.
ASCII value is 103.
Output:
➢ %d for int
➢ %f for float
➢ %lf for double
➢ %c for char
C Comments:
In programming, comments are hints that a programmer can add to make their code easier to read and understand.
For example,
#include <stdio.h>
int main() {
Output:
Hello World
Here, // print Hello World to the screen is a comment in C programming. Comments are completely ignored by C
compilers.
Types of Comments:
There are two ways to add comments in C:
Single-line Comments in C:
In C, a single line comment starts with //. It starts and ends in the same line. For example,
#include <stdio.h>
int main() {
return 0;
}
Output:
Age: 25
In the above example, // create integer variable and // print the age variable are two single line comments.
We can also use the single line comment along with the code. For example,
Here, code before // are executed and code after // are ignored by the compiler.
Multi-line Comments in C:
In C programming, there is another type of comment that allows us to comment on multiple lines at once, they are
multi-line comments.
To write multi-line comments, we use the /*. ...*/ symbol. For example,
#include <stdio.h>
int main() {
int age;
printf("Enter the age: ");
scanf("%d", &age);
return 0;
}
Output:
Use of Comments in C
1. Make Code Easier to Understand
If we write comments on our code, it will be easier to understand the code in the future. Otherwise you will end up
spending a lot of time looking at our own code and trying to understand it.
Comments are even more important if you are working in a group. It makes it easier for other developers to
understand and use your code.
In the program below, suppose we don't need data related to height. So, instead of removing the code related to
height, we can simply convert them into comments.
#include <stdio.h>
int main() {
int age;
// double height;
return 0;
}
Now later on, if we need height again, all you need to do is remove the forward slashes. And, they will now become
statements not comments.
Note: Comments are not and should not be used as a substitute to explain poorly written code. Always try to write
clean, understandable code, and then use comments as an addition.
In most cases, always use comments to explain 'why' rather than 'how' and you are good to go.
C Programming Operators:
An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition.
C Arithmetic Operators:
An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc
on numerical values (constants and variables).
* Multiplication
/ Division
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output:
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Relational Operators:
A relational operator checks the relationship between two operands. If the relation is true, it returns 1; if the relation
is false, it returns value 0.
!= Division 5 != 3 is evaluated to 1
return 0;
}
Output:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators:
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or
false. Logical operators are commonly used in decision making in C programming.
include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output:
(a == b) fifi (c > b) is 1
(a == b) fifi (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
C Assignment Operators:
An assignment operator is used for assigning a value to a variable. The most common assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Output:
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Comma Operators:
Comma operators are used to link related expressions together. For example:
int a, b = 5, c;
Operators Precedence in C:
Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated.
Certain operators have higher precedence than others; for example, the multiplication operator has a higher
precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first
gets multiplied with 3*2 and then adds into 7.
Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the
bottom. Within an expression, higher precedence operators will be evaluated first.