0% found this document useful (0 votes)
9 views9 pages

C Programming Language Notes 1

The C programming language is a general-purpose, procedural language developed in the 1970s, primarily for system applications and is considered the foundation for many other languages. It features a structured program format including header files, main functions, variable declarations, and input/output functions like printf() and scanf(). These functions allow for formatted output and user input, making C essential for software development.

Uploaded by

Devbrat Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

C Programming Language Notes 1

The C programming language is a general-purpose, procedural language developed in the 1970s, primarily for system applications and is considered the foundation for many other languages. It features a structured program format including header files, main functions, variable declarations, and input/output functions like printf() and scanf(). These functions allow for formatted output and user input, making C essential for software development.

Uploaded by

Devbrat Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is the C programming language?

The C programming language is a general-purpose, procedural, and structured


programming language that was developed by Dennis Ritchie at AT&T Bell Labs in
the early 1970s. It was initially used to develop the UNIX Operating System. Now, it is
the most widely used programming language.

The C programming language is mainly developed to create system applications that


directly interact with the hardware devices, such as drivers, kernels, etc. It is
considered the base for other programming languages, which is why it is known as
the mother language.

1. #include <stdio.h>
2. int main() {
3. printf("Hello, World!\n");
4. return 0;
5. }

C Programming Definition
It can be defined in the following ways:

Why Learn the C Programming Language?


The C programming language is essential for students and professionals who want to
become great software developers. The C programming language provides several
useful features, which make it a widely utilized programming language. Some main
features are as follows:
Where is C Programming Used?
The C programming language is commonly used in several areas where performance,
efficiency, and control are required. Several uses of the C programming language are
as follows:

Structure of the C Program


The basic structure of a simple C program typically contains several important
components. Some of the most utilized components of the C program are as follows:

1) Header Files:
The header files are typically included at the start of the program in C programming
with the help of the #include directives. These header files are commonly used to
give the prototypes and definitions of the functions that the compiler can
understand and make sense of the functions used in the program.
2) Main Function:
The main() function of the program is referred to as the entry point of the program.
Its return type is integer because it has to give an integer value to the operating
system on completion.

3) Variable Declarations:
If we need to use variables in the program, we need to declare these variables with
their data types before using them. It is commonly utilized after the main function's
curly opening brace.

4) Statements and Expressions:


This part of the C program holds the program instructions and logic. C programs
contain several statements that perform actions and expressions to calculate values.
These statements can carry out numerous operations, which include assignments,
loops, and functions.

5) Comments:
Comments are mainly used in the C programming language, where they can be used
to provide a human-readable description of the code. During the compilation, these
comments have no impact on the working of the program. Several types of
comments exist in the C programming, among which include Single-line comments
denoted by // and multi-line comments denoted by /* */.

6) Functions:
The user-defined functions are also known as blocks of code, which are created to
execute particular operations that can be included in C programs. These functions
help in modularizing the program, which makes the code more organized, reusable,
and manageable.

7) Return Statement:
The C return statement is also used to end a function and return a value back to the
calling function. The return statement with a value of 0 typically indicates a successful
execution. On the other hand, a non-zero value indicates an error or unexpected
termination.

8) Standard Input/Output:
The C program contains several library functions, including the scanf() and printf
functions. The scanf() function is used to read the user input, and the printf() function
is used to print the output to the console. They are included in C programs, and they
are included in the standard I/O library.
printf() and scanf() in C
The C programming language provides many functions for input and output. The most
basic functions for input and output are the printf() and scanf(). These two functions are
part of the standard input/output library functions of the C language, which are declared
in the stdio.h (header file). The printf() function is used to display the output. On the
other hand, the scanf() function is used to take input from the user.

Printf() Function
In the C programming language, the printf() function is used to display output. It
prints the given statement to the console. The printf() function can print text,
numbers, and variables to the standard output.

Syntax:
The syntax of the printf() function is given below:

1. printf("format string", argument_list);

In this syntax,

o Format string: It is used to define the output structure and include several
format specifiers, such as %d (integer), %c (character), %s (string), %f (float),
and many others.
o Argument_list: It is used to pass the argument (value of variables) that will
replace the format specifiers.

Printf() Function Example in C


Let us take an example to illustrate the printf() function in C.

Example
1. #include <stdio.h>
2. int main() { //main function
3. int stu_age = 24; //using integer
4. float pi = 3.14159; //using float with 2 decimals
5. char grade = 'A'; //using char
6. char stu_name[] = "Michael Bravo";
7.
8. printf("Name: %s\n", stu_name);
9. printf("Age: %d\n", stu_age);
printf("Grade: %c\n", grade);

10. printf("Pi (2 decimals): %.2f\n", pi);


11. return 0;
12. }
13. Output:
14. Name: Michael Bravo
15. Age: 24
16. Grade: A
17. Pi (2 decimals): 3.14
In this example, we have taken 4 different data types with their value: integer, floating-
point number, character, and string. After that, we use the printf() function to display
different data types values in a formatted way.

