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

c Programming

C is a procedural and general-purpose programming language developed by Dennis Ritchie in 1972, known for its low-level memory access and simple syntax. It has influenced many modern programming languages and is used in various applications including operating systems, embedded systems, and database management software. The document covers key features of C, including its structure, data types, keywords, comments, and format specifiers.

Uploaded by

mrphy2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c Programming

C is a procedural and general-purpose programming language developed by Dennis Ritchie in 1972, known for its low-level memory access and simple syntax. It has influenced many modern programming languages and is used in various applications including operating systems, embedded systems, and database management software. The document covers key features of C, including its structure, data types, keywords, comments, and format specifiers.

Uploaded by

mrphy2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

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.

It includes low-level memory access, simple syntax, and a clean


style.

After C, several programming languages like PHP, JavaScript,


Java, and many others have borrowed the syntax and the features
directly or indirectly from the C language.

GUI development, IDE development

 Operating Systems { Mac ,IOS , Android ,Window}


 windows and linux kernels
 Assemblers
 Text Editors
 Print Spoolers
 Modern Programs
 Databases
 Utilities

softwares such as photoshop, premier pro, After Effects etc.


In daily life, we use different embedded systems like coffee
machines, microwaves, climate control systems etc

Popular database management software, MySQL was developed


using the C programming language. MATLAB, Mathematica

Tic-Tac-Toe, The Snake game, Sudoku etc. are developed using


the C language.

So many programming languages like Java, Python, PHP etc. get


influenced by this. Even the libraries of Python are developed
using C.

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.

{ Used for indicating the beginning of 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”);

return 0; Used for terminating a C program and it returns 0.

} Used for indicating the end of the main() function.

Including header files:-


In a C program code, at first you have to include a header file. To
execute the program, you must include a header file into your
program code.

Below are some of the header files:-


 stddef.h – Used for defining various types and macros.
 stdint.t – Used for defining exact width integer types.
 stdio.h – Used for defining input and output functions.
 stdlib.h – Used for defining numeric conversion
functions, pseudo-random network generator, memory
allocation.
 string.h – Used for defining string handling functions.
 math.h – Used for defining mathematical functions.

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.

printf() The printf() function is used to print data on the console.

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

Mult Line Comments


1. /*
2. code
3. to be commented
4. */

Tokens in C

Let's understand each token one by one.

Keywords in C

Keywords in C can be defined as the pre-defined or the reserved


words having

Auto double Int Struct

Break else long Switch


Case enum register Typedef

Char extern return Union

Const float short Unsigned

Continue for signed Void

Default goto sizeof Volatile

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

Strings in C are always represented as an array of characters having null


character '\0' at the end of the string

Operators in C

Operators in C is a special symbol used to perform the functions. .


Depending on the number of operands, operators are classified as follows:

Unary Operator

A unary operator is an operator applied to the single operand. For


example: increment operator (++), decrement operator (--), sizeof,
(type)*.

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.

There are two ways of declaring constant:

o Using const keyword : const float pi=3.14


o Using #define pre-processor #define PI 3.14
o Types of constants in C

Constant Example

Integer constant 10, 11, 34, etc.

Floating-point constant 45.6, 67.8, 11.2, etc.

Octal constant 011, 088, 022, etc.

Hexadecimal constant 0x1a, 0x4b, 0x6b, etc.

Character constant 'a', 'b', 'c', etc.

String constant "java", "c++", ".net", etc.

o Special characters in C

o Some special characters are used in C, and they have a special


meaning which cannot be used for another purpose.

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.

o There are the following data types in C language.


Types Data Types

Basic Data Type int, char, float, double

Derived Data Type array, pointer, structure, union

Enumeration Data Type enum

Void Data Type Void

o Basic Data Types


o The basic data types are integer-based and floating-point based. C language supports
both signed and unsigned literals.
o The memory size of the basic data types may change according to 32 or 64-bit
operating system.
o Let's see the basic data types. Its size is given according to 32-bit
architecture.

Data Types Memory Size Range

Char 1 byte −128 to 127

signed char 1 byte −128 to 127

unsigned char 1 byte 0 to 255

Short 2 byte −32,768 to 32,767

signed short 2 byte −32,768 to 32,767

unsigned short 2 byte 0 to 65,535

Int 2 byte −32,768 to 32,767

signed int 2 byte −32,768 to 32,767

unsigned int 2 byte 0 to 65,535


short int 2 byte −32,768 to 32,767

signed short int 2 byte −32,768 to 32,767

unsigned short int 2 byte 0 to 65,535

long int 4 byte -2,147,483,648 to 2,147,483,647

signed long int 4 byte -2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

Float 4 byte

Double 8 byte

long double 10 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 A list of 32 keywords in the c language is given below:

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

o
Differences between Keyword and Identifier
Keyword Identifier

Keyword is a pre-defined word. The identifier is a user-defined word

It must be written in a lowercase It can be written in both lowercase and


letter. uppercase letters.

Its meaning is pre-defined in the Its meaning is not defined in the c


c compiler. compiler.

It is a combination of It is a combination of alphanumeric


alphabetical characters. characters.

It does not contain the It can contain the underscore character.


underscore character.

Let's understand through an example.

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.

The commonly used format specifiers in printf() function are:

Format Description
specifier

%d or %i It is used to print the signed integer value where signed


integer means that the variable can hold both positive and
negative values.

%u It is used to print the unsigned integer value where the


unsigned integer means that the variable can hold only
positive value.

%o It is used to print the octal unsigned integer where octal


integer value always starts with a 0 value.
%x It is used to print the hexadecimal unsigned integer where the
hexadecimal integer value always starts with a 0x value. In
this, alphabetical characters are printed in small letters such
as a, b, c, etc.

%X It is used to print the hexadecimal unsigned integer, but %X


prints the alphabetical characters in uppercase such as A, B,
C, etc.

%f It is used for printing the decimal floating-point values. By


default, it prints the 6 values after '.'.

%e/%E It is used for scientific notation. It is also known as Mantissa or


Exponent.

%g It is used to print the decimal floating-point values, and it uses


the fixed precision, i.e., the value after the decimal in input
would be exactly the same as the value in the output.

%p It is used to print the address in a hexadecimal form.

%c It is used to print the unsigned character.

%s It is used to print the strings.

%ld It is used to print the long-signed integer value.


Let's understand the format specifiers in detail through an
example.

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

In the above program, we are displaying the value of b and c by using an


unsigned format specifier, i.e., %u. The value of b is positive, so %u
specifier prints the exact value of b, but it does not print the value of c as
c contains the negative value.

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

The above code prints the floating value of y.

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

In the above code, we are displaying the floating value of y by using %g


specifier. The %g specifier displays the output same as the input with a
same precision.

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.

The syntax of printf() function is given below:

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

The format string can be %d (integer), %c (character), %s (string), %f


(float) etc.

scanf() function
The scanf() function is used for input. It reads the input data from the
console.

1. scanf("format string",&argument_list);

Program to print cube of given number


Let's see a simple example of c language that gets input from the user
and prints the cube of the given number.

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

The scanf("%d",&number) statement reads integer number from the


console and stores the given value in number variable.

The printf("cube of number is:%d


",number*number*number) statement prints the cube of number on
the console.

Program to print sum of 2 numbers


Let's see a simple example of input and output in C language that prints
addition of 2 numbers.

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

enter first number:9


enter second number:9
sum of 2 numbers:18

You might also like