From basic syntax to advanced pointers, matrix operations, functions, storage classes, and structures.
'C' is a powerful programming language which has attracted worldwide adoption because software industries rely on it for high-performance computing. A program written in 'C' can be transferred easily from one computer system to another with minimal or no changes. Programs written in 'C' execute fast and efficiently. This versatility makes 'C' a desirable language in the highly competitive software industry.
A system programmer named Dennis Ritchie developed the 'C' language at Bell Laboratories in early 1972. It was written originally for programming under the Unix operating system. The language derives from earlier work by Ken Thompson, who created the 'B' language based on B.C.P.L (Basic Combined Programming Language). Thompson dubbed his version 'B', and later Dennis Ritchie chose the second letter of B.C.P.Lβ'C'βto represent the improved successor language.
Genealogy of C Language Development
'C' uses the following set of valid characters:
*, (, ), #, &, [, ], ", ', +, -, <, >, =, _, etc.
Identifiers are names given to various program elements such as variables, functions, and arrays. An identifier consists of letters and digits in any order, except that the first character must be a letter or an underscore (_).
| Valid Identifiers | Invalid Identifiers | Reason for Invalidity |
|---|---|---|
Temp |
8level |
Cannot start with a digit |
_78po |
cust name |
Cannot contain spaces |
total_sum |
sum-total |
Hyphen (-) is an operator |
Reserved words in 'C' are called keywords. These keywords have predefined meanings to the compiler and cannot be used as user-defined identifiers. All reserve words must be written in lower case letters.
'C' provides several basic data types along with qualifiers (short, long, signed, unsigned):
| Data Type | Size (Bytes) | Range |
|---|---|---|
char or signed char |
1 | -128 to 127 |
unsigned char |
1 | 0 to 255 |
int or signed int |
2 | -32,768 to 32,767 |
unsigned int |
2 | 0 to 65,535 |
short int |
1 | -128 to 127 |
unsigned short int |
1 | 0 to 255 |
long int |
4 | -2,147,483,648 to 2,147,483,647 |
unsigned long int |
4 | 0 to 4,294,967,295 |
float |
4 | 3.4e-38 to 3.4e+38 |
double |
8 | 1.7e-308 to 1.7e+308 |
long double |
10 | 3.4e-4932 to 1.1e+4932 |
'C' has four basic types of constants: Integer constants, Floating point constants, Character constants (enclosed in single quotes `'a'`), and String constants (enclosed in double quotes `"srinivas"`).
An escape sequence begins with a backslash (\) followed by special control characters:
| Character | Escape Sequence | ASCII Value |
|---|---|---|
| Bell (Alert) | \a | 007 |
| Backspace | \b | 008 |
| Horizontal Tab | \t | 009 |
| Vertical Tab | \v | 011 |
| New Line | \n | 010 |
| Form Feed | \f | 012 |
| Carriage Return | \r | 013 |
| Question Mark | \? | 063 |
| Double Quotation Mark | \" | 034 |
| Single Quote | \' | 039 |
| Backslash | \\ | 092 |
| Null Character | \0 | 000 |
A variable is a named memory location used to represent specified information. Variables must be declared before they are used in executable statements.
// Syntax: data_type variable1, variable2;
int a, b, c;
char ch;
float sum;
// Variable Initialization
int value = 5;
char name[10] = "srinivas";
+ (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus - remainder after integer division).
< (Less than), > (Greater than), <= (Less than or equal), >= (Greater than or equal), == (Equal to), != (Not equal).
Logical: && (AND), || (OR), ! (NOT).
Compound Assignment: +=, -=, *=, /=, %= (e.g. a += 2 is equivalent to a = a + 2).
Operators acting on a single operand: Increment (++), Decrement (--), Size of operator (sizeof).
| Specifier | Meaning / Data Type |
|---|---|
%c | Single character |
%d | Signed decimal integer |
%f | Floating point decimal number |
%e | Exponential floating point format |
%s | String of characters |
%x | Hexadecimal integer |
%o | Octal integer |
#include <stdio.h>
int main() {
char a;
printf("Enter a character: ");
a = getchar();
printf("You entered: ");
putchar(a);
return 0;
}
The if-else statement evaluates a condition to select between two decision paths.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0) {
printf("The number %d is EVEN\n", n);
} else {
printf("The number %d is ODD\n", n);
}
return 0;
}
#include <stdio.h>
int main() {
int n, i = 1, count = 0;
printf("Enter any positive integer: ");
scanf("%d", &n);
while (i <= n) {
if (n % i == 0) {
count++;
}
i++;
}
if (count == 2) {
printf("%d is a Prime Number\n", n);
} else {
printf("%d is Not a Prime Number\n", n);
}
return 0;
}
#include <stdio.h>
int main() {
int n = 4, i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
/* Output:
1
1 2
1 2 3
1 2 3 4
*/
An array is a collection of similar data elements stored in contiguous memory locations.
#include <stdio.h>
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2], i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}
A string in 'C' is a 1D character array terminated by a null character (\0). Standard string functions in <string.h>:
strcpy(dest, src) - Copies string src into dest.strcat(str1, str2) - Appends str2 to the end of str1.strcmp(str1, str2) - Compares two strings (returns 0 if identical).strrev(str) - Reverses the characters in a string.#include <stdio.h>
int main() {
int n = 5, i = 1;
loop_start:
printf("%d x %d = %d\n", n, i, n * i);
i++;
if (i <= 10) {
goto loop_start;
}
return 0;
}
#include <stdio.h>
int main() {
int a = 10, b = 5, choice = 1;
switch(choice) {
case 1: printf("Addition: %d\n", a + b); break;
case 2: printf("Subtraction: %d\n", a - b); break;
default: printf("Invalid choice\n"); break;
}
return 0;
}
Functions allow breaking programs into modular, reusable blocks.
#include <stdio.h>
int calculateArea(int length, int width) {
return length * width;
}
int main() {
int area = calculateArea(12, 5);
printf("Area of Rectangle: %d\n", area);
return 0;
}
Storage classes determine the scope, lifetime, and initial value of variables:
auto: Local variable created on function entry and destroyed on exit.extern: Global variable accessible across multiple files.static: Retains its current value across multiple function calls.register: Requests compiler to store variable in fast CPU registers.#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int rollNo;
};
int main() {
struct Student s1;
strcpy(s1.name, "Ramu K.");
s1.rollNo = 101;
printf("Student: %s, Roll: %d\n", s1.name, s1.rollNo);
return 0;
}
Get personalized 1-on-1 lab guidance, problem-solving techniques, and live project certification.
Enroll in Ongole Campus