Unit 1
Unit 1
SOLVING AND C
PROGRAMMING
UNIT-1
PROBLEM SOLVING AND C
FUNDAMENTALS
#include <stdio.h>
int main ()
{
printf(“Welcome to 21CB101!\n”);
}
History of C
1960: ALGOL (ALGOrithmic Language)
1967: BCPL (Basic Combined Programming
Language)
1970: B programming language (typeless)
1972: C: BCPL plus B with types
1978: Kernighan + Ritchie standard for C
1989: ANSI standard for C
C Program Structure
Preprocessor Directives
• Program defined by:
– global declarations
Global Declarations
– function definitions
Function Definitions • May contain preprocessor
int main () {
directives
Local Declarations
• Always has one function
Statements named main, may contain
}
others
Parts of a Program
int main () {
int y; Local Declaration
Function printf("Enter x and y: ");
scanf(&x,&y); Statements
printf("Sum is %d\n",x+y);
}
Preprocessor Directives
• Begin with #
• Instruct compiler to perform some
transformation to file before compiling
• Example: #include <stdio.h>
– add the header file stdio.h to this file
– .h for header file
– stdio.h defines useful input/output functions
Declarations
• Global
– visible throughout program
– describes data used throughout program
• Local
– visible within function
– describes data used only in function
Functions
• Consists of header and body
– header: int main ()
– body: contained between { and }
• starts with location declarations
• followed by series of statements
• More than one function may be defined
• Functions are called (invoked) - more later
Main Function
• Every program has one function main
• Header for main: int main ()
• Program is the sequence of statements
between the { } following main
• Statements are executed one at a time from
the one immediately following to main to
the one before the }
Comments
• Text between /* and */
• Used to “document” the code for the human
reader
• Ignored by compiler (not part of program)
• Have to be careful
– comments may cover multiple lines
– ends as soon as */ encountered (so no internal
comments - /* An /* internal */ comment */)
Comment Example
#include <stdio.h>