what is C ?
C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972.
- A basic programming language is C. Of all the languages, this one is used for programming the most.
- C is a fundamental language in which we can write in whole operating system.
Why Learn C ?
- It is one of the most popular languages in the world.
- After learning this language, we will not have any problem in learning any other programming language.
- Like Java ,JavaScript, C++,Python etc. Their syntax is similar.
- c language is faster than other programming languages. Like Java, Python.
Get Started C Language
To start two thing:
- A text editor, like Notepad, To write C code
- A compiler GCC, to translate a C code to Machine Language.
Install IDE(integrated Development environment)IDE is that which provides all facilities required by a programmer for developing software on a single Screen.
- There are many IDE includes : Visual studio code, code: :Block, Devpp.
- But it is popular in Visual studio code.
C syntax
Line 1. #include<stdio.h> is a header file library. Its work is input output function.
Line 2. The Next part C program is to declare main() this is a function. Any code inside the curly bracket {} will execute.
Line 3. printf is a function used that prints text on the output screen.
Line4. return 0 ends the main() function.
Some of C Header files:
- Stddef.h - Describes several useful types and macros.
- Stdint.h - identifies precise width integer types.
- Stdio.h - specifies core input and output function.
- Stdlib.h - Defines numeric conversion function, pseudo-random network generator,memory allocation.
- String.h - Defines string handling function.
- Math.h - Defines common mathematics function.
C Output (print Text)
- To output values and print text, it utilizes the printf function.
Example
Double Quotes
- Text must be enclosed in double quotation marks (" ") when working with it.
- If you forget the double quotes, an error occurs:
New Lines
- To insert the new line , use the \n (backslash) character.
Example
- Multiple lines can also be used in a single printf function.
Example
- #include<stdio.h>
- int main(){printf("hello world\n I am learning C\nAnd it is Awesome");return 0;}
Escape Sequence Description
\t Creates a horizontal tab
\\ Insert a backslash character (\)
\” Insert a double quotes character
- We use comments to test alternative code since they help to clarify the code and make it more readable.
- Comments can be single line and multiple lines.
Single line comments
- Two forward slashes (//) welcome single line comments.
- Text between // forward slash the end of ignore by the compiler will not execute.
Example
// This is comment
printf("Hello world");
- This example uses a single line comment.
Example
- printf("Hello world"); // This is comment
Multi line Comments
- Multi line comment start with /* and end with */.
- Text between /* and */ ignore the compiler.
Example
- */The code will printf in Hello world into the screen*/
Variables
- The data values, such as numbers and alphabets, are stored in a variable.
Different types of variable (define different keyword), For example;
- int - store integer (whole Number),without decimals, Example - 123,-123.
- float - Store floating point Number, With decimals, Example - 19.99,-19.99.
- char - store single character , Example - ‘a’ , ’d’ character will under single quotes.
Variables naming Rules:
- Variables name is any combination of alphabet, digit and underscore.
- No other symbol is allowed.
- Valid variable names cannot start with a digit.
Output Variables
- In many other programming languages like Python, Java, C++ we use print function to get the value of variables but this is not possible in C.
C Format Specifier
Format specifier to be used together with the printf () function to tell the compiler what type of data the variable is storing.it is basically a placeholder for the variable value.
- Format specifiers start with a percentage sign %. And, it follows the character.
- To output the value of an int variable, we use the format specifier %d within double quotes in the printf function.
Example:
printf("My favorite number is: %d",x);
float
| Bitwise OR Sets each 2 bit is 1 the result come as 1
int x = 15;
printf("%d",x);
- To print other types, use char for %c , float for %f.
Example
int main()
{
int myNum = 15; //integer(whole Number)
float myfloatNum = 19.99; //floating point Number
char myLetter = 'a'; // character
// print variables
printf("%d",myNum);
printf("%f",myfloatNum);
printf("%c",myLetter);
}
- To combine both text and variables, separate with the commas inside the printf function.
printf("My favorite number is: %d",x);
Variables Related Programs:
- This is int variables program.
#include<stdio.h>
int main()
{
int x = 10;
int y = 20;
printf("%d%d\n",x,y);
return 0;
}
Output
10
20
- This is float program.
#include<stdio.h>
int main()
{
float x = 9.9;
float y = 10.6;
printf("%f%f",x + y);
return 0;
}
Output
19.79
char program.
#include<stdio.h>
int main(){
char x = 'a';
char y = 'b';
printf("%c%c\n",x,y);
return 0;
}
Output
a,b
#include<stdio.h>
int main()
{
// student data
int studentid = 123;
int studentage = 18;
float studentfees = 2500.00;
// print variablrs
printf("student id: %d\n",studentid);
printf("student age%d\n",studentage);
printf("student fees%f\n",studentfees);
return 0;
}
C Data Types:
A variables in C specified data type and use a format specifier inside the printf function to display it:
Example
#include<stdio.h>
int main()
{
// Create variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
}
Output
5
5.99
D
Basic Data types
Variables are stored according to the size and type of the data that specifies it.
Data size Description Example
Type
int 4 bytes store whole number , without decimals 1
float 4 bytes store fractional number, with decimals. store 1.99
in 6 to 7 decimals number.
double 8 bytes store fractional number, with decimals. storing 1.98
upto 15 decimal digits
char 1 bytes Store a single character / letter / number. 'A"
Basic Format Specifier Data Type
%d int
%f or %F float
%If double
%c char
C character Data types:
A single character can be stored using the char data type.
Example :
char myGrade = 'A';
printf("%c", myGrade);
Example :
char a = 65, b = 66, c = 67;
printf("%c", a);
printf("%c", b);
printf("%c", c);
Example :
char myText = 'Hello';
printf("%c", myText);
C Numeric Data Types
Numeric Types
When you need to store a whole number (without decimals), such as 35 or 1000, use int. When you need to store a floating point number (with decimals), such as 9.99 or 3.14515, use float or double.
int
int myNum = 100;
printf("%d",myNum);
float
float myNum = 9.99;
printf("%f",myNum);
double
double myNum = 19.99;
printf("%1f",myNum);
Float vs double
float - Seven- and six-digit numbers can be stored in floating-point variables. Four bytes are used by Float.
double - We can store 15-digit values in double variables. Memory used by double is 8 bytes.
C Data type example
#include<stdio.h>
int main()
{
// create variables in data types
int item = 50;
float cost per item = 9.99;
float total cost = item * cost per item;
char = 'a';
// print variables//
printf("Number of items: %d\n", items);
printf("Cost per item: %.2f %c\n", cost_per_item, currency);
printf("Total cost = %.2f %c\n", total_cost, currency);
return 0;
}
C Constants
If you change the value of existing variables then use the const keyword.
Example
const int my Num = 15; // myNum is always is 15
myNum = 10; // assigment can read only variables myNum
Example
const int minutesperhour = 60;
const float pi = 3.15;
Example
const int minutesperhour;
This is not work;
const int minutesperhour;
minutesperhour = 60;
C Operator
Operator are used to perform operation on variables and values.
Arithmetic Instruction
An instruction which is used to manipulate data using operator is
known as arithmetic instruction.
Unary Operator
The unary operator only accepts one data as input. unary operator
commonly known as increment and decrement operator are used .
only single variables used.
- Pre increment Operator ++x , --x.
- Post increment Operator x++ , x--.
Unary Operator Types:
- Increment Operators
- Decrement Operators
Increment Operator
- Operators for increments like ++x and x++. And they increase by the value of one.
Pre increment:
#include<stdio.h>
int main()
{
int x = 10, y = 20;
int p = ++x + ++y;
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",p);
return 0;
}
Output
11
21
32
Post increment:
#include<stdio.h>
int main()
{
int x = 20;
int y = 40;
int p = x++ + y++;
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",p);
return 0;
}
Output
21
41
60
Decrement Operator
- Operators for increments like --x and x--. And they Decrease by the value of one.
Pre increment:
#include<stdio.h>
int main()
{
int x = 10, y = 20;
int p = --x - --y;
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",p);
return 0;
}
Output
9
19
- 10
Post increment:
#include<stdio.h>
int main()
{
int x = 10, y = 20;
int p = x-- - y--;
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",p);
return 0;
}
Output
9
19
- 10
#include<stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d\n",++a);
printf("--b = %d\n",--b);
printf("++c = %d\n",++c);
printf("--d = %d\n",--d);
return 0;
}
Output
++a = 11
--b = 99
++c = 0
--d =0
Binary Operators:
Binary operators are operators that take two operands or data.
Binary Operators Types:
- Arithmatic Operators
- Logical Operators
- Relational Operators
- Bit wise Operators
- Assignment Operators
Arithmatic Operators
Arithmatic Operators Example
+ (Addition) A+B
- (Subtraction) A-B
* (Multiplications) A*B
/ (Division) A/B
% (Modulus) A%B
Note - C language real constants not the apply modulus.
Related Programs:
#include<stdio.h>
int main()
{
int x = 10;
int y = 20;
printf("%d",x+y);
return 0;
}
#include<stdio.h>
int main()
{
int a = 10;
int b = 20;
int c ;
c = a+b;
printf("a+b = %d\n",c);
c = a-b;
printf("a-b = %d\n",c);
c = a*b;
printf("a*b = %d\n",c);
c = a/b;
printf("a/b = %d\n",c);
return 0;
}
Output
a+b = 30
a-b = -10
a*b = 200
a/b = 0
Note - Two integer will be perform operation also answer is integer.
Logical Operators
Logical operators can also used to test True aur False value.
Operators Name Description
&& AND Logical AND Operator. It both Operands
are non zero ,condition become True.
|| OR Logical OR Operator. It both operands
are non zero , condition become True.
! NOT Logical NOT Operator. If condition True.
But, Logical NOT Operator it is make in False.
Relational Operators
Relational Operators are used to compare two values. This is important in programming, because it helps find answer and decisions.
Operators Name Description
< less than if the value of the left operands
is less than the values of the right
operands . if yes then the condition
become true.
> Greater than if the value of the left operands
is Greater than the values of the right
operands . if yes then the condition
become true.
<= less than equal if the value of the left operands
is less than or equal to the values of the right
operands. If yes then the condition
become true.
>= Greater than equal the value of the left operands is greater than or equal to
the values of the right operands. if yes then the condition become true.
== Equal to If two operands have equal value then the condition is true otherwise it is false.
!= Not equal to if the value of two operands are equal
or not. if the value are not equal then the
condition become true.
Bitwise Operators
Operator | Name | Description |
---|
& | Bitwise AND | Sets each bit to 1 if both bits are 1 |
^ | Bitwise XOR | Sets each bit to 1 if only one of the bits is 1 |
~ | Bitwise NOT | Inverts all the bits (flips 1s to 0s and 0s to 1s) |
<< | Left Shift | Shifts bits to the left by the specified number of positions |
>> | Right Shift | Shifts bits to the right by the specified number of positions |
Control Instruction
- Decision control instruction.
- Iternative control instruction.
- Switch case control instruction.
- Goto control instruction.
Decision control Instruction
If will enter the block if the condition is true. if will leave the block if the condition is false.
If Syntax
if (condition)
{
code;
code;
code;
}
If program
#include<stdio.h>
int main()
{
int a = 5;
printf("enter a number");
scanf("%d",&a);
if(a>0);
{
printf("positive");
}
if(a<=0);
{
printf("non positive");
}
getch();
}