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

1 - Introduction To C Programming

C is a programming language invented in the 1970s for use with Unix operating systems. A typical C program is created using an editor, preprocessed, compiled, linked, loaded into memory, and executed by the CPU. The core components of a basic C program are comments, preprocessor directives like #include, functions like main that contain declarations, statements, and return values. Arithmetic operators allow mathematical expressions to be evaluated and printed out.

Uploaded by

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

1 - Introduction To C Programming

C is a programming language invented in the 1970s for use with Unix operating systems. A typical C program is created using an editor, preprocessed, compiled, linked, loaded into memory, and executed by the CPU. The core components of a basic C program are comments, preprocessor directives like #include, functions like main that contain declarations, statements, and return values. Arithmetic operators allow mathematical expressions to be evaluated and printed out.

Uploaded by

anuraggupta85
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to C Programming

Compiled by:
Anurag Gupta
Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 1
Introduction

• C programming language
– C is invented for unix operating system by Dennis Ritchie
– PDP-11 (Program Data Processor ) was the mainframe machine on which C
was invented

• Reference books
– Programming in ANSI C by E-balaguru Swamy
– Let us C by Yashwant Kanitkar
– Exploring C by Yashwant Kanitkar
– Pointers in C by Yashwant Kanitkar
– ANSI C Programming by Dennis Ritchie
– Test your C Skill by Yashwant Kanitkar

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 2


Basics of a Typical C Program
Development Environment
Program is created in
Editor Disk the editor and stored

• Phases of C Programs:
on disk.

Preprocessor Preprocessor program


Disk

1. Edit
processes the code.

Compiler creates

2. Preprocess
Compiler Disk object code and stores
it on disk.
Linker links the object

3. Compile Linker Disk code


libraries,
with the

creates a.exe and


4. Link Loader
Primary
Memory
stores it on disk

5. Load Loader puts program


in memory.

6. Execute Disk ..
..
..

Primary
Memory
CPU
CPU takes each
  instruction and
executes it, possibly
storing new data
.. values as the program
Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam .. 3
.. executes.
A Simple C Program: Printing a Line of
Text
1 // A first program in C
2 #include<stdio.h>
3
4 int main()
5 {
6 printf(“Welcome to C! \n”);
7 return 0;
8 }

Output : Welcome to C!
• Comments
– Text written after // is ignored by computer
– Used to describe program
– There are two types of comments :
• Single Line Comment //
• Multi Line Comment /* Statements */
Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 4
• #include <stdio.h>
– Preprocessor directive - tells computer to load contents of a certain file
– <stdio.h> allows standard input/output operations

• int main()
– C programs contain one or more functions, exactly one of which must be main
– Parenthesis used to indicate a function
– int means that main "returns" an integer value
– Braces indicate a block
• The bodies of all functions must be contained in braces

• Left brace {
– Indicates starting of main

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 5


• printf( "Welcome to C!\n" );
– Instructs computer to perform an action
• Specifically, prints string of characters within quotes
– Entire line called a statement
• All statements must end with a semicolon
– \ escape character
• Indicates that printf should do something out of the ordinary
• \n is the newline character
• return 0;
– A way to exit a function
– return 0, in this case, means that the program terminated normally
• Right brace }
– Indicates end of main has been reached
• Linker
– When a function is called, linker locates it in the library
– Inserts it into object program
– If function name misspelled, linker will spot error because it cannot find function in library
Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 6
Another Simple C Program: Adding Two
Integers
• As before
– Comments, #include <stdio.h> and main
• int integer1, integer2, sum;
– Declaration of variables
• Variables: locations in memory where a value can be stored
– int means the variables can hold integers (-1, 3, 0, 47)
– integer1, integer2, sum - variable names (identifiers)
• Identifiers: consist of letters, digits (cannot begin with a digit), and
underscores, case sensitive
– Declarations appear before executable statements
• If not, syntax (compile) error

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 7


• scanf( "%d", &integer1 );
– Obtains value from user
• scanf uses standard input (usually keyboard)
– This scanf has two arguments
• %d - indicates data should be a decimal integer
• &integer1 - location in memory to store variable
• & is confusing in beginning - just remember to include it with the variable
name in scanf statements
– It will be discussed later
– User responds to scanf by typing in number, then pressing the enter (return)
key

• = (assignment operator )
– Assigns value to a variable
– Binary operator (has two operands)
sum = variable1 + variable2;
– Variable receiving value on left

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 8


• printf( "Sum is %d\n", sum );
– Similar to scanf - %d means decimal integer will be printed
• sum specifies what integer will be printed
– Calculations can be performed inside printf statements
printf( "Sum is %d\n", integer1 + integer2 );

• Variables
Memory Concepts
– Variable names correspond to locations in the computer's memory.
– Every variable has a name, a type, a size and a value.
– Whenever a new value is placed into a variable (through scanf, for example), it
replaces (and destroys) previous value
– Reading variables from memory does not change them
• A visual representation

integer1 45

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 9


Rules for writing variable name

• Variable names should not contain any keyword and must be unique.
• Variable name must start with an alphabet and can contain digits also. The
only special character allowed in the naming of a variable is underscore _ .
• Variable declaration in a C program must appear before the use of any
function call.
• The maximum length of a variable name in C can be 30 or 31 (depending
on compiler) and C++ it is 255.
• White space is not allowed.

• Valid example : area, sum1, phone_no


• Invalid example : 123, (area), 25shape, student name

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 10


Arithmetic operator

C operation Arithmetic Algebraic C expression


operator expression
Addition + f+7 f+7
Subtraction - p–c p-c
Multiplication * bm b*m
Division / x/y x/y
Modulus % r mod s r%s

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 11


Relational operator

Standard algebraic C++ equality Example Meaning of


equality operator or or relational of C++ C++ condition
relational operator operator condition
Relational operators

> > x>y x is greater than y


< < x<y x is less than y
>= x >= y x is greater than or
equal to y
<= x <= y x is less than or equal
to y
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 12


Keywords

• Special words reserved for C


• Cannot be used as identifiers or variable names

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

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 13


Data Types
Data Type Name Number of bytes Format Specifier Range
int or signed int 2 bytes %d -32768 to 32767
unsigned int 2 bytes %u 0 to 65535
long int 4 bytes %ld -2,147,483,648 to
2,147,483,647
unsigned long int 4 bytes %lu 0 to 4,294,967,295
char or signed char 1 byte %c -128 to 127
unsigned char 1 byte %c 0 to 255
float 4 byte %f -3.4 * 1038 to +3.4 * 1038
double 8 byte %lf -3.4 * 10308 to +3.4 * 10308
long double 10 byte %Lf -1.1 * 104932 to +1.1 *
104932

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 14


Type Casting
• Type casting is a way to convert a variable from one data type to another
data type.
Syntax : (type_name) expression
• Example : void main()
{
int a=2,b=3,c=5;
int sum;
float avg;

sum = a+b+c;
avg = sum/3; or avg = (float) sum/3;
printf(“%d”, avg);
}

Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 15


Type Conversion
• Compiler converts any intermediate values to the proper type so that the expression
can be evaluated without any significance. This automatic conversion is known as
type conversion.

• Example 1: void main()


{
int x;
x = 10.7;
printf(“%d”, x);
}
Example2 :void main()
{
float x;
x = 10;
printf(“%f”, x);
}
Compiled By: Anurag Gupta Hyperfocused Stratergies, Ratlam 16

You might also like