Important Points about the printf() function


Several important points about the printf() function in the C programming language
are as follows:

o The printf() function is declared in the stdio.h header file. It is used to display the
output in the console.
o It is also used to print a different type of data format on the output string.
o We need to use the "\n" format specifiers in the printf() statement to print a new line
on the console.

o Format Specifier in C
o In the C programming language, the format specifier is a string that is used in
the formatted input and output functions. There are several format specifiers
in C. Some of the popular format specifiers are as follows:

Format Specifier Meaning Results

%d It is used to print the 10


integer value.

%f It is used to print the 3.141593


floating-point value.

%.2f It is used to print the 3.14


float with 2 decimal
values.
%c It is used to print the A
character value.

%s It is used to print the Hello


string.

%u It is used to print the 25


unsigned integer
value.

%x It is used to print the 1a


hexadecimal value.

%o It is used to print the 12


octal value.

Scanf() Function
In the C programming language, the scanf() function is utilized to take input from the
user. It reads the input data from the console. It can read different types of data, such
as numeric, floating-point numbers, strings, characters, and many others.

Syntax:
It has the following syntax:

scanf("format string", argument_list);


In this syntax,

o Format string: It is used to define the input type and include several format
specifiers, such as %d (integer), %c (character), %s (string), %f (float), etc.
o argument_list: It is used to provide the addresses of variables (by using &)
where the input values will be stored.
Scanf() Function Example in C
Let us take an example to illustrate the Scanf() function in C. In this example, we will
get the input from the user and print the cube of the given number.

Example
1. #include<stdio.h>
2. int main(){ //main function
3. int num;
4. printf("Enter a number: ");
5. scanf("%d", &num);
6. printf("Cube of a number is: %d ", num*num*num);
7. return 0;
8. }

Output:

Enter a number:15
Cube of a number is:3375
Explanation:

In this example, we can conclude that the scanf("%d", &num) statement reads an
integer number from the console and stores the given value in the number variable.
After that, the printf("cube of number is: %d ", num*num*num) statement prints the
cube of the number on the console.

C Program to print the sum of 2 numbers using both


printf() and scanf() functions
Let us take a simple example of input and output in the C language that prints the
sum of 2 numbers.

Example
1. #include<stdio.h>
2. int main(){ //main function
3. int x=0, y=0, result=0;
4.
5. printf("Enter first number: ");
6. scanf("%d", &x);
7. printf("Enter second number: ");
8. scanf("%d", &y);
9.
10. result = x + y;
11. printf("sum of 2 numbers: %d ", result);
12.
13. return 0;
14. }
15. Output:
16. Enter first number: 15
17. Enter second number: 20
18. sum of 2 numbers: 35

Explanation:

In this example, we calculate the sum of two numbers using the printf() and scanf()
functions. Here, we have taken 3 integer variables: x, y, and result. After that, the
scanf() function takes input for x and y, calculates their sum, and displays the output
using the printf() function.

C Program to print the sum of 2 numbers using both


printf() and scanf() functions
Let us take a simple example of input and output in the C language that prints the
sum of 2 numbers.

Example
1. #include<stdio.h>
2. int main(){ //main function
3. int x=0, y=0, result=0;
4.
5. printf("Enter first number: ");
6. scanf("%d", &x);
7. printf("Enter second number: ");
8. scanf("%d", &y);
9.
10. result = x + y;
11. printf("sum of 2 numbers: %d ", result);
12.
13. return 0;
14. }
Output:

Enter first number: 15


Enter second number: 20
sum of 2 numbers: 35
Explanation:

In this example, we calculate the sum of two numbers using the printf() and scanf()
functions. Here, we have taken 3 integer variables: x, y, and result. After that, the
scanf() function takes input for x and y, calculates their sum, and displays the output
using the printf() function.

C Program to Swap Two Numbers


Let us take an example to illustrate how to swap two numbers in C.

Example
1. #include <stdio.h>
2. int main() { //main function
3. int a, b, temp;
4.
5. printf("Enter two numbers: ");
6. scanf("%d %d", &a, &b);
7.
8. temp = a;
9. a = b;
10. b = temp;
11.
12. printf("After swapping: a = %d, b = %d\n", a, b);
13.
14. return 0;
15. }
Output:

Enter two numbers: 10


21
After swapping: a = 21, b = 10
Explanation:

In this example, we have two integers that take input value from the user using the
scanf() function, which swaps their values using a temp variable. After that, we use
the printf() function to display the swapped values.

Conclusion
In conclusion, the printf() function is utilized to display output on the console. On the
other hand, the scanf() function is utilized to take input from the user. These
functions form the foundation of input/output operations in C. By using format
specifiers, these functions can handle different data types, such as integers, floats,
characters, and strings, which makes them essential to create interactive programs.

You might also like