C Programming
DAY 1
When, How and Why?
 C is a general-purpose, high-level language that was originally developed by
Dennis M. Ritchie to develop the UNIX operating system at Bell Labs.
 First implemented on the DEC PDP-11 computer in 1972.
 Easy to learn
 Structured language
 It produces efficient programs
 It can handle low-level activities
 It can be compiled on a variety of computer platforms
Where?
Mostly used for:
 Operating Systems
 Language Compilers
 Assemblers
 Text Editors
 Print Spoolers
 Network Drivers
 Modern Programs
 Databases
 Language Interpreters
 Utilities
What do we need to write C pgms?
 A C compiler.
What is a compiler?
 The source code written in source file is the human readable source for your
program.
 It needs to be "compiled", into machine language so that your CPU can actually
execute the program as per the instructions given.
 The compiler compiles the source codes into final executable programs.
C Program Structure
#include <stdio.h>
int main()
{
/* my first program in C */
clrscr();
printf("Hello, World! n");
return 0;
}
C Program Structure [contd.]
A C program basically consists of the
following sections −
 Preprocessor Commands
 Global Declarations
 The main functions
 Local Declarations
 Program statements & Expressions
 User defined functions
A C program basically consists of the
following parts −
 Preprocessor Commands
 Functions
 Variables
 Statements & Expressions
 Comments
C Pgm for Hello World
Basic Syntax
Tokens
 Token is either a keyword, a constant, a
string literal, or a symbol.
 For example, consider
printf("Hello, World! n");
 Individual tokens are:
printf
(
"Hello, World! n“
)
;
Semicolons
 Semicolon is a statement terminator.
 It indicates the end of one logical entity.
Comments
 Help user understand the program
 Ignored by the compiler
 Represented as /*--------*/ or by //
 You cannot have comments within
comments.
Basic Syntax [contd.]
Keywords – reserved words in C
auto else long switch
break enum register typedef
case extern return union
char float short unsigned
const for signed void
continue goto sizeof volatile
default if static while
do int struct _Packed
double
Basic Syntax [contd.]
Whitespace/Blank-space in C
 A line containing only whitespace is known as a blank line, and a C compiler totally ignores it.
 Used in C to describe blanks, tabs, newline characters and comments.
 separates one part of a statement from another and enables the compiler to identify where one
element in a statement ends and the next element begins.
 Example:
int age; //space required
a = b + c; //space not required
Data Types
 To identify the type of a variable when it is
declared
 To identify the type of return value of a
function
 To identify the type of a parameter
expected by a function
Data Types in C
Fundamental
Void, int, float, char,
double, pointer…
Derived
Array, String,
Structure
Data Types [contd.]
 To get the exact size of a type or a variable on a particular platform, you can use
the sizeof operator.
#include <stdio.h>
#include <limits.h>
int main()
{
printf("Storage size for int : %d n", sizeof(int));
return 0;
}
Variables in C
 A variable is a name given to a storage area that our programs can manipulate.
 Each variable in C has a specific type, which determines the size and layout of the
variable's memory;
the range of values that can be stored within that memory;
and the set of operations that can be applied to the variable.
Naming a Variable
 name of a variable can be composed of letters, digits, and the underscore
character.
 It must begin with either a letter or an underscore.
 Upper and lowercase letters are distinct because C is case-sensitive.
Variables in C[contd.]
Defining a variable = creating a variable
 Syntax: vartype varname;
OR vartype varname1, varname2,…….., varname n;
ex: int a;
float a_123;
char _abc;
int a, m123, abc, myVariable, myvariable;
Initializing a variable = assigning a value to the variable;
 a=1;
a_123=12.56;
abc = “Z”;
myvariable=24;
Define+ Initialize a variable
 Int a=1;
float a_123=12.56;
char abc = “Z”;
int myvariable=24;
Constants in C
 They are variables with a
constant value throughout
the program
 Also called as literals.
 Use CONST keyword to
define :
CONST int side = 10;
Operators in C
 A symbol to perform
mathematical or logical
operations
Operators in C [contd.]
Operators in C
Preprocessors & Macros
 A C Preprocessor is just a text substitution
tool
 It instructs the compiler to do required pre-
processing before the actual compilation.
 Begins with a # symbol
 #include<stdio.h>
