c unit Ist
c unit Ist
in 1972.
It is a very popular language, despite being old. The main reason for its popularity is because it
is a fundamental language in the field of computer science.
C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Advantages of c:-
It is one of the most popular programming languages in the world and it is fundamental
language
If you know C, you will have no problem learning other popular programming languages
such as Java, Python, C++, C#, etc, as the syntax is similar
C is very fast, compared to other programming languages, like Java and Python
C is very versatile; it can be used in both applications and technologies
Rich library :-C provide lot of inbuilt function that make the development fast
C is a simple language because it is provide structure approach, inbuilt fuction, data
types etc.
Machine independent
Extensible language
myfirstprogram.c
Code:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Result:
Hello World!
Line 1: #include <stdio.h> is a header file library that lets us work with input and output
functions, such as printf() (used in line 4). Header files add functionality to C programs.
Don't worry if you don't understand how #include <stdio.h> works. Just think of it as something
that (almost) always appears in your program.
Line 2: A blank line. C ignores white space. But we use it to make the code more readable.
Line 3: Another thing that always appear in a C program is main(). This is called a function. Any
code inside its curly brackets {} will be executed.
Line 4: printf() is a function used to output/print text to the screen. In our example, it will
output "Hello World!".
Note that: Every C statement ends with a semicolon ;
Note: The body of int main() could also been written as:
int main(){printf("Hello World!");return 0;}
Remember: The compiler ignores white spaces. However, multiple lines makes the code more
readable.
Line 5: return 0 ends the main() function.
Line 6: Do not forget to add the closing curly bracket } to actually end the main function.
Statements
What is computer program ?
A computer program is a list of "instructions" to be "executed" by a computer.
In a programming language, these programming instructions are called statements.
The following statement "instructs" the compiler to print the text "Hello World" to the screen:
It is important that you end the statement with a semicolon ;
If you forget the semicolon (;), an error will occur and the program will not run:
To output values or print text in C, you can use the printf() function:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Many printf Functions
You can use as many printf() functions as you want. However, note that it does not insert a new
line at the end of the output:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
printf("And it is awesome!");
return 0;
}
To insert a new line, you can use the \n character:
Example
#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
What is \n exactly?
The newline character (\n) is called an escape sequence, and it forces the cursor to change its
position to the beginning of the next line on the screen. This results in a new line. Or ‘\n’ is used
to insert a new line
Examples of other valid escape sequences are:
Comments in C
It is not executable part of the program and it is ignored by the compiler in c programming
language
Uses :-
Comments can be used to explain code, and to make it more readable. It can also be used to
prevent execution when testing alternative code.
Comments can be singled-lined or multi-lined.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will not be executed).
This example uses a single-line comment before a line of code:
Example
// This is a comment
printf("Hello World!");
History of C language
C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph), located in the U.S.A.
Dennis Ritchie is known as the founder of the c language.
It was developed to overcome the problems of previous languages such as B, BCPL, etc.
initially, C language was developed to be used in UNIX operating system. It inherits many
features of previous languages such as B and BCPL.
Let's see the programming languages that were developed before C language.
Features of C Language
C is the widely used language. It provides many features that are given below.
1. Simple i
2. Machine Independent or Portable viii
3. Mid-level programming language vii
4. structured programming language ix
5. Rich Library iii
6. Memory Management x
7. Fast Speed ii
8. Pointers vi
9. Recursion v
10. Extensible iv
1) Simple
C is a simple language in the sense that it provides a structured approach (to break the problem
into parts), the rich set of library functions, data types, etc.
5) Rich Library
C provides a lot of inbuilt functions that make the development fast.
6) Memory Management
It supports the feature of dynamic memory allocation. In C language, we can free the allocated
memory at any time by calling the free() function.
7) Speed
The compilation and execution time of C language is fast since there are lesser inbuilt functions
and hence the lesser overhead.
8) Pointer
C provides the feature of pointers. A pointer is a variable that contains the address of another
variable. We can directly interact with the memory by using the pointers. We can use pointers
for memory, structures, functions, array, etc.
Ex:-
int x=10;
int pointer = &x;
printf (“%d”,pointer);
9) Recursion
In C, A function which calls itself is called recursive function and this technique is called
recursion. we can call the function within the function. It provides code reusability for every
function. Recursion enables us to use the approach of backtracking.
10) Extensible
C language is extensible because it can easily adopt new features.
Structure of C language
C programming follows a well-defined structure that consists of various components, each
serving a specific purpose. Here's a breakdown of the typical structure of a C program:
1. Preprocessor Directives
`#include <stdio.h>`
Preprocessor directives are commands that are processed by the preprocessor before the actual
compilation of the code begins or preprocessor directives that instruct preprocessor to perform
specific task before compilation of the code begins.
- These lines begin with a `#` and instruct the compiler to include or process certain files or
macros before compiling the code. Commonly used directives include `#include` for including
standard or user-defined header files, and `#define` for defining constants.
2. Global Declaration
- Example:`int globalVar;`
Golbal declaration refers to variable which is declared outside all the function and can be access
throughout the program
- Variables, constants, or function prototypes that need to be accessible throughout the
program can be declared globally.
3. main() Function
Example:
int main() {
// code here
return 0;
}
```
- This is the entry point of any C program. The execution starts from the `main()` function. The
code is between curly braces is executed by compiler. typically returns an integer value (usually
`0` for successful execution) and may receive command-line arguments.
4. Local Declarations
-Example:`int localVar;`
Local declaration refers to variable which is declared inside the fuction and this variable is not
accessible outside the function.
- Variables declared within a function are local to that function and cannot be accessed
outside of it.
5. Statements & Expressions
- Example: localVar = 10;`
- These are the executable parts of the program. Statements include assignments, function
calls, loops, and conditional structures that define the logic of the program.
6. User-Defined Functions
- Example
// Global Declaration
int globalVar = 5;
// User-Defined Function
void greet() {
printf("Hello from a function!\n");
}
// Executable Statements
printf("Hello, World!\n");
printf("Global Variable: %d\n", globalVar);
printf("Local Variable: %d\n", localVar);
// Return Statement
return 0;
}
This structure allows C programs to be modular, efficient, and easy to debug.
int main() {
char ch = 'A';
printf("Character: %c, ASCII value: %d\n", ch, ch);
return 0;
}
Keywords in C Programming
in C programming language, identifiers are the building blocks of a program. Identifiers are
unique names that are assigned to variables, structs, functions, and other entities.
1. An identifier can include letters (a-z or A-Z), and digits (0-9).
2. An identifier cannot include special characters except the ‘_’ underscore.
3. Spaces are not allowed while naming an identifier.
4. An identifier can only begin with an underscore or letters.
5. We cannot name identifiers the same as keywords because they are reserved words to
perform a specific task. For example, printf, scanf, int, char, struct, etc. If we use a
keyword’s name as an identifier the compiler will throw an error.
6. The identifier must be unique in its namespace.
7. C language is case-sensitive so, ‘name’ and ‘NAME’ are different identifiers.
int main() {
int sum = 0; // 'sum' is an identifier
return 0;
}
A variable is a named location in memory that stores data and whose value can change
during the execution of a program.
Variables must be declared with a data type before they are used.
char 1 %c
float 4 %f
double 8 %lf
signed char 1 %c
unsigned char 1 %c
int
Integers are whole numbers that can have both zero, positive and negative values but no
decimal values. For example, 0, -5, 10
float and double
float and double are used to hold real numbers.
char
Keyword char is used for declaring character type variables. For example,
char test = 'h';
The size of float (single precision float data type) is 4 bytes. And the size of double (double
precision float data type) is 8 bytes.
The size of the character variable is 1 byte.
void
void is an incomplete type. It means "nothing" or "no type". You can think of void as absent.
For example, if a function is not returning anything, its return type should be void.
sigvalid codes
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int
User Defined
The user-defined data types are defined by the user himself.
Data Types
The data types that are derived from the primitive or built-in data
types are referred to as Derived Data Types.
Derived Types
Non - Primitive Data Types :- The data types thar are derived from inbuilt data types or
defined by user himself ex:- array, fuction , structure, union, enum etc.
Comments:-
Comments are used to add explanations or annotations in the code. They are ignored by the
compiler and do not affect the execution of the program.
C supports two types of comments:
Single-line Comment: Starts with // and continues to the end of the line.
Multi-line Comment: Enclosed between /* and */.
// This is a single-line comment
/*
* This is a multi-line comment
* that spans multiple lines.
*/
int main() {
// Print a message
printf("Hello, World!\n");
return 0;
}