Introduction To C Programming: CSE115: Computing Concepts
Introduction To C Programming: CSE115: Computing Concepts
Introduction to C Programming
CSE115: Computing Concepts
General Form of a C Program
preprocessor directives
main function heading
{
declarations
executable statements
}
A Simple Program in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}
A Simple Program in C
#include <stdio.h>
standard Library, input-output, header-file
#include <stdlib.h>
Beginning of program
int main()
Start of Segment
{ Function for printing text
printf("Hello world!\n");
return 0; End of statement
End of Segment
Output
Hello world!
Preprocessor Directives
• They are commands that give instructions to the C preprocessor,
whose job is to modify the text of C before it is compiled.
• It begins with ‘#’ and ends with ‘.h’.
• Many actions, necessary for a computer program are not directly
defined in C. Collections of useful functions and symbols called
‘libraries’.
• Two most common directives :
• #include
• #define
• In our example (#include<stdio.h>) identifies the header
file for standard input and output operations.
• #include<stdlib.h>
Function main()
• Identify the start of the program
• Every C program has a main( )
• 'main' is a C keyword. We must not use it for any other
purpose.
• 4 common ways of main declaration
return 0;
} } } }
The curly braces { }
• Identify a segment / body of a program
• The start and end of a function
• The start and end of the selection or repetition block.
• Since the opening brace indicates the start of a segment
with the closing brace indicating the end of a segment,
there must be just as many opening braces as closing
braces (this is a common mistake of beginners)
Statement
• Specifying an action to be taken by the computer as the program
executes.
• Each statement in C needs to be terminated with semicolon (;)
• Example:
#include <stdio.h>
int main()
{
printf(“I love programming\n”); statement
printf(“You will love it too once ”); statement
printf(“you know the trick\n”); statement
return 0; statement
}
Statement
• Statement has two parts :
• Declaration
• The part of the program that tells the compiler the names of memory
cells in a program
• Executable statements
• Program lines that are converted to machine language instructions and
executed by the computer
• Important Terms: Reserved Words, Standard Identifiers and User defined
identifiers.
• Reserved Words: int, void, return (all small letter)
• Standard Identifiers: scanf, printf (all small letters)
• User defined identifiers: kms, KMS_PER_MILES (consists of letters,
digits and underscores. Can’t begin with digits. Can’t use reserved words or
standard identifiers).
An Example
/*
Converts distance in miles
to kilometres.
*/
#include <stdio.h> //printf, scanf definitions
#define KMS_PER_MILE 1.609 //conversion constant
int main(void) {
float miles, // input – distance in miles
kms; // output – distance in kilometres
return 0;
}
An Example
/*
Converts distance in miles
to kilometres.
*/ standard header file
preprocessor #include <stdio.h> //printf, scanf definitions
directives #define KMS_PER_MILE 1.609 //conversion constant
constant
int main(void) {
float miles, // input – distance in miles
reserved kms; // output – distance in kilometres
words
//Get the distance in miles
variables printf("Enter distance in miles: "); comments
scanf("%f", &miles);
return 0;
} punctuations
An Example
/*
Converts distance in miles
to kilometres.
*/
#include <stdio.h> //printf, scanf definitions
#define KMS_PER_MILE 1.609 //conversion constant
int main(void) {
float miles, // input – distance in miles
declarations
kms; // output – distance in kilometres
return 0;
}
An Example
/*
Converts distance in miles
to kilometres.
*/
#include <stdio.h> //printf, scanf definitions
#define KMS_PER_MILE 1.609 //conversion constant
int main(void) {
float miles, // input – distance in miles
kms; // output – distance in kilometres
-2,147,483,648 to n/a
int 4 bytes 2,147,483,647
Memory
21
A closer look at variables
Memory
a 139
b -5
22
A closer look at variables
Memory
a 139
b -5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 132 bits
23
A closer look at variables
Memory
a 139
b -5
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 132 bits
24
------------------------------------ 2’s complement of 5 ------------------------
A closer look at variables
char c = ‘H’;
0 1 0 0 1 0 0 0 8 bits
A closer look at variables
char c = ‘H’;
0 1 0 0 1 0 0 0 8 bits
?
A closer look at variables
char c = ‘H’;
0 1 0 0 1 0 0 0 8 bits
?
10010002 = 7210
Input/Output Operations
• 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)
Input/Output Functions
• 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()
The printf function
• 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.
The printf function
• Example:
• printf(“Thank you”);
• printf (“Total sum is: %d\n”, 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
Placeholder / Conversion Specifier
No Conversion Output Type Output Example
Specifier
1 %d Signed decimal integer 76
2 %i Signed decimal integer 76
3 %o Unsigned octal integer 134
4 %u Unsigned decimal integer 76
5 %x Unsigned hexadecimal (small letter) 9c
6 %X Unsigned hexadecimal (capital letter) 9C
7 %f Integer including decimal point 76.0000
8 %e Signed floating point (using e 7.6000e+01
notation)
9 %E Signed floating point (using E 7.6000E+01
notation)
10 %g The shorter between %f and %e 76
11 %G The shorter between %f and %E 76
12 %c Character ‘7’
13 %s String ‘76'
Escape Sequence
Escape Sequence Effect
\a Beep sound
\b Backspace
\n New line
\t Tab
\v Vertical tab
\\ Backslash
\” “ sign
Formatting output
int meters = 21, feet = 68 , inches =
11;
printf("Results:%3d meters=%4d ft.%2d
in.\n", meters, feet, inches);
R e s u l t s : 2 1 m e t e r s = 6 8 f t . 1 1 i n .
printf scanf
int %d %d
float %f %f
double %lf %lf
char %c %c
string %s %s
The scanf function
• If you want the user to enter more than one value, you
serialize the inputs.
• Example:
float height, weight;