0% found this document useful (0 votes)
11 views

c unit Ist

C is a general-purpose programming language developed by Dennis Ritchie in 1972, primarily for writing the UNIX operating system. It is widely used due to its speed, versatility, and the foundational knowledge it provides for learning other programming languages. The document also discusses various IDEs for C programming, the structure of C programs, and the importance of functions in modular programming.

Uploaded by

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

c unit Ist

C is a general-purpose programming language developed by Dennis Ritchie in 1972, primarily for writing the UNIX operating system. It is widely used due to its speed, versatility, and the foundational knowledge it provides for learning other programming languages. The document also discusses various IDEs for C programming, the structure of C programs, and the importance of functions in modular programming.

Uploaded by

atul46160
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories

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

 C is considered as middle level language because it bridges the gap


between low level programming language and high level
programming language by providing features of both high level and
low level language.
 C can be used to write both system software and application
software. Middle level language are closely related to low level
language as well as high level language.
 C programming language is considered middle level language
because of its low-level features as well as high level features:
 o run a C program, you can use several editors and Integrated
Development Environments (IDEs) depending on your preference and
operating system. Below are some popular options:
 1. Code::Blocks
 Platform: Windows, macOS, Linux
 Description: Code::Blocks is a free and open-source IDE specifically
designed for C, C++, and Fortran. It comes with a built-in compiler
(MinGW for Windows).
 Features: Debugger, code completion, and project management.
 Installation:
 Download from Code::Blocks.
 Install the version that includes the MinGW compiler.
 Write and compile C programs directly in the IDE.
 2. Dev C++
 Platform: Windows
 Description: Dev C++ is a lightweight IDE for C and C++. It uses the
MinGW port of GCC (GNU Compiler Collection) as its compiler.
 Features: Simple UI, code completion, syntax highlighting.
 Installation:
 Download from Dev C++.
 Install and start coding.
 3. Turbo C++
 Platform: Windows
 Description: An older IDE, often used for educational purposes. It
provides a DOS-like environment and is still taught in some academic
settings.
 Features: Old-school interface, limited by modern standards.
 Installation: You can download it from various online sources, but it's
mostly recommended for academic purposes, not modern
development.
 4. Visual Studio Code (VS Code)
 Platform: Windows, macOS, Linux
 Description: Visual Studio Code is a popular, lightweight code editor
from Microsoft that supports a variety of programming languages,
including C. It requires extensions to run C code.
 Features: Extensible, code autocompletion, built-in Git, customizable.
 Installation:
 Download and install from Visual Studio Code.
 Install the C/C++ Extension from the Extensions marketplace.
 Install a C compiler (e.g., MinGW for Windows).
 Configure tasks to build and run the program.
 Note: You can follow this guide for setting up C in VS Code: VS Code
C++ Setup Guide.
 5. Geany
 Platform: Windows, macOS, Linux
 Description: Geany is a lightweight, fast, and simple text editor with
IDE-like features, supporting C and other languages.
 Features: Syntax highlighting, code folding, and a built-in terminal for
compiling.
 Installation:
 Download from Geany.
 Install and configure the compiler settings.
 6. Eclipse IDE for C/C++ Developers
 Platform: Windows, macOS, Linux
 Description: Eclipse is a powerful, open-source IDE. With the CDT
(C/C++ Development Tooling) plugin, it supports C and C++
development.
 Features: Advanced debugging, refactoring tools, code templates.
 Installation:
 Download from Eclipse IDE for C/C++ Developers.
 Install and configure with a C compiler (e.g., MinGW for Windows).
 7. CLion
 Platform: Windows, macOS, Linux
 Description: CLion is a premium IDE by JetBrains, specifically
designed for C and C++ development. It is also support other
programming languages.It offers advanced features but requires a
subscription (free for students).
 Features: Smart editor, code analysis, powerful debugging.
 Installation:
 Download from JetBrains CLion.
 Install and configure with the appropriate compilers.
 8. Online C Editors
 If you prefer not to install anything locally, you can use online
