1 - Introduction To C Programming
1 - 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
• Phases of C Programs:
on disk.
1. Edit
processes the code.
Compiler creates
2. Preprocess
Compiler Disk object code and stores
it on disk.
Linker links the object
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
• = (assignment operator )
– Assigns value to a variable
– Binary operator (has two operands)
sum = variable1 + variable2;
– Variable receiving value on left
• 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
• 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.
sum = a+b+c;
avg = sum/3; or avg = (float) sum/3;
printf(“%d”, avg);
}