c Programming
c Programming
What is C – Introduction
to C Programming
Language
C programming language is a procedural and general-purpose
programming language. It is fast and simple to learn and
implement. It was developed by Dennis Ritchie in the year of
1972.
Features of C:-
Working of C programming
language:-
Structure of C Program
Documentation section
//print hello program – comments
/* gmtl
*/
Link Section
#include<stdio.h>
- standard input output header file Global variable declaration
Main() function
int main()
{
Declaration part;
Executable part;
return 0;
}
Function definition;
Structure of C programming
language:-
Following is a basic example of C language:-
Commands Meaning
#include<stdio.h It helps you to include the stdio.h header file into your program. With this,
> you can perform input/output operations.
int main() From here the execution of the C program starts. It is the main() function.
/* comments */ If you write something inside this ‘/* */’ then it won’t get executed.
printf(“Hello
Used for printing the output to the screen.
World”);
First C Program
// print Hello C Language
1. #include <stdio.h>
2. int main(){
3. printf("Hello C Language");
4. return 0;
5. }
#include <stdio.h> includes the standard input output library functions. The printf() function
is defined in stdio.h .
int main() The main() function is the entry point of every program in c language.
return 0 The return 0 statement, returns execution status to the OS. The 0 value is used for
successful execution and 1 for unsuccessful execution.
Comments in C
Single Line Comments
// Print Hello World
1. #include<stdio.h>
2. int main(){
3. //printing information
4. printf("Hello World");
5. return 0;
6. }
Tokens in C
Keywords in C
Do If static While
Identifiers in C
Identifiers in C are used for naming variables, functions, arrays, structures, etc.
Identifiers in C are the user-defined words.
Strings in C
Operators in C
Unary Operator
Constants in C
A constant is a value assigned to the variable which will remain the same
throughout the program, i.e., the constant value cannot be changed.
Constant Example
o Special characters in C
o Data Types in C
o A data type specifies the type of data that a variable can store such as integer, floating,
character, etc.
Float 4 byte
Double 8 byte
o
o
o Keywords in C
o A keyword is a reserved word. You cannot use it as a variable
name, constant name, etc. There are only 32 reserved words
(keywords) in the C language.
o
Differences between Keyword and Identifier
Keyword Identifier
1. int main()
2. {
3. int a=10;
4. int A=20;
5. printf("Value of a is : %d",a);
6. printf("\nValue of A is :%d",A);
7. return 0;
8. }
Output
Value of a is : 10
Value of A is :20
C Format Specifier
The Format specifier is a string used in the formatted input and output
functions. The format string determines the format of the input and
output. The format string always starts with a '%' character.
Format Description
specifier
o %d
1. int main()
2. {
3. int b=6;
4. int c=8;
5. printf("Value of b is:%d", b);
6. printf("\nValue of c is:%d",c);
7.
8. return 0;
9. }
In the above code, we are printing the integer value of b and c by using
the %d specifier.
Output
o %u
1. int main()
2. {
3. int b=10;
4. int c= -10;
5. printf("Value of b is:%u", b);
6. printf("\nValue of c is:%u",c);
7.
8. return 0;
9. }
Output
o %o
1. int main()
2. {
3. int a=0100;
4. printf("Octal value of a is: %o", a);
5. printf("\nInteger value of a is: %d",a);
6. return 0;
7. }
In the above code, we are displaying the octal value and integer value of
a.
Output
o %f
1. int main()
2. {
3. float y=3.4;
4. printf("Floating point value of y is: %f", y);
5. return 0;
6. }
Output
o %x and %X
1. int main()
2. {
3. int y=0xA;
4. printf("Hexadecimal value of y is: %x", y);
5. printf("\nHexadecimal value of y is: %X",y);
6. printf("\nInteger value of y is: %d",y);
7. return 0;
8. }
In the above code, y contains the hexadecimal value 'A'. We display the
hexadecimal value of y in two formats. We use %x and %X to print the
hexadecimal value where %x displays the value in small letters, i.e., 'a'
and %X displays the value in a capital letter, i.e., 'A'.
Output
o
o %e
1. int main()
2. {
3. float y=3;
4. printf("Exponential value of y is: %e", y);
5. return 0;
6. }
Output
o %E
1. int main()
2. {
3. float y=3;
4. printf("Exponential value of y is: %E", y);
5. return 0;
6. }
Output
o %g
1. int main()
2. {
3. float y=3.8;
4. printf("Float value of y is: %g", y);
5. return 0;
6. }
Output
o %p
1. int main()
2. {
3. int y=5;
4. printf("Address value of y in hexadecimal form is: %p", &y);
5. return 0;
6. }
Output
o %c
1. int main()
2. {
3. char a='c';
4. printf("Value of a is: %c", a);
5. return 0;
6. }
Output
o %s
1. int main()
2. {
3. printf("%s", "javaTpoint");
4. return 0;
5. }
Output
Minimum Field Width Specifier
Suppose we want to display an output that occupies a minimum number
of spaces on the screen. You can achieve this by displaying an integer
number after the percent sign of the format specifier.
1. int main()
2. {
3. int x=900;
4. printf("%8d", x);
5. printf("\n%-8d",x);
6. return 0;
7. }
In the above program, %8d specifier displays the value after 8 spaces
while %-8d specifier will make a value left-aligned.
Output
Now we will see how to fill the empty spaces. It is shown in the
below code:
1. int main()
2. {
3. int x=12;
4. printf("%08d", x);
5. return 0;
6. }
In the above program, %08d means that the empty space is filled with
zeroes.
Output
Specifying Precision
We can specify the precision by using '.' (Dot) operator which is followed
by integer and format specifier.
1. int main()
2. {
3. float x=12.2;
4. printf("%.2f", x);
5. return 0;
6. }
Output
bi
printf() and scanf() in C
The printf() and scanf() functions are used for input and output in C
language. Both functions are inbuilt library functions, defined in stdio.h
(header file).
printf() function
The printf() function is used for output. It prints the given statement to
the console.
1. printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the
console.
1. scanf("format string",&argument_list);
1. #include<stdio.h>
2. int main(){
3. int number,cb;
4. printf("enter a number:");
5. scanf("%d",&number);
6. cb= number*number*number;
7. printf("cube of number is:%d ",cb);
8. return 0;
9. }
Output
enter a number:5
cube of number is:125
1. #include<stdio.h>
2.
3. int main(){
4. int x,,y,result;
5.
6. printf("enter first number:");
7. scanf("%d",,&x);
8. printf("enter second number:");
9. scanf("%d",,&y);
10.
11.result=x+y;
12. printf("sum of 2 numbers:%d ",result);
13.
14. return 0;
15.}
Output