compilers to run your C programs:
 Repl.it: Replit C Compiler

 OnlineGDB: OnlineGDB C Compiler


 JDoodle: JDoodle C Compiler
 These online compilers are useful for quick testing and sharing code
snippets, but they may lack advanced debugging features compared to
local IDEs.
 Choosing the Right Editor
 Beginners:Turbo C++, Geany, Code::Blocks or Dev C++ are great
starting points because they are easy to install and come with
everything you need.
 Intermediate/Advanced: VS Code with extensions or CLion
provides more flexibility and power for larger projects.
 Online Options: If you want a quick way to run C programs without
installation, go for Repl.it or OnlineGDB.

 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:

Escape Sequence Description

\t Creates a horizontal tab

\\ Inserts a backslash character (\)

\" Inserts a double quote character

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.

Language Year Developed By

Algol 1960 International Group

BCPL 1967 Martin Richard

B 1970 Ken Thompson

Traditional C 1972 Dennis Ritchie

K&RC 1978 Kernighan & Dennis Ritchie


ANSI C 1989 ANSI Committee

ANSI/ISO C 1990 ISO Committee

C99 1999 Standardization Committee

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.

2) Machine Independent or Portable


Unlike assembly language, c programs can be executed on different machines with some
machine specific changes. Therefore, C is a machine independent language.

3) Mid-level programming language


Although, C is intended to do low-level programming. It is used to develop system applications
such as kernel, driver, etc. It also supports the features of a high-level language. That is why it
is known as mid-level language.
) Structured programming language
C is a structured programming language in the sense that we can break the program into parts
using functions. So, it is easy to understand and modify. Functions also provide code reusability.

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

int add(int a, int b) {


return a + b;
}
```
- Functions are blocks of code that perform specific tasks. They help in organizing the code
into reusable components. Functions can be declared before `main()` or in separate files.
7. Return Statement
- Example: `return 0;`
- The `main()` function typically ends with a `return` statement that returns a value to the
operating system.
8. Comments
- **Example:** `// This is a comment`
- Comments are used to explain code and are ignored by the compiler. Single-line comments
start with `//`, while multi-line comments are enclosed between `/*` and `*/`.
Example of a Simple C Program
#include <stdio.h> // Preprocessor Directive

// Global Declaration
int globalVar = 5;

// User-Defined Function
void greet() {
printf("Hello from a function!\n");
}

int main() { // Main Function


// Local Declarations
int localVar = 10;

// Executable Statements
printf("Hello, World!\n");
printf("Global Variable: %d\n", globalVar);
printf("Local Variable: %d\n", localVar);

// Call User-Defined Function


greet();

// Return Statement
return 0;
}
This structure allows C programs to be modular, efficient, and easy to debug.

Function as building Block


Functions in C programming are fundamental building blocks that allow you to break down a
program into smaller, manageable, and reusable components. They help organize code, making
it easier to read, maintain, and debug.
Why Functions are Important /Advantages /Features of functions:-
1. Modularity: Functions allow you to divide a program into smaller, logical parts. Each
function can perform a specific task.
2. Reusability: Once a function is defined, it can be used repeatedly without rewriting the
same code.
3. Ease of Maintenance: If a function needs to be modified, the change is made in one
place, which simplifies maintenance.
4. Abstraction: Functions hide the implementation details from the user, exposing /
available only the interface (function name and parameters).
5. Collaboration : In large program / project members of team can work on different parts
of program simultaneously.
Structure of a Function
A function in C typically consists of the following parts:
1. Return Type: Specifies the type of value the function returns. If it does not return any
value, the return type is void.
2. Function Name: An identifier that names the function. It should be descriptive of the
function's purpose.
3. Parameters (Optional): Input values the function needs to perform its task. These are
defined within parentheses after the function name.
4. Function Body: Contains the code to be executed when the function is called. It is
enclosed within curly braces {}.
5. Return Statement (Optional): If the function returns a value, the return statement is
used to specify what to return.
Character sets
In C programming, a character set is a defined set of characters that the language recognizes
and supports. These characters include letters, digits, symbols, and control characters.
The character set in C defines the set of characters that can be used in the C language. This
includes:
 Letters: A-Z (uppercase) and a-z (lowercase)
 Digits: 0-9
 Special Characters: e.g., +, -, *, /, =, >, <, @, $, etc.
 White Space Characters: Space, tab, newline, etc.
 Control Characters: Characters like \n (newline), \t (tab), etc.
