SlideShare a Scribd company logo
A First C Program
Try#include <stdio.h> C/*Program */
Your First
I/O header file
pre-processor directive

header file – contains I/O routines

main()
Indicates a {
program
building
block called
function

comment

main must be present in each C program

one statement

printf(“Hello world ”);

statement terminator

printf(“Welcome to CSCI230n“);
printf(“I am John Smithn”);

}

A C program contains one or more functions
 main() is the function name of your main (root) program
 { }: braces (left & right) to construct a block containing the statements of a
function
 Every statement must end with a ;
  is called an escape character
 n is an example of an escape sequence which indicates newline
 Other escape sequences are: t r a  ”
Exercise: Use any editor to type and then save your first program as main.c
% gcc main.c
% a.out
and observe its result.



Variable identifiers
Identifiers


Begin with a letter or underscore: A-Z, a-z, _



The rest of the name can be letters, underscore, or digits



Guarantee that east least the first 8 characters are significant (those
come after the 8th character will be ignored) while most of C
compiler allows 32 significant characters.
Example:
_abc ABC

Time time

_a1 abcdefgh

abcdefghi (may be the same as abcdefgh)



Case sensitive



Keywords: reserved names (lexical tokens)
auto double if static break
case entry

long

typedef float

else

switch char

return union

int struct

extern register

do go sizeof continue

…


Fundamental Data Type

Four Data Types (assume 2’s complement, byte machine)
Data Type

Size
(byte)

Range

char

1

-128 ~ 127

unsigned char

char

Abbreviation

1

0 ~ 255

2 or 4

-215 ~ 215-1 or -231 ~ 231-1

2 or 4

0 ~ 65535 or 0 ~ 232-1

int
unsigned int

short int

short

2

-32768 ~ 32767

unsigned short int

unsigned short

2

0 ~ 65535

long int

long

4

-231 ~ 231-1

unsigned long int

int

unsigned

unsigned long

4

0 ~ 232-1

float
double

Note:

4
8

27 = 128, 215 =32768, 231 = 2147483648
Complex and double complex are not available
Variable Declarations

type v1,v2,v3, …, vn
Example:

int i;
int j;
float k;
char c;
short int x;
long int y;
unsigned int z;
int a1, a2, a3, a4, a5;


Numeric, Char, String Literals

Literal


Numeric literal


fixed-point



octal

O32 (= 24D) (covered later)



hexadecimal

OxFE or Oxfe (=254D) (covered later)



decimal int

32



long (explicit)

32L or 32l



an ordinary integer literal that is too long to fit in an int is also too long for long



floating-point


No single precision is used; always use double for literal

Example:
1.23
123.456e-7

0.12E
Numeric, Char, String Literals

• Character literal (covered later)

• American Standard Code for Information Interchange (ASCII)

single space

32

‘0’ - ‘9’

48 - 57

‘A’ - ‘Z’

65 - 90

‘a’ - ‘z’

• Printable:

97 - 122

• Nonprintable and special meaning chars
„n‟

new line

„‟ back slash
„0‟ null

9
0

„f‟ formfeed

10

„t‟ tab

„‟‟ single quote 39
„b‟ back space
12

8

‟r‟ carriage return

„”‟ double quote 34

„ddd‟ arbitrary bit pattern using 1-3 octal digits
„Xdd‟ for Hexadecimal mode
„017‟ or „17‟ Shift-Ins, ^O
„04‟ or „4‟ or „004‟
„033‟ or „X1B‟

9

<esc>

EOT (^D)

13
Numeric, Char, String Literals


String Literal


will be covered in Array section



String is a array of chars but ended by „0‟



String literal is allocated in a continuous memory space of
Data Segment, so it can not be rewritten
A B C D „0‟ ...
Example:

“ABCD”

4 chars but takes 5 byte spaces in memory

Question: “I am a string” takes ? Bytes
Ans: 13+1 = 14 bytes
Numeric,literals & ASCII codes:
• Character Char, String Literals
char x;
x=„a‟;

/* x = 97*/

Notes:
– „a‟ and “a” are different; why?
„a‟ is the literal 97
“a” is an array of character literals, { „a‟, „0‟} or {97, 0}
– “a” + “b” +”c” is invalid but „a‟+‟b‟+‟c‟ = ? (hint: „a‟ = 97 in ASCII)
„a‟ + „b‟ + „c‟ = 97 + 98 + 99 = 294 = 256 + 38
in the memory

1

38

– if the code used is not ASCII code, one should check out each
value of character
Initialization


If a variable is not initialized, the value of variable may
be either 0 or garbage depending on the storage class of
the variable.

int i=5;
float x=1.23;
char c=„A‟;