#define c 2.99 /*speed of light in m/s*/
C pgm to find area of circle using preprocessors.
C pgm to find area of circle, passing arguments to
macros.
C pgm to display date, time, filename etc.
C pgm for type casting
Predefined Macro Value
__DATE__
String containing the current
date "MMM DD YYYY"
__FILE__ String containing the file
__LINE__
Integer representing the
current line number
__TIME__
String containing the current
time "HH:MM:SS"

Computer programming(CP)

  • 1.
  • 2.
    When, How andWhy?  C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs.  First implemented on the DEC PDP-11 computer in 1972.  Easy to learn  Structured language  It produces efficient programs  It can handle low-level activities  It can be compiled on a variety of computer platforms
  • 3.
    Where? Mostly used for: Operating Systems  Language Compilers  Assemblers  Text Editors  Print Spoolers  Network Drivers  Modern Programs  Databases  Language Interpreters  Utilities
  • 4.
    What do weneed to write C pgms?  A C compiler. What is a compiler?  The source code written in source file is the human readable source for your program.  It needs to be "compiled", into machine language so that your CPU can actually execute the program as per the instructions given.  The compiler compiles the source codes into final executable programs.
  • 5.
    C Program Structure #include<stdio.h> int main() { /* my first program in C */ clrscr(); printf("Hello, World! n"); return 0; }
  • 6.
    C Program Structure[contd.] A C program basically consists of the following sections −  Preprocessor Commands  Global Declarations  The main functions  Local Declarations  Program statements & Expressions  User defined functions A C program basically consists of the following parts −  Preprocessor Commands  Functions  Variables  Statements & Expressions  Comments C Pgm for Hello World
  • 7.
    Basic Syntax Tokens  Tokenis either a keyword, a constant, a string literal, or a symbol.  For example, consider printf("Hello, World! n");  Individual tokens are: printf ( "Hello, World! n“ ) ; Semicolons  Semicolon is a statement terminator.  It indicates the end of one logical entity. Comments  Help user understand the program  Ignored by the compiler  Represented as /*--------*/ or by //  You cannot have comments within comments.
  • 8.
    Basic Syntax [contd.] Keywords– reserved words in C auto else long switch break enum register typedef case extern return union char float short unsigned const for signed void continue goto sizeof volatile default if static while do int struct _Packed double
  • 9.
    Basic Syntax [contd.] Whitespace/Blank-spacein C  A line containing only whitespace is known as a blank line, and a C compiler totally ignores it.  Used in C to describe blanks, tabs, newline characters and comments.  separates one part of a statement from another and enables the compiler to identify where one element in a statement ends and the next element begins.  Example: int age; //space required a = b + c; //space not required
  • 10.
    Data Types  Toidentify the type of a variable when it is declared  To identify the type of return value of a function  To identify the type of a parameter expected by a function Data Types in C Fundamental Void, int, float, char, double, pointer… Derived Array, String, Structure
  • 11.
    Data Types [contd.] To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. #include <stdio.h> #include <limits.h> int main() { printf("Storage size for int : %d n", sizeof(int)); return 0; }
  • 12.
    Variables in C A variable is a name given to a storage area that our programs can manipulate.  Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. Naming a Variable  name of a variable can be composed of letters, digits, and the underscore character.  It must begin with either a letter or an underscore.  Upper and lowercase letters are distinct because C is case-sensitive.
  • 13.
    Variables in C[contd.] Defininga variable = creating a variable  Syntax: vartype varname; OR vartype varname1, varname2,…….., varname n; ex: int a; float a_123; char _abc; int a, m123, abc, myVariable, myvariable; Initializing a variable = assigning a value to the variable;  a=1; a_123=12.56; abc = “Z”; myvariable=24; Define+ Initialize a variable  Int a=1; float a_123=12.56; char abc = “Z”; int myvariable=24;
  • 14.
    Constants in C They are variables with a constant value throughout the program  Also called as literals.  Use CONST keyword to define : CONST int side = 10;
  • 15.
    Operators in C A symbol to perform mathematical or logical operations
  • 16.
  • 17.
  • 18.
    Preprocessors & Macros A C Preprocessor is just a text substitution tool  It instructs the compiler to do required pre- processing before the actual compilation.  Begins with a # symbol  #include<stdio.h> #define c 2.99 /*speed of light in m/s*/ C pgm to find area of circle using preprocessors. C pgm to find area of circle, passing arguments to macros. C pgm to display date, time, filename etc. C pgm for type casting Predefined Macro Value __DATE__ String containing the current date "MMM DD YYYY" __FILE__ String containing the file __LINE__ Integer representing the current line number __TIME__ String containing the current time "HH:MM:SS"