ASCII Character Set
The C language traditionally uses the ASCII (American Standard Code for Information
Interchange) character set, which consists of 128 characters (standard ASCII). Each character has
a corresponding integer value (from 0 to 127). For example:
 A has an ASCII value of 65.
 a has an ASCII value of 97.
 0 has an ASCII value of 48.
#include <stdio.h>

int main() {
char ch = 'A';
printf("Character: %c, ASCII value: %d\n", ch, ch);
return 0;
}

Extended ASCII Character Set


Extended ASCII supports 256 characters (values 0 to 255). This includes the standard ASCII
characters plus additional characters used for foreign languages and special symbols.
C Tokens
 Tokens are the smallest units in a C program that are meaningful to the compiler. The
different types of tokens are:
o Keywords: Reserved words that have a special meaning in C, like int, return, if,
etc.
o Identifiers: Names given to variables, functions, arrays, etc., like x, sum, total,
etc.
o Constants: Fixed values that do not change during the execution of the program,
like 10, 3.14, 'A', etc.
o Strings: Sequence of characters enclosed in double quotes, like "Hello".
o Operators: Symbols that perform operations, like +, -, *, /, etc.
o Special Symbols: Symbols like {, }, [, ], (, ), ;, ,, etc.
 Keywords are reserved words in C that have special meaning and cannot be used as
identifiers (variable names, function names, etc.). Some common keywords include:
o int: Defines an integer type variable.
o float: Defines a floating-point type variable.
o return: Exits a function and optionally returns a value.
o if, else: Used for conditional branching.
o while, for: Used for loops.
o void: Indicates that a function does not return any value.
o else if : is not a single keyword in c is used in conditional statement is
combination of two keywords
o Label : goto is keyword in c but label is identifier or statement . it is not a
keyword in c

Keywords in C Programming

auto break case char

const continue default do

double else enum extern

float for goto if

int long register return

short signed sizeof static

struct switch typedef union

unsigned void volatile while

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.

int age = 25; // 'age' is a variable of type 'int'


float salary = 5000.50; // 'salary' is a variable of type 'float'
Constants
 Constants are fixed values that do not change during the execution of a program. They
can be of various types like integer constants, floating-point constants, character
constants, and string literals.
 Defined using the const keyword or using #define.

const int MAX = 100; // 'MAX' is a constant integer


#define PI 3.14 // 'PI' is a constant defined using #define
Data type
Each variable in C has an associated data type. It specifies the type of data that the variable can
store like integer, character, floating, double, etc. Each data type requires different amounts of
memory and has some specific operations which can be performed over it.

Type Size (bytes) Format Specifier

int at least 2, usually 4 %d, %i

char 1 %c

float 4 %f

double 8 %lf

short int 2 usually %hd

unsigned int at least 2, usually 4 %u


Type Size (bytes) Format Specifier

long int at least 4, usually 8 %ld, %li

long long int at least 8 %lld, %lli

unsigned long int at least 4 %lu

unsigned long long int at least 8 %llu

signed char 1 %c

unsigned char 1 %c

long double at least 10, usually 12 or 16 %Lf

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

// invalid code: unsigned int cannot hold negative integers


unsigned int num = -35;
signed and unsigned
In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by
using them:
 signed - allows for storage of both positive and negative numbers or it is used to stored
both +ve and -ve number in c
 unsigned - allows for storage of only positive numbers or it is used to stored only -ve
numbers in c

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

The data types that are inbuilt data types in c


Primary Data
Types
Primitive Data Types:- The data types that are inbuilt data types or predefined data types in c
ex:- float , int , char etc.

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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy