• OVERVIEW OF C
• C PROGRAM SKELETON
• C PROGRAM STRUCTURE
• HEADER FILES
• MAIN FUNCTION
• RUNNING A C PROGRAM
• COMMANDS IN C
• VARIABLE NAME RULES
• INPUT/OUTPUT
• OPERATORS
• CHARACTERS USED IN C
• SPECIAL CHARACTERS
• KEYWORDS
• IDENTIFIERS
• ESCAPE SEQUENCE
• VARIABLES
• BASIC DATA TYPES
• CONSTANTS
• I/O OPERATIONS
• I/O FUNCTIONS
• PROGRAMMING ERRORS
• NOTES
• BIBLIOGRAPHY
• END PAGE
• C is developed by Dennis Ritchie
• C is a structured programming language
• C supports functions that enables easy
maintainability of code, by breaking large file
into smaller modules
• Comments in C provides easy readability
• C is a powerful language
• In short, the basic skeleton of a C program
looks like this:
#include <stdio.h>
void main()
{
statement(s);
}
Preprocessor directives
Main function
Start of
segment(program)
End of
segment(program)
Simple C Program
#include<stdio.h>
void main()
{
--other statements
// Comments after double slash
}
• The files that are specified in the include
section are known as header file
• These are precompiled files that has some
functions defined in them
• We can call those functions in our program
by supplying parameters
• Header file is given an extension .h
• C Source file is given an extension .c
• This is the entry point of a program
• When a file is executed, the start point is the
main function
• From main function the flow goes as per the
programmers choice.
• There may or may not be other functions
written by user in a program
• Main function is compulsory for any c program
• Type a program
• Save it
• Compile the program – This will generate an exe file
(executable)
• Run the program (Actually the exe created out of
compilation will run and not the .c file)
• In different compiler we have different option for
compiling and running. We give only the concepts.
• Single line comment
– // (double slash)
– Termination of comment is by pressing enter key
• Multi line comment
/*….
…….*/
This can span over to multiple lines
• Should not be a reserved word like int,struct
etc..
• Should start with a letter or an underscore(_)
• Can contain letters, numbers or underscore.
• No other special characters are allowed
including space
• Variable names are case sensitive
– A and a are different.
• Input
– scanf(“%d”,&a);
– Gets an integer value from the user and stores it
under the name “a”
• Output
– printf(“%d”,a);
– Prints the value present in variable a on the
screen
• Arithmetic (+,-,*,/,%)
• Relational (<,>,<=,>=,==,!=)
• Logical (&&,||,!)
• Bitwise (&,|)
• Assignment (=)
• Compound assignment(+=,*=,-=,/=,%=,&=,|=)
• Shift (right shift >>, left shift <<)
C language uses the following keywords which are not available to users to
use them as variables/function names. Keywords are always written with
the lower case letters. Some C compilers use more keywords which they
include in their documentation or manual pages.
An identifier is a name having a few letters , numbers and special character
_(underscore). It is used to identify a variable, function, symbolic constant
and so on. An identifier can be written with a maximum of 31 characters.
Example:
x2 , sum ,avg (can be used as variables)
calcu_avg , matadd (can be used as function names)
pi, sigma (can be used as symbolic constants)
Escape Sequence Effect
a Beep sound
b Backspace
f Formfeed (for printing)
n New line
r Carriage return
t Tab
v Vertical tab
 Backslash
” “ sign
o Octal decimal
x Hexadecimal
O NULL
• Variable  a name associated with a memory
cell whose value can be change
• Variable Declaration: specifies the type of a
variable
– Example: int num;
• Variable Definition: assigning a value to the
declared variable
– Example: num = 5;
• There are 4 basic data types :
– int
– float
– double
– char
• int
– used to declare numeric program variables of integer type
– whole numbers, positive and negative
– keyword: int
int number;
number = 12;
• float
– fractional parts, positive and negative
– keyword: float
float height;
height = 1.72;
• double
– used to declare floating point variable of higher precision
or higher range of numbers
– exponential numbers, positive and negative
– keyword: double
double valuebig;
valuebig = 12E-3;
• char
– equivalent to ‘letters’ in English language
– Example of characters:
• Numeric digits: 0 - 9
• Lowercase/uppercase letters: a - z and A - Z
• Space (blank)
• Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
– single character
– keyword: char
char my_letter;
my_letter = 'U';
• In addition, there are void, short, long, etc.
• Entities that appear in the program code as fixed values.
• Any attempt to modify a CONSTANT will result in error.
• 3 types of constants:
– Integer constants
• Positive or negative whole numbers with no fractional part
• Example:
– const int MAX_NUM = 10;
– const int MIN_NUM = -90;
– Floating-point constants (float or double)
• Positive or negative decimal numbers with an integer part, a
decimal point and a fractional part
• Example:
– const double VAL = 0.5877e2; (stands for 0.5877 x
102)
– Character constants
• A character enclosed in a single quotation mark
• Example:
– const char letter = ‘n’;
– const char number = ‘1’;
– printf(“%c”, ‘S’);
» Output would be: S
#include <stdio.h>
void main()
{
const double pi = 3.412; //hence pi value cant be changed
double h, r, b, volume;
printf(“Enter the height and radius of the cone:”);
scanf(“%lf %lf”,&h, &r);
b = pi * r * r;
volume = (1.0/3.0) * b * h;
printf(“nThe volume of a cone is :%f ”, volume);
}
• Input operation
– an instruction that copies data from an input
device into memory
• Output operation
– an instruction that displays information stored in
memory to the output devices (such as the
monitor screen)
• A C function that performs an input or output
operation
• A few functions that are pre-defined in the
header file stdio.h such as :
– printf()
– scanf()
– getchar() & putchar()
– gets() & puts()
• Used to send data to the standard output (usually
the monitor) to be printed according to specific
format.
• General format:
– printf(“string literal”);
• A sequence of any number of characters surrounded by
double quotation marks.
– printf(“format string”, variables);
• Format string is a combination of text, conversion
specifier and escape sequence.
• Example:
– printf(“Thank you”);
– printf (“Total sum is: %dn”, sum);
• %d is a placeholder (conversion specifier)
– marks the display position for a type integer variable
• n is an escape sequence
– moves the cursor to the new line
• Read data from the standard input device
(usually keyboard) and store it in a variable.
• General format:
– scanf(“Format string”, &variable);
• Notice ampersand (&) operator :
– C address of operator
– it passes the address of the variable instead of the
variable itself
– tells the scanf() where to find the variable to store
the new value
• Example :
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
• Common Conversion Identifier used in printf and
scanf functions.
• Debugging  Process removing errors from a
program
• Three kinds of errors :
– Syntax Error
• a violation of the C grammar rules, detected during
program translation (compilation).
• statement cannot be translated and program
cannot be executed
– Run-time errors
• An attempt to perform an invalid operation,
detected during program execution.
• Occurs when the program directs the computer to
perform an illegal operation, such as dividing a
number by zero.
• The computer will stop executing the program, and
displays a diagnostic message indicates the line
where the error was detected
– Logic Error/Design Error
• An error caused by following an incorrect algorithm
• Very difficult to detect - it does not cause run-time
error and does not display message errors.
• The only sign of logic error – incorrect program output
• Can be detected by testing the program thoroughly,
comparing its output to calculated results
• To prevent – carefully desk checking the algorithm and
written program before you actually type it
• C is case-sensitive
– Word, word, WorD, WORD, WOrD, worD, etc are all different
variables / expressions
Eg. sum = 23 + 7
• Comments
– are inserted into the code using /* to start and */ to end a
comment
– Some compiler supports comments starting with ‘//’
– Provides supplementary information but is ignored by the
preprocessor and compiler
• /* This is a comment */
• // This program was written by Hanly Koffman
• Reserved Words
– Keywords that identify language entities such as
statements, data types, language attributes, etc.
– Have special meaning to the compiler, cannot be
used as identifiers (variable, function name) in our
program.
– Should be typed in lowercase.
– Example: const, double, int, main, void,printf,
while, for, else (etc..)
• Punctuators (separators)
– Symbols used to separate different parts of the C
program.
– These punctuators include:
[ ] ( ) { } , ; “: * #
– Usage example:
• Operators
– Tokens that result in some kind of computation or
action when applied to variables or other
elements in an expression.
– Example of operators:
• * + = - /
– Usage example:
• result = total1 + total2;
• Google
• Wikipedia
• Reference books
Cpu

Cpu

  • 2.
    • OVERVIEW OFC • C PROGRAM SKELETON • C PROGRAM STRUCTURE • HEADER FILES • MAIN FUNCTION • RUNNING A C PROGRAM • COMMANDS IN C • VARIABLE NAME RULES • INPUT/OUTPUT • OPERATORS • CHARACTERS USED IN C
  • 3.
    • SPECIAL CHARACTERS •KEYWORDS • IDENTIFIERS • ESCAPE SEQUENCE • VARIABLES • BASIC DATA TYPES • CONSTANTS • I/O OPERATIONS • I/O FUNCTIONS • PROGRAMMING ERRORS • NOTES • BIBLIOGRAPHY • END PAGE
  • 4.
    • C isdeveloped by Dennis Ritchie • C is a structured programming language • C supports functions that enables easy maintainability of code, by breaking large file into smaller modules • Comments in C provides easy readability • C is a powerful language
  • 5.
    • In short,the basic skeleton of a C program looks like this: #include <stdio.h> void main() { statement(s); } Preprocessor directives Main function Start of segment(program) End of segment(program)
  • 6.
    Simple C Program #include<stdio.h> voidmain() { --other statements // Comments after double slash }
  • 7.
    • The filesthat are specified in the include section are known as header file • These are precompiled files that has some functions defined in them • We can call those functions in our program by supplying parameters • Header file is given an extension .h • C Source file is given an extension .c
  • 8.
    • This isthe entry point of a program • When a file is executed, the start point is the main function • From main function the flow goes as per the programmers choice. • There may or may not be other functions written by user in a program • Main function is compulsory for any c program
  • 9.
    • Type aprogram • Save it • Compile the program – This will generate an exe file (executable) • Run the program (Actually the exe created out of compilation will run and not the .c file) • In different compiler we have different option for compiling and running. We give only the concepts.
  • 10.
    • Single linecomment – // (double slash) – Termination of comment is by pressing enter key • Multi line comment /*…. …….*/ This can span over to multiple lines
  • 11.
    • Should notbe a reserved word like int,struct etc.. • Should start with a letter or an underscore(_) • Can contain letters, numbers or underscore. • No other special characters are allowed including space • Variable names are case sensitive – A and a are different.
  • 12.
    • Input – scanf(“%d”,&a); –Gets an integer value from the user and stores it under the name “a” • Output – printf(“%d”,a); – Prints the value present in variable a on the screen
  • 13.
    • Arithmetic (+,-,*,/,%) •Relational (<,>,<=,>=,==,!=) • Logical (&&,||,!) • Bitwise (&,|) • Assignment (=) • Compound assignment(+=,*=,-=,/=,%=,&=,|=) • Shift (right shift >>, left shift <<)
  • 16.
    C language usesthe following keywords which are not available to users to use them as variables/function names. Keywords are always written with the lower case letters. Some C compilers use more keywords which they include in their documentation or manual pages.
  • 17.
    An identifier isa name having a few letters , numbers and special character _(underscore). It is used to identify a variable, function, symbolic constant and so on. An identifier can be written with a maximum of 31 characters. Example: x2 , sum ,avg (can be used as variables) calcu_avg , matadd (can be used as function names) pi, sigma (can be used as symbolic constants)
  • 19.
    Escape Sequence Effect aBeep sound b Backspace f Formfeed (for printing) n New line r Carriage return t Tab v Vertical tab Backslash ” “ sign o Octal decimal x Hexadecimal O NULL
  • 20.
    • Variable a name associated with a memory cell whose value can be change • Variable Declaration: specifies the type of a variable – Example: int num; • Variable Definition: assigning a value to the declared variable – Example: num = 5;
  • 21.
    • There are4 basic data types : – int – float – double – char • int – used to declare numeric program variables of integer type – whole numbers, positive and negative – keyword: int int number; number = 12;
  • 22.
    • float – fractionalparts, positive and negative – keyword: float float height; height = 1.72; • double – used to declare floating point variable of higher precision or higher range of numbers – exponential numbers, positive and negative – keyword: double double valuebig; valuebig = 12E-3;
  • 23.
    • char – equivalentto ‘letters’ in English language – Example of characters: • Numeric digits: 0 - 9 • Lowercase/uppercase letters: a - z and A - Z • Space (blank) • Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc – single character – keyword: char char my_letter; my_letter = 'U'; • In addition, there are void, short, long, etc.
  • 24.
    • Entities thatappear in the program code as fixed values. • Any attempt to modify a CONSTANT will result in error. • 3 types of constants: – Integer constants • Positive or negative whole numbers with no fractional part • Example: – const int MAX_NUM = 10; – const int MIN_NUM = -90; – Floating-point constants (float or double) • Positive or negative decimal numbers with an integer part, a decimal point and a fractional part • Example: – const double VAL = 0.5877e2; (stands for 0.5877 x 102)
  • 25.
    – Character constants •A character enclosed in a single quotation mark • Example: – const char letter = ‘n’; – const char number = ‘1’; – printf(“%c”, ‘S’); » Output would be: S
  • 26.
    #include <stdio.h> void main() { constdouble pi = 3.412; //hence pi value cant be changed double h, r, b, volume; printf(“Enter the height and radius of the cone:”); scanf(“%lf %lf”,&h, &r); b = pi * r * r; volume = (1.0/3.0) * b * h; printf(“nThe volume of a cone is :%f ”, volume); }
  • 27.
    • Input operation –an instruction that copies data from an input device into memory • Output operation – an instruction that displays information stored in memory to the output devices (such as the monitor screen)
  • 28.
    • A Cfunction that performs an input or output operation • A few functions that are pre-defined in the header file stdio.h such as : – printf() – scanf() – getchar() & putchar() – gets() & puts()
  • 29.
    • Used tosend data to the standard output (usually the monitor) to be printed according to specific format. • General format: – printf(“string literal”); • A sequence of any number of characters surrounded by double quotation marks. – printf(“format string”, variables); • Format string is a combination of text, conversion specifier and escape sequence.
  • 30.
    • Example: – printf(“Thankyou”); – printf (“Total sum is: %dn”, sum); • %d is a placeholder (conversion specifier) – marks the display position for a type integer variable • n is an escape sequence – moves the cursor to the new line
  • 31.
    • Read datafrom the standard input device (usually keyboard) and store it in a variable. • General format: – scanf(“Format string”, &variable); • Notice ampersand (&) operator : – C address of operator – it passes the address of the variable instead of the variable itself – tells the scanf() where to find the variable to store the new value
  • 32.
    • Example : intage; printf(“Enter your age: “); scanf(“%d”, &age); • Common Conversion Identifier used in printf and scanf functions.
  • 33.
    • Debugging Process removing errors from a program • Three kinds of errors : – Syntax Error • a violation of the C grammar rules, detected during program translation (compilation). • statement cannot be translated and program cannot be executed
  • 34.
    – Run-time errors •An attempt to perform an invalid operation, detected during program execution. • Occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. • The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected
  • 35.
    – Logic Error/DesignError • An error caused by following an incorrect algorithm • Very difficult to detect - it does not cause run-time error and does not display message errors. • The only sign of logic error – incorrect program output • Can be detected by testing the program thoroughly, comparing its output to calculated results • To prevent – carefully desk checking the algorithm and written program before you actually type it
  • 36.
    • C iscase-sensitive – Word, word, WorD, WORD, WOrD, worD, etc are all different variables / expressions Eg. sum = 23 + 7 • Comments – are inserted into the code using /* to start and */ to end a comment – Some compiler supports comments starting with ‘//’ – Provides supplementary information but is ignored by the preprocessor and compiler • /* This is a comment */ • // This program was written by Hanly Koffman
  • 37.
    • Reserved Words –Keywords that identify language entities such as statements, data types, language attributes, etc. – Have special meaning to the compiler, cannot be used as identifiers (variable, function name) in our program. – Should be typed in lowercase. – Example: const, double, int, main, void,printf, while, for, else (etc..)
  • 38.
    • Punctuators (separators) –Symbols used to separate different parts of the C program. – These punctuators include: [ ] ( ) { } , ; “: * # – Usage example:
  • 39.
    • Operators – Tokensthat result in some kind of computation or action when applied to variables or other elements in an expression. – Example of operators: • * + = - / – Usage example: • result = total1 + total2;
  • 40.