- C Programming
- C Tutorial
- C Basic Syntax
- C Data Types
- C if...else Statement
- C switch Statement
- C for Loop
- C while Loop
- C do...while Loop
- C Jump Statements
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Library Functions
- C Recursion
- C Variable Scope
- C Structures
- C Linked List
- C Stacks
- C Queues
- C Binary Tree
- C Header Files
- C File I/O
- C Programming Examples
- C Programming Examples
C Tutorial
Hi, I'm William, and it is my pleasure to introduce you to Dave, a highly skilled and experienced C programmer with over 8 years of experience in the field. Dave has built a successful career in software development. He loves to code and is good at finding solutions to problems.
Dave's expertise in C programming is extensive, having been honed over many years of hands-on experience. His ability to write clean, efficient, and effective code has earned him a reputation as a reliable and highly sought-after developer. His depth of knowledge in the language is matched only by his enthusiasm for sharing that knowledge with others.
In this tutorial, Dave will serve as your guide, sharing his expertise and insights into the world of C programming. Dave's knowledge and guidance will help you reach your goals, whether you are a beginner who wants to learn the basics of the language or an experienced developer who wants to learn more about more advanced ideas.
So, without further ado, let's start our journey into the interesting and difficult world of C programming with Dave's expert help.
Introduction to C programming
C is one of the most popular and widely used programming languages in the world. C is known for its speed, efficiency, and low-level control over system resources, making it an ideal language for developing operating systems, device drivers, and other system software. It is also used a lot to make high-performance software, such as game engines and financial software.
The C programming language was created, influenced, and field-tested by working programmers. C gives the programmer what the programmer wants: few restrictions, few complaints, stand-alone functions, block structure, and a compact set of keywords. By using the C programming language, you can nearly achieve the efficiency of assembly code when combined with the structure of Pascal or Modula-2.
Who created the C programming language?
It was created by Dennis Ritchie.
When was the C programming language created?
The C programming language was created in the 1970s and first appeared in 1972. Despite being over 40 years old, C remains relevant and widely used today. It is still a popular choice for developing embedded systems as well as scientific and engineering applications.
What are the filename extensions of C source code?
The filename extension of a C source file is ".c." For example, "fresherearth.c."
Which languages are influenced by the C programming language?
The C programming language influenced many other programming languages, including C++, Java, JavaScript, PHP, Python, Perl, Objective-C, and others.
About this C programming tutorial
In this tutorial, we will explore the basics of C programming, starting with the fundamental concepts and building up to more advanced topics. We will cover topics such as data types, variables, operators, control structures, functions, and arrays. We will also talk about some of the language's more advanced features, like pointers, structures, and working with files.
Whether you are a beginner programmer looking to learn a new language or an experienced developer looking to expand your skill set, this tutorial will provide you with a solid foundation in C programming. By the end of this tutorial, you will have a good understanding of the language and be able to write your own C programs. So let's get started!
Hot topics in the C programming language
The following is a list of hot topics, or topics that are frequently discussed when discussing the C programming language. Understanding these topics is required to become an effective C programmer.
- basic syntax of a C program
- data types in C programming
- if...else statement in C
- switch statement in C
- for loop in C
- while loop in C
- do...while loop in C
- arrays in C programming
- strings in C programming
- pointers in C programming
- functions in C programming
- structures in C programming
- linked list data structure in C
- stack data structure in C
- queue data structure in C
- file handling in C programming
All of the above topics are covered in their own posts; however, let me cover some of the C language topics in this post, beginning with "variables in C."
Variables in C
Variables are named locations in the computer's memory that are used to store program values. Before using a variable in a program, it must be declared. Based on the data types, we can declare and define multiple types of variables. I believe you should visit the "data types" post first before proceeding to read the content further.
To put it another way, we can declare "int" type variables to store integers or whole numbers, "float" type variables to store real numbers or floating-point numbers, and "char" type variables to store characters.
The general form to declare a variable in the C programming language is as follows:
type variable_name;
The "type" must be a valid C data type, whereas "variable_name" is any valid identifier such as "myvar," "fresherearth," "sum," "a," "res," etc. For example:
int num;
The above statement declares an "int" type variable named "num." Now we can use the "num" variable in our C program to store integer values. We can also declare multiple variables of the same type using a single statement. Consider the following code fragment:
int a, b, c;
The preceding statement declares three variables of type "int." That is, the variables "a," "b," and "c" are used to store integers in the program. The following code fragment shows some other variable declarations.
int a, b, c; char ch1, ch2; double bal, prof, loss; float perc, avg;
The following is the general form of variable declaration and initialization in the C programming language:
type variable_name = value;
or
type variable1 = value1, variable2 = value2, ..., variableN = valueN;
For example
int num1 = 12, num2 = 23, num3 = 34; float perc = 89.50;
The variable can also be initialized later in the program. Consider the following code fragment as an example:
int num1, num2, num3; float perc; num1 = 12; num2 = 23; num3 = 34; perc = 89.50;
If a variable is already initialized, you can also use it directly or indirectly to initialize another variable. For example:
int a, b, c; float p; a = 12; b = 23; c = 34; p = (a + b + c) / 3;
Let me include an example program to demonstrate variables practically. I believe you should visit the post titled "Basic Syntax of a C Program" first to get the complete idea behind the working of a C program.
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("\nYou entered: %d \n", num); return 0; }
The following snapshot shows the initial output produced by the above program.
Now type a number, say 10, and hit the ENTER key to get the following output:
In the preceding program, I used the "printf()" statement to display the text "Enter a number: " on the output console, so the user knew he or she needed to enter a number. When the number was entered, it was stored in the "num" variable. I used the "scanf()" method to scan the input. The "%d" is the format specifier for "int" type values.
After receiving the values from the user, I printed the value back on the output console using the "printf()" statement. The "\n" is an escape sequence character used to insert a line break on the output console.
Let me include another example regarding the "variables in C."
#include <stdio.h> int main() { int a = 10, b = 20, c = 30, sum; float p; sum = a + b + c; p = sum/3; printf("%f", p); return 0; }
20.000000
You can use ".2" before the "f" format specifier to print only two digits after the decimal. Here is the "printf()" statement to do this job.
printf("%.2f", p);
Now the output should be:
20.00
To declare constants in the program, use the "const" keyword. When we need to initialize a variable but do not want to change its value, we can define it as a constant variable by using the "const" keyword. It takes the following general form:
const type variableName = value;
For example:
const int x = 10;
The variable "x" now has the value "10," which cannot be changed in the program.
We can also use the "#define" preprocessor to define constants or macros in the program. The following is its general form.
#define name value
For example:
#define PI 3.1415
A semicolon is not required at the end of the "#define" statement. Consider the following example program demonstrating the use of the "#define" preprocessor.
#include <stdio.h>
#define PI 3.1415
int main()
{
float r, a;
printf("Enter the radius of a circle: ");
scanf("%f", &r);
a = PI * r * r; // use of PI here
printf("Area = %.2f \n", a);
return 0;
}
Enter the radius of a circle: 54.6 Area = 9365.31
Now, wherever we write "PI" in the program, it will be replaced with "3.1415".
We can also use the "#define" preprocessor to define the most frequently used code in the program. Consider the following program:
#include <stdio.h> #define for1to10 for(int i=1; i<=10; i++) int main() { int num = 4, x; for1to10 { x = num+i; printf("%d \n", x); } printf("\nMultiplication table of %d: \n", num); for1to10 { printf("%d \n", num*i); } return 0; }
5 6 7 8 9 10 11 12 13 14 Multiplication table of 4: 4 8 12 16 20 24 28 32 36 40
As you can see, I used the macro "for1to10" two times in the same program to use the following code:
for(int i=1; i<=10; i++)
These are just demos for your understanding; you can, however, use the "#define" preprocessor to define a variety of macros in your program.
Escape sequence characters in C
The following table lists all the escape sequences that can be used in the C programming language.
Escape Sequence Character | Output |
---|---|
\a | Bell (alert) |
\b | Backspace |
\f | Form feed |
\n | New line |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\' | Single quotation mark |
\" | Double quotation mark |
\\ | Backslash |
\? | Literal question mark |
When you write \" in the printf() statement, then you will get " on the output console. For example:
#include <stdio.h> int main() { printf("C is \"fun\" to learn."); return 0; }
C is "fun" to learn.
The other three escape sequence characters are: \ ooo, \x hh, and \x hhhh. The first two are used for ASCII characters in octal and hexadecimal notation, whereas the last is used for Unicode characters in hexadecimal notation.
Operators in the C language
Operators in a C program are used when we need to perform operations on variables or directly on values. Consider the following program as an example demonstrating the use of operators in the C language:
#include <stdio.h>
int main()
{
int a = 10, b = 20, sum;
sum = a + b; // use of '+' operator
printf("%d + %d = %d", a, b, sum);
return 0;
}
10 + 20 = 30
In the above example, I used the "+" operator to find the sum of two values.
Following are the categories or types of operators available in the C programming language:
Arithmetic Operators in C
Arithmetic operators are the most basic operators available in the C programming language and are used to perform arithmetic operations. The following is a list of arithmetic operators available in the C language.
- +: The addition operator, which is used to find the addition.
- -: The subtraction operator, which is used to find the subtraction.
- *: The multiplication operator, which is used to find the multiplication.
- /: The division operator, which is used to find the division.
- %: The modulus operator, which is used to find the remainder while dividing two numbers.
- ++: The increment operator, which is used to increment the value by 1.
- --: The decrement operator, which is used to decrement the value by 1.
For example:
#include <stdio.h> int main() { int a = 10, b = 21, c = 5; printf("%d + %d = %d \n", a, b, a+b); printf("%d - %d = %d \n", b, a, b-a); printf("%d * %d = %d \n", a, b, a*b); printf("%d / %d = %d \n", b, a, b/a); printf("%d %% %d = %d \n", b, a, b%a); printf("%d %% %d = %d \n", a, c, a%c); return 0; }
10 + 21 = 31 21 - 10 = 11 10 * 21 = 210 21 / 10 = 2 21 % 10 = 1 10 % 5 = 0
The "increment" and "decrement" operators come in two forms. The pre- and post-form. The value is first incremented or decremented in the "pre-" form before being used. Whereas in "post-" form, the value is first used, then incremented or decremented. Consider the following program as an example:
#include <stdio.h> int main() { int a = 10, b; b = ++a; // increment before use printf("%d \n", b); a = 10; b = a++; // increment after use printf("%d \n", b); a = 10; b = --a; // decrement before use printf("%d \n", b); a = 10; b = a--; // decrement after use printf("%d \n", b); return 0; }
11 10 9 10
Assignment Operators in C
Assignment operators are used to assign values. Consider the following code fragment as an example demonstrating the use of the assignment operator to assign values to a variable in the program.
int num = 5; num += 10;
The first statement assigns 5 to "num," whereas the second statement adds 10 to "num" and assigns the final value to "num." Therefore, "num" now has the value "15."
The following is a list of assignment operators available in the C programming language.
- =: assigns values to variables
- +=: add and then assign
- -=: subtract, then assign
- *=: multiply, then assign
- /=: divide, then assign
- %=: take modulus and assign
- <<=: left-shift and assign
- >>=: right-shift and assign
- &=: bitwise-AND and assign
- | =: bitwise-inclusive-OR and assign
- ^=: bitwise-exclusive-OR and assign
Comparison Operators in C
Comparison operators are used when we need to compare values. Consider the following program as an example:
#include <stdio.h> int main() { int a = 10, b = 20; if(a<b) printf("'a' is less than 'b'."); return 0; }
'a' is less than 'b'.
In the code "a<b", the "<" is the comparison operator, which is used to compare if "a" is less than "b." Since "a" (10) is less than "b" (20), therefore the code returns "true" or "1" and the condition of "if" evaluates to "true" and the program flow enters the body of the "if" and executes the "printf()" statement, which prints the text "'a' is less than 'b'." Don't worry, there is a separate tutorial on the "if" statement in the coming post. You will learn all about it.
The following is a list of comparison operators available in the C programming language:
- ==: equal to
- !=: not equal to
- >: greater than
- <: less than
- >=: greater than or equal to
- <=: less than or equal to
Logical Operators in C
Here are the three logical operators you can use in the C programming language to figure out how variables and/or values make sense together.
- &&: logical AND
- ||: logical OR
- !: logical NOT
The "&&" returns "true" if all statements are true, the "||" returns true if any of the statements is true, whereas the "!" returns true if the statement is true and returns false if the statement is true. Basically, "!" reverses the result. Consider the following program as an example:
#include <stdio.h> int main() { int a = 10, b = 20, c = 30; printf("%d \n", (a<b && b<c)); printf("%d \n", (a<b && c<b)); printf("%d \n", (a<b || b<c)); printf("%d \n", (a<b || c<b)); printf("%d \n", !(a<b)); printf("%d \n", !(b<a)); return 0; }
1 0 1 1 0 1
Bitwise Operators in C
The bitwise operators are used when we need to do bit-by-bit operations in the program. The following is a list of bitwise operators available in the C language.
- &: AND
- |: OR
- ^: XOR
- ˜: One's complement
- <<: left shift
- >>: right shift
The following table shows the truth table of "AND," "OR," and "XOR."
A | B | A & B | A | B | A ^ B |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
However, other operators, such as "sizeof()," "&," "*," and "?:," are also available in C. The "sizeof()" operator returns the size of a variable. The "&" and "*" are called the "address of" and "value at address" operators. These two operators are discussed in the "pointer in C" post. The "?:" is called the "conditional" or "ternary" operator. This operator can be used as a replacement for the "if...else" statement. Consider the following program:
#include <stdio.h> int main() { int a = 10, b = 20, res; res = (a>b) ? a : b; printf("%d \n", res); return 0; }
20
The ternary operator (?:) has three components. The following is its general form:
statement ? value1 : value2;
If the "statement" evaluates to "true," then the whole expression is replaced with "value1," otherwise with "value2." Therefore, in the preceding example, since the expression "a>b" evaluates to "false," because the value of "a" is not greater than the value of "b," the whole expression is replaced with "b," which is "20." And 20 was initialized to "res," whose value was printed on the output console using the "printf()" statement.
Because the content is becoming too lengthy, I'm closing the discussion for this post. This is page one of the "C tutorial." You will learn much more about the C language one by one, beginning with "basic syntax," then "data types," and so on. So just follow the "Next Tutorial" link to continue learning the C language.
« fresherearth Home Next Tutorial »