Professional Documents
Culture Documents
PROGRAMMING IN C
C: History
2
Libraries
Linker
Executable code
a.out
Preprocessor Compiler
A first program: Text rearranger
8
Input
First line: pairs of nonnegative integers, separated by
whitespace, then terminated by a negative integer
x1 y1 x2 y2 … xn yn -1
Each subsequent line: a string of characters
Output
For each string S, output substrings of S:
First, the substring starting at location x1 and ending at y1;
Next, the substring starting at location x2 and ending at y2;
…
Finally, the substring starting at location xn and ending at xn.
Use of comments
9 /*
** This program reads input lines from the standard input and prints
** each input line, followed by just some portions of the lines, to
** the standard output.
**
** The first input is a list of column numbers, which ends with a
** negative number. The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, 0 3 10 12 -1 indicates that only columns 0 through 3
** and columns 10 through 12 will be printed.
*/
}
This will not!
*/
Anyway, commenting out code is confusing, and dangerous (easy to forget
about) – avoid it
A Simple C Program
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 ({ and }) indicate a block
The bodies of all functions must be contained in braces
2.2 A Simple C Program:
Printing a Line of Text
Return 0;
A way to exit a function
Return 0, in this case, means that the program
terminated normally
Preprocessor directives
13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
The #include directives “paste” the contents of the files stdio.h, stdlib.h and
string.h into your source code, at the very place where the directives appear.
These files contain information about some library functions used in the
program:
stdio stands for “standard I/O”, stdlib stands for “standard library”, and
#define MAX_COLS 20
#define MAX_INPUT 1000
These look like function definitions – they have the name and all the type
information – but each ends abruptly with a semicolon. Where’s the body
of the function – what does it actually do?
(Note that each function does have a real definition, later in the program.)
Q: Why are these needed, if the functions are defined later in the program
anyway?
A: C programs are typically arranged in “top-down” order, so functions are
used (called) before they’re defined.
(Note that the function main() includes a call to
read_column_numbers().)
When the compiler sees a call to read_column_numbers() , it must check
whether the call is valid (the right number and types of parameters, and
the right return type).
But it hasn’t seen the definition of read_column_numbers() yet!
The prototype gives the compiler advance information about the function
that’s being called.
Of course, the prototype and the later function definition must match in
terms of type information.
The main() function
17
int
main( void )
{…
return EXIT_SUCCESS;
C is a high-level language.
Keywords
char, static, if , while, return ..................... Total= about 32
Data Types
int , char, float ...………..….. Some more later
Arithmetic Operators
+ (Plus), - (Minus), * (Multiplication), /(Division)
……….………. Some more later
#include <stdio.h>
// program prints hello world
int main() {
printf ("Hello world!");
return 0;
}
#include <stdio.h>
// program prints a number of type int
int main() {
int number = 4;
printf (“Number is %d”, number);
return 0;
}
Output: Number is 4
#include <stdio.h>
// program reads and prints the same thing
int main() {
int number ;
printf (“ Enter a Number: ”);
scanf (“%d”, &number);
printf (“Number is %d\n”, number);
return 0;
}
#include <stdio.h>
int main() {
/* this program adds
two numbers */
int a = 4; //first number
int b = 5; //second number
int answer = 0; //result
answer = a + b;
}
Errors
Compilation
Compiler generally gives the line number at
which the error is present.
Run time
C programs are sequential making the debugging
easier.
User Defined:
typedef: used to rename a data type
typedef int integer; can use integer to declare an int.
enum, struct, union
C Course, Programming club, Fall
2008
Some more Arithmetic Operators
52
Modulus (remainder): %
example:
12%5 = 2;
Assignment by addition: +=
example:
int a=4;
a+=1; //(means a=a+1) value of a becomes 5
Meaning of a + b * c ?
is it a+(b*c) or (a+b)*c ?
All operators have precedence over each other
*, / have more precedence over +, - .
If both *, / are used, associativity comes into
picture. (more on this later)
example :
5+4*3 = 5+12= 17.
Highest on top
++ -- (Postfix)
++ -- (Prefix)
* / %
+ -
<< >>
< >
&
|
&&
||
& in scanf.
It is used to access the address of the variable used.
example:
scanf(%d,&a);
we are reading into the address of a.
Data Hierarchy.
example:
int value can be assigned to float not vice-versa.
Type casting.
Meaning of
Syntax
Semantics of a programming language
Find the Output:
value=value++ + value++;
Value=++value + ++value;
value=value++ + ++value;
THANK YOU