Official Study Materials for Computer Science
Explore structured advanced study materials through the Problem Solving Through C Unit 03 Introduction to C Language Notes Set 01. Tailored for learners, utilizing these Computer Science resources ensures thorough preparation and strengthens foundational knowledge before final evaluations.
Advanced Resources for Computer Science
Navigate directly to the advanced Computer Science study materials using the digital viewer below. Each resource pack includes detailed conceptual summaries and solved practice questions, allowing students to instantly reinforce their learning.
This page covers the basics of the C language: its history, the ASCII code and C character set, variables, identifiers, data types and declarations, and every family of operators with example programs. It then explains literal and symbolic constants, #define and const, assignment statements, the input and output functions getchar, putchar, gets, puts, scanf and printf, and simple programs with escape sequences.
History of C
1. C was invented and first implemented by Dennis Ritchie on DEC PDP-11 that used the UNIX operating system.
2. C is the result of a development process that started with an older language called BCPL.
3. BCPL was developed by Martin Richards, and it influenced a language called B, which was invented by Ken Thompson.
4. B led to development of C in the 1970.
5. To alter this situation, ANSI established a committee in the beginning of 1983 to create a standard for C, which was implemented in 1987.
ASCII Code
1. ASCII is an acronym for the American Standard Code for Information Interchange.
2. ASCII is a code for representing English characters as numbers, with each letter assigned a number from 0 to 127.
3. For example, the ASCII code for uppercase M is 77.
4. Most computers use ASCII codes to represent text, which makes it possible to transfer data from one computer to another.
5. Text files stored in ASCII format are sometimes called ASCII files.
6. Text editors and word processors are usually capable of storing data in ASCII format, although ASCII format is not always the default storage format.
7. Most data files, particularly if they contain numeric data, are not stored in ASCII format.
8. Executable programs are never stored in ASCII format.
9. The standard ASCII character set uses just 7 bits for each character.
10. There are several larger character sets that use 8 bits, which gives them 128 additional characters.
11. The extra characters are used to represent non-English characters, graphics symbols, and mathematical symbols.
Character Set: What is a character set?
1. A character set is the mapping of characters to binary values.
2. In 8 bit character sets, the values range from 0-255 and one character will be mapped to each of these values.
3. This means that if the users terminal is set to support a particular character set; every time he press a certain key or a combination of keys a corresponding character will be invoked.
ASCII - Character Set
The table shows the 128 ASCII characters. The row number gives the first hexadecimal digit of the code and the column heading gives the second digit (for example, M is in row 4, column D, so its code is 4D hexadecimal = 77 decimal).
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | HT | LF | VT | FF | CR | SO | SI |
| 1 | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN | EM | SUB | ESC | FS | GS | RS | US |
| 2 | SPC | ! | " | # | $ | % | & | ' | ( | ) | * | + | , | - | . | / |
| 3 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | : | ; | < | = | > | ? |
| 4 | @ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
| 5 | P | Q | R | S | T | U | V | W | X | Y | Z | [ | \ | ] | ^ | _ |
| 6 | ` | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o |
| 7 | p | q | r | s | t | u | v | w | x | y | z | { | | | } | ~ | DEL |
C Character Set
1. These are the characters that C recognizes.
2. Letters (upper case and lower case): A B C D E ... and so on; a b c d e ... and so on.
3. Digits: 0 1 2 3 ... and so on.
4. Special Characters (punctuation etc), space (also known as blank): ‘ " ( ) * + - / : = ! & $ ; <> % ? , . ^ # @ ~ ` { } [ ] \ |
Variables and Identifiers
Variables
1. A variable is a named data storage location in user computer’s memory.
2. By using a variable’s name in the program, the users are, in effect, referring to the data stored there.
3. For example: Age = 10; implies that age hold the value 10. Age = 20; now implies that age hold the value 20. So from the above example age changed its value from 10 to 20. Hence age can be referred to as a variable.
Identifier
1. In C the names that are used to reference variables, functions, labels and various other user-defined objects are called identifiers.
2. The length of an identifier in C can vary from one to several characters.
3. In most cases the first character must be a letter or an underscore, and subsequent characters can be a letter, number or an underscore.
4. Identifiers are used extensively in virtually all information processing systems.
5. Naming entities makes it possible to refer to them, which is essential for any kind of symbolic processing.
Variable Names
1. To use variables in the user C program, he must know how to create variable names.
2. In C, variable names must adhere to the following rules: (a) The name can contain letters, digits, and the underscore character (_). (b) The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended. (c) Case matters (that is, upper- and lowercase letters). Thus, the names count and Count refer to two different variables. (d) C keywords can’t be used as variable names. A keyword is a word that is part of the C language.
Variable Name Legality
The following list contains some examples of legal and illegal C variable names:
| Variable Name | Legality |
|---|---|
| Percent | Legal |
| y2x5_fg7h | Legal |
| annual_profit | Legal |
| _1990_tax | Legal but not advised. |
| saving#account | Illegal: Contain the illegal character #. |
| double | Illegal: Is a C Keyword. |
| 9winter | Illegal: First character is a digit. |
1. Using an underscore to separate words in a variable name makes it easy to interpret.
2. The second style is called camel notation.
3. Instead of using spaces, the first letter of each word is capitalized.
4. Instead of interest_rate, the variable would be named InterestRate.
5. Camel notation is gaining popularity, because it’s easier to type a capital letter than an underscore.
6. We use the underscore because it’s easier for most people to read.
7. User should decide which style, he wants to adopt.
Built in Data Types
Data type
1. Two types of built-in data types: Fundamental data types (int, char, double, float, void, pointer) and Derived data types (array, string, structure).
2. It is a way of representing data storage formats.
3. There are five basic data types in C.
4. Those are char for character, int for integer, float for floating point, double - double precision floating point, and void.
5. Values of type char are, in theory, restricted to the defined ASCII characters.
6. The range of types float and double is usually given in digits of precision.
7. The magnitude of type float and double depend upon the method used to represent the floating point numbers.
8. Type void is used to explicitly declare a function as returning no value.
Fundamental Data Types
1. void – used to denote the type with no values.
2. void cannot be used to define a variable.
3. void, in C, is a type that has no size.
4. Thus, if one was to declare a variable of type "void", the compiler would not know how much memory to allocate for it.
5. Therefore void cannot be used to define a variable.
6. int – used to denote an integer type.
7. char – used to denote a character type.
8. float, double – used to denote a floating point type.
9. int *, float *, char * – used to denote a pointer type, which is a memory address type.
Derived data types
1. Array – a finite sequence (or table) of variables of the same data type.
2. String – an array of character variables.
3. Structure – a collection of related variables of the same and/or different data types.
4. The structure is called a record and the variables in the record are called members or fields.
Numeric data Types
1. C provides several different types of numeric variables.
2. The users need different types of variables because different numeric values have varying memory storage requirements and differ in the ease with which certain mathematical operations can be performed on them.
3. Small integers (for example, 1, 199, and -8) require less memory to store, and the user computer can perform mathematical operations (addition, multiplication, and so on) with such numbers very quickly.
4. In contrast, large integers and floating-point values (123,000,000 or 0.000000871256, for example) require more storage space and more time for mathematical operations.
5. By using the appropriate variable types, the users ensure that the program runs as efficiently as possible.
6. Listed below are the numeric data types in c.
| Type | Bytes | Values |
|---|---|---|
| int | 2 or 4 | -32,768 to 32,767 |
| unsigned int | 2 or 4 | 0 to 65,535 |
| signed int | 2 or 4 | -32,768 to 32,767 |
| short int | 2 | -32,768 to 32,767 |
| unsigned short int | 2 | 0 to 65,535 |
| signed short int | 2 | -32,768 to 32,767 |
| long int | 4 | -2,147,483,648 to 2,147,483,647 |
| signed long int | 4 | -2,147,483,648 to 2,147,483,647 |
| unsigned long int | 4 | 0 to 4,294,967,295 |
Variable Definition
1. A variable is a way of referring to a memory location used in a computer program.
2. This memory location holds values - perhaps numbers or text or more complicated types of data like a payroll record.
Variable Declarations
1. Before the users can use a variable in a C program, it must be declared.
2. A variable declaration tells the compiler the name and type of a variable and optionally initializes the variable to a specific value.
3. If the program attempts to use a variable that hasn’t been declared, the compiler generates an error message.
4. A variable declaration has the following form: typename varname; where typename specifies the variable type and must be one of the keywords.
5. From the declarations, varname is the variable name, which must follow the rules mentioned earlier.
6. User can declare multiple variables of the same type on one line by separating the variable names with commas:
int count, number, start; /* three integer variables */ float percent, total; /* two float variables */ char firstname; /* character type variable */
Character Variables
1. A character is a single letter, numeral, punctuation mark, or other such symbol.
2. A string is any sequence of characters.
3. Strings are used to hold text data, which is comprised of letters, numerals, punctuation marks, and other symbols.
4. Using Character Variables, like other variable the users must declare chars before using them, and he can initialize them at the time of declaration.
5. Here are some examples of character variables:
char a, b, c; /* Declare three uninitialized char variables */ char code = 'x'; /* Declare the char variable named code and store the character x there */ code = '!'; /* Store ! in the variable named code */
Integer, Float and Double declarations
1. To declare a variable as integer, follow the below syntax: int variable_name; Here int is the type of the variable named variable_name; ‘int’ denotes integer type.
2. A float is a single-precision floating point value. To declare a variable as float, follow the below syntax: float variable_name;
3. A double is a double-precision floating point value. To declare a variable as double, follow the below syntax: double variable_name;
4. Examples:
int i, j, k; float f, salary; double d;
Expression and Operators
1. The symbols which are used to perform logical and mathematical operations in a C program are called C operators.
2. These C operators join individual constants and variables to form expressions.
3. Operators, functions, constants and variables are combined together to form expressions.
4. Consider the expression A + B * 5.
5. Where, +, * are operators, A, B are variables, 5 is constant and A + B * 5 is an expression.
Types of C operators
1. C language offers many types of operators.
2. They are: Arithmetic operators, Assignment operators, Relational operators, Logical operators, Bit wise operators, Conditional operators (ternary operators), Increment/decrement operators and Special operators.
Arithmetic Operators in C
C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs.
| S.no | Arithmetic Operators | Operation | Example |
|---|---|---|---|
| 1 | + | Addition | A+B |
| 2 | - | Subtraction | A-B |
| 3 | * | Multiplication | A*B |
| 4 | / | Division | A/B |
| 5 | % | Modulus | A%B |
Example program for C arithmetic operators
In this example program, two values “40” and “20” are used to perform arithmetic operations such as addition, subtraction, multiplication, division, modulus and output is displayed for each operation.
#include <stdio.h>
int main()
{
int a=40, b=20, add, sub, mul, div, mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is: %d\n", add);
printf("Subtraction of a, b is: %d\n", sub);
printf("Multiplication of a, b is: %d\n", mul);
printf("Division of a, b is: %d\n", div);
printf("Modulus of a, b is: %d\n", mod);
}Output:
Addition of a, b is: 60 Subtraction of a, b is: 20 Multiplication of a, b is: 800 Division of a, b is: 2 Modulus of a, b is: 0
Assignment operators
1. In C programs, values for the variables are assigned using assignment operators.
2. For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”.
3. Other assignment operators in C language are given below.
| Operators | Symbol | Example | Explanation |
|---|---|---|---|
| Simple assignment operator | = | sum=10 | 10 is assigned to variable sum |
| Compound assignment operators | += | sum+=10 | This is same as sum = sum+10 |
| Compound assignment operators | -= | sum-=10 | This is same as sum = sum-10 |
| Compound assignment operators | *= | sum*=10 | This is same as sum = sum*10 |
| Compound assignment operators | /= | sum/=10 | This is same as sum = sum/10 |
| Compound assignment operators | %= | sum%=10 | This is same as sum = sum%10 |
| Compound assignment operators | &= | sum&=10 | This is same as sum = sum&10 |
| Compound assignment operators | ^= | sum^=10 | This is same as sum = sum^10 |
Example program for C assignment operators
1. In this program, values from 0 – 9 are summed up and total “45” is displayed as output.
2. Assignment operators such as “=” and “+=” are used in this program to assign the values and to sum up the values.
#include <stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Total+i
}
printf("Total = %d", Total);
}Output:
Total = 45
Relational operators
Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
| S.no | Operators | Example | Description |
|---|---|---|---|
| 1 | > | x > y | x is greater than y |
| 2 | < | x < y | x is less than y |
| 3 | >= | x >= y | x is greater than or equal to y |
| 4 | <= | x <= y | x is less than or equal to y |
| 5 | == | x == y | x is equal to y |
| 6 | != | x != y | x is not equal to y |
Example program for relational operators in C
1. In this program, relational operator (==) is used to compare 2 values whether they are equal or not.
2. If both values are equal, output is displayed as “values are equal”.
3. Else, output is displayed as “values are not equal”.
4. Note : double equal sign (==) should be used to compare 2 values.
5. We should not use single equal sign (=).
#include <stdio.h>
int main()
{
int m=40, n=20;
if(m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}Output:
m and n are not equal
Logical operators
1. These operators are used to perform logical operations on the given expressions.
2. There are 3 logical operators in C language.
3. They are, logical AND (&&), logical OR (||) and logical NOT (!).
| S.no | Operators | Name | Example | Description |
|---|---|---|---|---|
| 1 | && | logical AND | (x>5)&&(y<5) | It returns true when both conditions are true. |
| 2 | || | logical OR | (x>=10)||(y>=10) | It returns true when at-least one of the condition is true. |
| 3 | ! | logical NOT | !((x>5)&&(y<5)) | It reverses the state of the operand “((x>5) && (y<5))”. If “((x>5) && (y<5))” is true, logical NOT operator makes it false |
Example program for logical operators in C
In this program, operators (&&, || and !) are used to perform logical operations on the given expressions.
1. && operator: “if clause” becomes true only when both conditions (m>n and m!=0) is true. Else, it becomes false.
2. || Operator: “if clause” becomes true when any one of the condition (o>p || p!=20) is true. It becomes false when none of the condition is true.
3. ! Operator: It is used to reverses the state of the operand. If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by “!” operator. So, “!(m>n && m!=0)” returns false (0).
#include <stdio.h>
int main()
{
int m=40, n=20;
int o=20, p=30;
if(m>n && m!=0)
{
printf("&& Operator: Both conditions are true\n");
}
if(o>p || p!=20)
{
printf("|| Operator: Only one condition is true\n");
}
if(!(m>n && m!=0))
{
printf("! Operator: Both conditions are true\n");
}
else
{
printf("! Operator: Both conditions are true. " \
"But, status is inverted as false\n");
}
}Output:
&& Operator: Both conditions are true || Operator: Only one condition is true ! Operator: Both conditions are true. But, status is inverted as false
Bit wise operators
1. These operators are used to perform bit operations.
2. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
3. Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift).
Truth table for bit wise operation
| x | y | x|y | x&y | x^y |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Bit wise operators
| Operator symbol | Operator name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ~ | Bitwise NOT |
| ^ | XOR |
| << | Left Shift |
| >> | Right Shift |
Consider x=40 and y=80. Binary form of these values are given below.
x = 00101000
y = 01010000
All bit wise operations for x and y are given below.
x&y = 00000000 (binary) = 0 (decimal).
x|y = 01111000 (binary) = 120 (decimal).
~x = 1111111111111111111111111111111111111111111111111111111111010111 = -41 (decimal).
x^y = 01111000 (binary) = 120 (decimal).
x << 1 = 01010000 (binary) = 80 (decimal).
x >> 1 = 00010100 (binary) = 20 (decimal).
Note:
1. Bit wise NOT : Value of 40 in binary is 0000000000000000000000000000000000000000000000000000000000101000. So, all 0’s are converted into 1’s in bit wise NOT operation.
2. Bit wise left shift and right shift : In left shift operation “x << 1”, 1 means that the bits will be left shifted by one place. If we use it as “x << 2”, then, it means that the bits will be left shifted by 2 places.
Example program for bit wise operators in C
In this example program, bit wise operations are performed as shown above and output is displayed in decimal format.
#include <stdio.h>
int main()
{
int m=40, n=80, AND_opr, OR_opr, XOR_opr, NOT_opr;
AND_opr = (m&n);
OR_opr = (m|n);
NOT_opr = (~m);
XOR_opr = (m^n);
printf("AND_opr value = %d\n", AND_opr);
printf("OR_opr value = %d\n", OR_opr);
printf("NOT_opr value = %d\n", NOT_opr);
printf("XOR_opr value = %d\n", XOR_opr);
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", m >> 1);
}Output:
AND_opr value = 0 OR_opr value = 120 NOT_opr value = -41 XOR_opr value = 120 left_shift value = 80 right_shift value = 20
Conditional or ternary operators
1. Conditional operators return one value if condition is true and returns another value if condition is false.
2. This operator is also called as ternary operator.
3. Syntax : (Condition? true_value: false_value);
4. Example : (A > 100 ? 0 : 1);
5. In above example, if A is greater than 100, 0 is returned else 1 is returned.
6. This is equal to if else conditional statements.
Example program for conditional/ternary operators in C
#include <stdio.h>
int main()
{
int x=1, y;
y = (x ==1 ? 2 : 0);
printf("x value is %d\n", x);
printf("y value is %d", y);
}Output:
x value is 1 y value is 2
Increment/decrement Operators
1. Increment operators are used to increase the value of the variable by one and decrement operators are used to decrease the value of the variable by one in C programs.
2. Syntax: Increment operator : ++var_name; (or) var_name++; Decrement operator : --var_name; (or) var_name--;
3. Example: Increment operator : ++i; i++; Decrement operator : --i; i--;
Example program for increment operators in C
In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}Output:
1 2 3 4 5 6 7 8 9
Example program for decrement operators in C
In this program, value of “i” is decremented one by one from 20 up to 11 using “i--” operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}Output:
20 19 18 17 16 15 14 13 12 11
Special Operators in C
Below are some of special operators that C language offers.
| S.no | Operators | Description |
|---|---|---|
| 1 | & | This is used to get the address of the variable. Example : &a will give address of a. |
| 2 | * | This is used as pointer to a variable. Example : *a where, * is pointer to the variable a. |
| 3 | Sizeof () | This gives the sizeof the variable. Example : sizeof(char) will give us 1. |
Example program for & and * operators in C
In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to. Please refer C – pointer topic to know more about pointers.
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}Output:
50
Example program for sizeof() operator in C
sizeof() operator is used to find the memory space allocated for each C data types.
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n", sizeof(a));
printf("Storage size for char data type:%d \n", sizeof(b));
printf("Storage size for float data type:%d \n", sizeof(c));
printf("Storage size for double data type:%d\n", sizeof(d));
return 0;
}Output:
Storage size for int data type: 4 Storage size for char data type: 1 Storage size for float data type: 4 Storage size for double data type: 8
Constants and Literals
1. Like a variable, a constant is a data storage location used by the users program.
2. Unlike a variable, the value stored in a constant can’t be changed during program execution.
3. C has two types of constants, each with its own specific uses.
Literal Constants
20 and ‘R’ are the examples for literal constant:
int count = 20; char name= 'R';
Symbolic Constants
1. A symbolic constant is a constant that is represented by a name (symbol) in the program.
2. Like a literal constant, a symbolic constant can’t change.
3. Whenever the User needs the constant’s value in the program, he can use its name as would use a variable name.
4. The actual value of the symbolic constant needs to be entered only once, when it is first defined.
5. Symbolic constants have two significant advantages over literal constants.
6. Suppose that the user’s writing a program that performs a variety of geometrical calculations.
7. The program frequently needs the value of π (PI) - (3.14159) for its calculations.
8. The value of π is constant.
9. For example: To calculate the circumference and area of a circle with a known radius, the user could write the following code:
Circumference = 3.14159 * (2 * radius); Area = 3.14159 * (radius) * (radius);
1. The asterisk (*) is C’s multiplication operator.
2. Thus, the first of these statements means Multiply 2 times the value stored in the variable radius, and then multiply the result by 3.14159.
3. Finally, assign the result to the variable named circumference.
4. If, however, the user define a symbolic constant with the name PI (π) and the value 3.14, he could write the following code:
Circumference = PI * (2 * radius); Area = PI * (radius) * (radius);
1. The second advantage of symbolic constants becomes apparent when the user’s need to change a constant.
2. Continuing with the preceding example, the user might decide that for greater accuracy the program needs to use a value of PI with more decimal places: 3.14159 rather than 3.14.
3. If the user had used literal constants for PI, he would have to go through the source code and change each occurrence of the value from 3.14 to 3.14159.
4. With a symbolic constant, he needs to make a change only in the place where the constant is defined.
5. The rest of code would not need to be changed.
Defining symbolic constants
1. C has two methods for defining a symbolic constant: the #define directive and the const keyword.
2. The first way to define a symbolic constant is the #define directive, used as follows: #define CONSTNAME literal
3. This creates a constant named CONSTNAME with the value of literal. literal represents a literal constant, as described earlier.
4. CONSTNAME follows the same rules described earlier for variable names.
5. By convention, the names of symbolic constants are uppercase.
6. This makes them easy to distinguish from variable names, which by convention are lowercase.
7. For the previous example, the required #define directive for a constant called PI would be: #define PI 3.14159
8. Note that #define lines don’t end with a semicolon(;).
9. #define statements can be placed anywhere in your source code, but the defined constant is in effect only for the portions of the source code that follow the #define directive.
10. Most commonly, programmers group all #define statements together, near beginning of the file and before the start of the main() function.
How a #define works?
1. The precise action of the #define directive is to instruct the compiler as follows: In the source code, replace CONSTNAME with literal.
2. The effect is exactly the same as if the user had used the editor to go through the source code and make the changes manually.
3. For example:
#define PI 3.14159 /* the users have defined a constant for PI. */ #define PIPE 100
Defining Constants with the const keyword
1. The second way to define a symbolic constant is with the const keyword.
2. Const is a modifier that can be applied to any variable declaration: const int a=10;
3. A variable declared to be const can’t be modified during program execution - only initialized at the time of declaration.
4. Here are some examples:
const int count = 100; const long debt = 12000000, float tax_rate = 0.21;
Const affects all variables on the declaration line. In the last line, debt and tax_rate are symbolic constants.
Literals
1. The constants refer to fixed values that the program may not alter during its execution.
2. These fixed values are also called literals.
3. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal.
4. There are also enumeration constants as well.
Integer literals
1. An integer literal can be a decimal, octal, or hexadecimal constant.
2. A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
3. An integer literal can also have a suffix that is a combination of U and L, for unsigned and long, respectively.
4. The suffix can be uppercase or lowercase and can be in any order.
Floating-point literals
1. A floating-point literal has an integer part, a decimal point, a fractional part, and an exponent part.
2. The user can represent floating point literals either in decimal form or exponential form.
3. While representing using decimal form, the user must include the decimal point, the exponent, or both and while representing using exponential form; he must include the integer part, the fractional part, or both.
4. The signed exponent is introduced by e or E.
Character literals
1. Character literals are enclosed in single quotes, e.g., ‘x’ and can be stored in a simple variable of char type.
2. A character literal can be a plain character (e.g., ‘x’), an escape sequence (e.g., ‘\t’), or a universal character (e.g., ‘\u02C0’).
3. There are certain characters in C when they are preceded by a backslash they will have special meaning and they are used to represent like newline (\n) or tab (\t).
4. Some of these escape sequence codes are listed with the program under Frequently used Escape Sequences at the end of this unit.
String literals
1. String literals or constants are enclosed in double quotes "".
2. A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.
3. The users can break a long line into multiple lines using string literals and separating those using whitespaces.
Simple Assignments Statement
1. A statement is a complete direction instructing the computer to carry out some task.
2. In C, statements are usually written one per line, although some statements span multiple lines.
3. C statements always end with a semicolon (except for #define and #include).
4. Statements and White Space: The term white space refers to spaces, tabs and blank lines in the user source code. The C compiler isn’t sensitive to white space. When the compiler reads a statement in the user source code, it looks for the characters in the statement and for the terminating semicolon, but it ignores white space.
5. Thus, the statement x=2+3; is equivalent to x = 2 + 3; with white spaces.
6. It is also equivalent to as follows:
x = 2 + 3;
1. This gives the user a great deal of flexibility in formatting the source code.
2. Statements should be entered one per line with a standardized scheme for spacing around variables and operators.
Basic Input Output Statements
1. The basic input/output functions are getchar, putchar, gets, puts, scanf and printf.
2. The first two functions, getchar and putchar, are used to transfer single characters.
3. The next function gets and puts are used to input and output strings, and the last two functions, scanf and printf, permit the transfer of single characters, numerical values and strings.
getchar() Function
1. getchar( ) function is used to read one character at a time from the key board.
2. Syntax ch = getchar( ); where ch is a char Var.
3. Ex:
main( )
{
char ch;
printf("Enter a char");
ch = getchar( );
printf("ch =%c", ch);
}Output:
Enter a char M M ch = M
When this function is executed, the computer will wait for a key to be pressed and assigns the value to the variable when the “enter” key pressed.
putchar() Function
1. putchar( ) function is used to display one character at a time on the monitor.
2. Syntax: putchar (ch);
3. EX:
char ch = 'M'; putchar(ch);
The Computer display the value char of variable ‘ch’ i.e M on the Screen.
gets() Function
1. gets( ) function is used to read a string of characters including white spaces.
2. Note that white spaces in a string cannot be read using scanf( ) with %s format specifier.
3. Syntax: gets (S); where ‘S’ is a char string variable.
4. Ex:
char S[ 20 ]; gets (S);
When this function is executed the computer waits for the string to be entered.
Puts() Function
puts() is a function used to display strings on screen. For example, puts(S); displays the string S on the screen.
scanf()
1. Just as most programs need to output data to the screen, they also need to input data from the keyboard.
2. The most flexible way the program can read numeric data from the keyboard is by using the scanf() library function.
3. The scanf() function reads data from the keyboard according to a specified format and assigns the input data to one or more program variables.
4. For example: The statement reads a decimal integer from the keyboard and assigns it to the integer variable x as shown below: scanf("%d", &x);
5. The ‘%’ indicates that the conversion specification follows: The ‘d’ represents the data type and indicates that the number should be read as a integer. The ‘&’ is ‘C’ unary operator that gets the memory address of the variable following it.
6. The user will read more about this operator and its associated operator ‘*’, in the section ‘Pointers’ in the course.
7. Likewise, the following statement reads a floating-point value from the keyboard and assigns it to the variable rate: scanf("%f", &rate); The ‘f’ represents the data type and indicates that the number should be read as a float.
printf()
1. The printf() function, part of the standard C library, is perhaps the most versatile way for a program to display data on-screen.
2. Printing a text message on-screen is simple.
3. Call the printf() function, passing the desired message enclosed in double quotation marks.
4. For example, to display an error that has occurred on-screen, the user write the following: printf("An error that has occurred!");
5. In addition to text messages, however, he frequently needs to display the value of program variables.
6. This is a little more complicated than displaying only a message.
7. It accepts a string parameter (called the format string), which specifies a method for rendering a number of other parameters (of which there typically may be arbitrarily many, of a variety of types) into a string.
8. For example, suppose the user wants to display the value of the numeric variable x on-screen, along with some identifying text.
9. Furthermore, he wants the information to start at the beginning of a new line.
10. The printf() function as shown below: printf("\nThe value of x is %d", x); where \n represents a new line character.
11. The resulting screen display, assuming that the value of x is 12, would display the following: The value of x is 12.
12. In this example, two arguments are passed to printf().
13. The first argument is enclosed in double quotation marks and is called the format string.
14. The second argument is the name of the variable (x) containing the value to be printed.
Simple ‘C’ programs
The ‘Hello World’ introduction
1. The best way to learn a computer language is to start writing short programs that work and then gradually add complexity.
2. The traditional first C program prints out "hello, world" and looks something like this:
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}Use of the gets() Function
#include <stdio.h>
int main()
{
char str[50];
printf("Enter a string: ");
gets(str);
printf("You entered: %s", str);
return(0);
}To Multiply Two Integers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=5;
b=10;
c=mul(a,b);
printf("Multiplication of %d and %d is %d",a,b,c);
}Frequently used Escape Sequences
/* Testing the escape sequences */
#include <stdio.h>
int main(void)
{
printf("Testing the escape sequences:\n");
printf("-----------------------------\n");
/* 3 times audible tone */
printf("The audible bell ---> \\a \a\a\a\n");
printf("The backspace ---> \\b___ \bTesting\n");
/* printer must be attached...*/
printf("The formfeed, printer ---> \\f \fTest\n");
printf("The newline ---> \\n \n\n");
printf("The carriage return ---> \\r \rTesting\r\n");
printf("The horizontal tab ---> \\t \tTesting\t\n");
printf("The vertical tab ---> \\v Testing\v\n");
printf("The backslash ---> \\ \\Testing\\\n");
printf("The single quote ---> \' \'Testing\'\'\n");
printf("The double quote ---> \" \"Testing\"\"\n");
printf("Some might not work isn't it?\n");
return 0;
}Free study material for Computer Science
Free CBSE Study Guides: Computer Science Programming in C
Quick Revision Material for Programming in C
Explore essential learning tools for Computer Science Programming in C. This curated collection features in-depth notes and targeted practice questions built around the active 2026 curriculum to streamline your daily revision.
Expert Notes and Solved Exam Questions
Built using official NCERT guidelines for Computer Science, these materials provide reliable academic support. Integrating past examination questions and step-by-step solutions helps students understand official CBSE grading criteria.
Additional Study Resources for
Wrap up your chapter revision by testing your knowledge against standard objective question formats. Explore our full library of free, up-to-date sample papers and online assessments to secure top grades in your CBSE examinations.
FAQs
The latest 2026-27 advanced study resources for Computer Science are available for free on StudiesToday.com which includes NCERT Exemplars, high-order thinking skills (HOTS) questions, and deep-dive concept summaries.
Our exhaustive Computer Science package includes chapter wise revision notes, solved practice sheets, important formulas and Concept Maps to help in better understanding of all topics.
Yes. For , our resources have been developed to help you get better marks in school exams and also build fundamental strength needed for entrance tests including Competency Based learning.
in , students should use Active Recall method, read the concept summary, then solve the Important Questions section without looking at the answers and then check your answers.
All Computer Science study materials are provided in mobile-friendly PDF. You can download and save them on your device.
Yes, our team has ensured that all Computer Science materials for are strictly aligned with the National Education Policy (NEP) 2020 and the latest 2026-27 syllabus.