You are on page 1of 24

NATIONAL ECONOMICS UNIVERSITY

SCHOOL OF INFORMATION TECHNOLOGY AND DIGITAL ECONOMICS

CHAPTER I
INTRODUCTION TO C
OUTLINE

¡ Introduction to C
¡ Write Our First Program – Hello World
¡ How a C Program Works
¡ Variables, Expressions, Statements
¡ Print Variables Using Printf
¡ Take Input from User
¡ Comments
INTRODUCTION TO C

¡ C is a high-level programming language developed in 1972 by Dennis Ritchie


PROGRAMING LANGUAGE (1)

People: Computer:
“Hello!” 11010101010101000
Programming
language
PROGRAMING LANGUAGE (2)
PROGRAMING LANGUAGE (3)

#include <stdio.h>

int main()
{
printf("Hello World\n");
return 0;
}
PROGRAMING TOOLS

¡ Online Tools:
¡ www.onlinegdb.com/online_c_compiler

¡ Offline Tools:
¡ CodeBlocks
¡ DevC
¡ CLion
ONLINE TOOL

¡ https://www.onlinegdb.com/online_c_compiler
¡ http://www.codeblocks.org/downloads/binaries

Download and Install


WRITING OUR FIRST PROGRAM – HELLO WORLD
HOW A C PROGRAM WORKS

#include <stdio.h>

int main()
{
printf("Hello World\n");
return 0;
}
C BASICS

¡ C is a space insensitive language


VARIABLES

Data
15

15

Memory
Each location in the memory is unique

Variables allow to provide a meaningful name for the location in memory


EXPRESSION AND STATEMENTS

¡ Combination of operators and operands, for example:

Operators

2*y+5

Operands
PRINT VARIABLES USING PRINTF

¡ In C programming language, printf() function is used to print the


“character, string, float, integer, octal and hexadecimal values” onto the
output screen.
¡ Following is the declaration for printf() function:

int printf(const char *format, ...)


printf(“Hello World!”)
TAKING INPUT FROM USER

#include <stdio.h>

int main(){
int x;
x = 5; //Hard Coding
int y = x/2
printf(”y= %i, x = %i,y,x");
return 0;
}
TAKING INPUT FROM USER

¡ In C programming, scanf() is one of the commonly used function to take input from
the user.
¡ The scanf() function reads formatted input from the standard input such as
keyboards.
TAKING INPUT FROM USER

¡ Program to Calculate Area of a Circle:


COMMENTS

¡ A comment is an explanation or description of the code of the program


¡ A comment be anywhere in your program.
¡ Two type of comments:
¡ Single line comment: //
¡ Multi-line comment: /* */.

“Code tells you HOW, Comments tell you WHY”


COMMENTS

/**********************************************************
Author: Pham Xuan Lam
Description: A Simple HelloWorld Program
Last Update: 20/12/2018
**********************************************************/
¡ Comment Tips:
¡ Don't overdo Comment
¡ Use TODO comment
EXERCISES 1

Write a program that prints the perimeter of a rectangle to take its height and
width as input:

Expected Output :
Input the height of the Rectangle : 5
Input the width of the Rectangle : 7
Perimeter of the Rectangle is : 24
EXERCISES 2

Write a program that converts Centigrade to Fahrenheit:

Expected Output :
Input a temperature (in Centigrade): 45
113 degrees Fahrenheit.
EXERCISES 3

Write a program that takes hours and minutes as input, and calculates the total number of
minutes:

Expected Output :
Input hours: 5
Input minutes: 37
Total: 337 minutes.
SUMMARY

¡ Introduction to C
¡ Write Our First Program – Hello World
¡ How a C Program Works
¡ Variables, Expressions, Statements
¡ Print Variables Using Printf
¡ Take Input from User
¡ Comments

You might also like