int i=1, j,k=5;
char c1 = „A‟, c2 = 97;
float x=1.23, y=0.1;


Memory Concepts

Each variable has a name, address, type, and value
1)

int x;

2)

scanf(“%d”, &x);

3)

user inputs

4)

x = 200;

10

After the execution of (1)
After the execution of (2)

x

After the execution of (3)

x

After the execution of (4)

x

x

Previous value of x was overwritten

10
200
Sample Problem



Write a program to take two numbers as input
data and print their sum, their difference,
their product and their quotient.
Problem Inputs
float x, y;
/*
problem Output
float sum;
/*
float difference;
x and y */
float product; /*
y */
float quotient; /*
divided by y */

two items */
sum of x and y */
/* difference of
product of x and
quotient of x
Sample Problem


(cont.)

Pseudo Code:

Declare variables of x and y;

Prompt user to input the value of x
and y;
Print the sum of x and y;
Print the difference of x and y;
Print the product of x and y;
If y not equal to zero, print the
quotient of x divided by y
Example Program
#include <stdio.h>
int main(void)
{
float x,y;
float sum;

function
• name
• list of argument along with their types
• return value and its type
• Body

printf(“Enter the value of x:”);
scanf(“%f”, &x);
printf(“nEnter the value of y:”);
scanf(“%f”, &y);
sum = x + y;
printf(“nthe sum of x and y is:%f”,sum);
printf(“nthe sum of x and y is:%f”,x+y);
printf(“nthe difference of x and y is:%f”,x-y);
printf(“nthe product of x and y is:%f”,x*y);
if (y != 0)
printf(“nthe quotient of x divided by y is:%f”,x/y);
else
printf(“nquotient of x divided by y does not exist!n”);
return(0);
}

inequality operator

More Related Content

What's hot (19)

PPTX
Input output statement in C
Muthuganesh S
 
PPTX
Introduction to C programming
Sabik T S
 
PPTX
What is c
Nitesh Saitwal
 
PPTX
Intro to c chapter cover 1 4
Hazwan Arif
 
PPT
CPU INPUT OUTPUT
Aditya Vaishampayan
 
PPTX
Managing input and output operations in c
niyamathShariff
 
PPTX
COM1407: Input/ Output Functions
Hemantha Kulathilake
 
PPTX
Introduction to Basic C programming 02
Wingston
 
PPTX
C formatted and unformatted input and output constructs
GopikaS12
 
PDF
C programing Tutorial
Mahira Banu
 
PDF
C programming Workshop
neosphere
 
DOCX
Important C program of Balagurusamy Book
Abir Hossain
 
PPT
C language basics
Nikshithas R
 
PDF
CP Handout#7
trupti1976
 
PPT
Mesics lecture 5 input – output in ‘c’
eShikshak
 
PPT
Basic concept of c++
shashikant pabari
 
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
Input output statement in C
Muthuganesh S
 
Introduction to C programming
Sabik T S
 
What is c
Nitesh Saitwal
 
Intro to c chapter cover 1 4
Hazwan Arif
 
CPU INPUT OUTPUT
Aditya Vaishampayan
 
Managing input and output operations in c
niyamathShariff
 
COM1407: Input/ Output Functions
Hemantha Kulathilake
 
Introduction to Basic C programming 02
Wingston
 
C formatted and unformatted input and output constructs
GopikaS12
 
C programing Tutorial
Mahira Banu
 
C programming Workshop
neosphere
 
Important C program of Balagurusamy Book
Abir Hossain
 
C language basics
Nikshithas R
 
CP Handout#7
trupti1976
 
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Basic concept of c++
shashikant pabari
 
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 

Viewers also liked (10)

PPS
C language first program
NIKHIL KRISHNA
 
DOCX
servlet programming
Rumman Ansari
 
PPTX
My first program in c, hello world !
Rumman Ansari
 
PPTX
How c program execute in c program
Rumman Ansari
 
PPTX
C Programming Language Step by Step Part 3
Rumman Ansari
 
PPTX
C Programming Language Step by Step Part 2
Rumman Ansari
 
PPTX
Introduction to C Programming
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
PPTX
Securing the Cloud
GGV Capital
 
PPSX
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
PPT
Basics of C programming
avikdhupar
 
C language first program
NIKHIL KRISHNA
 
servlet programming
Rumman Ansari
 
My first program in c, hello world !
Rumman Ansari
 
How c program execute in c program
Rumman Ansari
 
C Programming Language Step by Step Part 3
Rumman Ansari
 
C Programming Language Step by Step Part 2
Rumman Ansari
 
Securing the Cloud
GGV Capital
 
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
Basics of C programming
avikdhupar
 
Ad

Similar to First c program (20)

PPS
T02 a firstcprogram
princepavan
 
PPTX
CHAPTER 3 STRUCTURED PROGRAMMING.pptx 2024
ALPHONCEKIMUYU
 
PPTX
c_pro_introduction.pptx
RohitRaj744272
 
PPT
Chapter02.PPTArray.pptxArray.pptxArray.pptx
yatakumar84
 
PPT
Variables, identifiers, constants, declaration in c
GayathriShiva4
 
PPT
THE BASIC CONCEPTS OF C PROGRAMMINGPPT.PPT
shanthabalaji2013
 
PPT
C language ppt is a presentation of how to explain the introduction of a c la...
sdsharmila11
 
PPT
Introduction to Problem Solving C Programming
RKarthickCSEKIOT
 
PPT
introduction2_programming slides briefly exolained
RumaSinha8
 
PPT
Chapter02.PPT
Chaitanya Jambotkar
 
PPT
A File is a collection of data stored in the secondary memory. So far data wa...
bhargavi804095
 
PPT
c programming 2nd chapter pdf.PPT
KauserJahan6
 
PPS
C language
umesh patil
 
PPTX
Introduction to C Programming language Chapter02.pptx
foxel54542
 
PPTX
Introduction to c programming
SaranyaK68
 
PPTX
Introduction to c programming
SaranyaK68
 
DOCX
Report on c and c++
oggyrao
 
PPT
C the basic concepts
Abhinav Vatsa
 
PPTX
Diploma ii cfpc u-2 datatypes and variables in c language
Rai University
 
PPTX
A Comprehensive Guide to C Programing Basics, Variable Declarations, Input/O...
Muhammad Khubaib Awan
 
T02 a firstcprogram
princepavan
 
CHAPTER 3 STRUCTURED PROGRAMMING.pptx 2024
ALPHONCEKIMUYU
 
c_pro_introduction.pptx
RohitRaj744272
 
Chapter02.PPTArray.pptxArray.pptxArray.pptx
yatakumar84
 
Variables, identifiers, constants, declaration in c
GayathriShiva4
 
THE BASIC CONCEPTS OF C PROGRAMMINGPPT.PPT
shanthabalaji2013
 
C language ppt is a presentation of how to explain the introduction of a c la...
sdsharmila11
 
Introduction to Problem Solving C Programming
RKarthickCSEKIOT
 
introduction2_programming slides briefly exolained
RumaSinha8
 
Chapter02.PPT
Chaitanya Jambotkar
 
A File is a collection of data stored in the secondary memory. So far data wa...
bhargavi804095
 
c programming 2nd chapter pdf.PPT
KauserJahan6
 
C language
umesh patil
 
Introduction to C Programming language Chapter02.pptx
foxel54542
 
Introduction to c programming
SaranyaK68
 
Introduction to c programming
SaranyaK68
 
Report on c and c++
oggyrao
 
C the basic concepts
Abhinav Vatsa
 
Diploma ii cfpc u-2 datatypes and variables in c language
Rai University
 
A Comprehensive Guide to C Programing Basics, Variable Declarations, Input/O...
Muhammad Khubaib Awan
 
Ad

Recently uploaded (20)

PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Complete Network Protection with Real-Time Security
L4RGINDIA
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Complete Network Protection with Real-Time Security
L4RGINDIA
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 

First c program

  • 1. A First C Program
  • 2. Try#include <stdio.h> C/*Program */ Your First I/O header file pre-processor directive header file – contains I/O routines main() Indicates a { program building block called function comment main must be present in each C program one statement printf(“Hello world ”); statement terminator printf(“Welcome to CSCI230n“); printf(“I am John Smithn”); } A C program contains one or more functions  main() is the function name of your main (root) program  { }: braces (left & right) to construct a block containing the statements of a function  Every statement must end with a ;  is called an escape character  n is an example of an escape sequence which indicates newline  Other escape sequences are: t r a ” Exercise: Use any editor to type and then save your first program as main.c % gcc main.c % a.out and observe its result. 
  • 3.  Variable identifiers Identifiers  Begin with a letter or underscore: A-Z, a-z, _  The rest of the name can be letters, underscore, or digits  Guarantee that east least the first 8 characters are significant (those come after the 8th character will be ignored) while most of C compiler allows 32 significant characters. Example: _abc ABC Time time _a1 abcdefgh abcdefghi (may be the same as abcdefgh)  Case sensitive  Keywords: reserved names (lexical tokens) auto double if static break case entry long typedef float else switch char return union int struct extern register do go sizeof continue …
  • 4.  Fundamental Data Type Four Data Types (assume 2’s complement, byte machine) Data Type Size (byte) Range char 1 -128 ~ 127 unsigned char char Abbreviation 1 0 ~ 255 2 or 4 -215 ~ 215-1 or -231 ~ 231-1 2 or 4 0 ~ 65535 or 0 ~ 232-1 int unsigned int short int short 2 -32768 ~ 32767 unsigned short int unsigned short 2 0 ~ 65535 long int long 4 -231 ~ 231-1 unsigned long int int unsigned unsigned long 4 0 ~ 232-1 float double Note: 4 8 27 = 128, 215 =32768, 231 = 2147483648 Complex and double complex are not available
  • 5. Variable Declarations type v1,v2,v3, …, vn Example: int i; int j; float k; char c; short int x; long int y; unsigned int z; int a1, a2, a3, a4, a5;
  • 6.  Numeric, Char, String Literals Literal  Numeric literal  fixed-point  octal O32 (= 24D) (covered later)  hexadecimal OxFE or Oxfe (=254D) (covered later)  decimal int 32  long (explicit) 32L or 32l  an ordinary integer literal that is too long to fit in an int is also too long for long  floating-point  No single precision is used; always use double for literal Example: 1.23 123.456e-7 0.12E
  • 7. Numeric, Char, String Literals • Character literal (covered later) • American Standard Code for Information Interchange (ASCII) single space 32 ‘0’ - ‘9’ 48 - 57 ‘A’ - ‘Z’ 65 - 90 ‘a’ - ‘z’ • Printable: 97 - 122 • Nonprintable and special meaning chars „n‟ new line „‟ back slash „0‟ null 9 0 „f‟ formfeed 10 „t‟ tab „‟‟ single quote 39 „b‟ back space 12 8 ‟r‟ carriage return „”‟ double quote 34 „ddd‟ arbitrary bit pattern using 1-3 octal digits „Xdd‟ for Hexadecimal mode „017‟ or „17‟ Shift-Ins, ^O „04‟ or „4‟ or „004‟ „033‟ or „X1B‟ 9 <esc> EOT (^D) 13
  • 8. Numeric, Char, String Literals  String Literal  will be covered in Array section  String is a array of chars but ended by „0‟  String literal is allocated in a continuous memory space of Data Segment, so it can not be rewritten A B C D „0‟ ... Example: “ABCD” 4 chars but takes 5 byte spaces in memory Question: “I am a string” takes ? Bytes Ans: 13+1 = 14 bytes
  • 9. Numeric,literals & ASCII codes: • Character Char, String Literals char x; x=„a‟; /* x = 97*/ Notes: – „a‟ and “a” are different; why? „a‟ is the literal 97 “a” is an array of character literals, { „a‟, „0‟} or {97, 0} – “a” + “b” +”c” is invalid but „a‟+‟b‟+‟c‟ = ? (hint: „a‟ = 97 in ASCII) „a‟ + „b‟ + „c‟ = 97 + 98 + 99 = 294 = 256 + 38 in the memory 1 38 – if the code used is not ASCII code, one should check out each value of character
  • 10. Initialization  If a variable is not initialized, the value of variable may be either 0 or garbage depending on the storage class of the variable. int i=5; float x=1.23; char c=„A‟; int i=1, j,k=5; char c1 = „A‟, c2 = 97; float x=1.23, y=0.1;
  • 11.  Memory Concepts Each variable has a name, address, type, and value 1) int x; 2) scanf(“%d”, &x); 3) user inputs 4) x = 200; 10 After the execution of (1) After the execution of (2) x After the execution of (3) x After the execution of (4) x x Previous value of x was overwritten 10 200
  • 12. Sample Problem  Write a program to take two numbers as input data and print their sum, their difference, their product and their quotient. Problem Inputs float x, y; /* problem Output float sum; /* float difference; x and y */ float product; /* y */ float quotient; /* divided by y */ two items */ sum of x and y */ /* difference of product of x and quotient of x
  • 13. Sample Problem  (cont.) Pseudo Code: Declare variables of x and y; Prompt user to input the value of x and y; Print the sum of x and y; Print the difference of x and y; Print the product of x and y; If y not equal to zero, print the quotient of x divided by y
  • 14. Example Program #include <stdio.h> int main(void) { float x,y; float sum; function • name • list of argument along with their types • return value and its type • Body printf(“Enter the value of x:”); scanf(“%f”, &x); printf(“nEnter the value of y:”); scanf(“%f”, &y); sum = x + y; printf(“nthe sum of x and y is:%f”,sum); printf(“nthe sum of x and y is:%f”,x+y); printf(“nthe difference of x and y is:%f”,x-y); printf(“nthe product of x and y is:%f”,x*y); if (y != 0) printf(“nthe quotient of x divided by y is:%f”,x/y); else printf(“nquotient of x divided by y does not exist!n”); return(0); } inequality operator