Learning About Compiler Installation, Basic C Program, Basic Data Types, Basic Input or Output and The Use of Variables
Learning About Compiler Installation, Basic C Program, Basic Data Types, Basic Input or Output and The Use of Variables
Learning About Compiler Installation, Basic C Program, Basic Data Types, Basic Input or Output and The Use of Variables
Programming
A programming language is a formal language designed to communicate instructions to a
machine, particularly a computer. Programming languages can be used to create programs that
control the behavior of a machine and/or to express algorithms precisely.
Compiler
A compiler is a computer program (or set of programs) that transforms source code written in
a programming language (the source language) into another computer language (the target
language, often having a binary form known as object code). The most common reason for wanting
to transform source code is to create an executable program.
Install Compiler
Step#1: http://download.savannah.nongnu.org/releases/tinycc/tcc-0.9.25-win32-
bin.zip
Step#7: Click on Advanced System Settings and you will get system properties window
Step#8: In advance tab there is Environment Variables button click on it
Step#9: In System variable, click on “New”
Step# 10: create a variable named “path” and value is c:\tcc\ like below and click OK button
Step# 11: go to search bar and write “cmd” and click on Command Prompt
Step#12: In command prompt write command “tcc” if you get result like given below means
you configure tcc compiler successfully
• Type tcc<space>filename.c in the command prompt and press enter. If you see the prompt
again without anything being displayed this means that your code compiled without errors
(i.e. no syntax errors – your program grammar is correct at least).
• You may observe that filename.exe has been created in the same directory where the
filename.c file is.
• Type filename in the command prompt and press enter. You may see that Hello World!!!
has been displayed on the screen. (filename here is the name of the exe file created)
C Language: C is a general-purpose programming language that was originally developed by Dennis
Ritchie between 1969 and 1973 at AT&T Bell Labs. C is reliable, simple and easy to use.
Include files (Library) Many languages use include files, and this is usually done at the top of the
program code. Include files contain some information that the program needs to know in order
to run the code contained in the program. For example, <stdio.h>, <conio.h>, <math.h> etc.
<stdio.h>: If you are using printf() and scanf() in your program, you will use <stdio.h> header file.
<conio.h>: If you are using getche() in your program, you will use <conio.h> header file.
<math.h>: If you are using sqrt() and pow() in your program, you will use <math.h> header file.
For more info about header files click the link below.
https://fresh2refresh.com/c-programming/c-function/c-library-functions/
Function main: Function is a named block of code. Function “main” marks the beginning of the
main function where program execution begins. Every C program has a main function. The
remaining lines of the program form the body of the function which is enclosed in braces { } .
Structure of a C program
#include <stdio.h> /*includes library file “stdio” */
void main (void) /*main starting point of the program */
{
: /*statements and variables here */
:
:
}
The output function: printf() : We use printf in our programs to display information on the screen.
printf() function is defined in <stdio.h>
Example01 :
Task 01: ::
Write a program that prints Welcome (Your Full Name) on output screen.
Escape Sequences:
Example02
#include <stdio.h>
void main(void)
{
printf("\n\t****************************\t");
printf("\n\t/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\");
printf("\n\t****************************\t");
printf("\n\t\"Programming Fundamentals\"\t");
printf("\n\t\tWelcome to C\t");
printf("\n\t****************************\t");
printf("\n\t/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\");
printf("\n\t****************************\t");
}
Task02
Write a program to print the given menu
Variable: Variable is a container which holds some value, a variable in programming is a
named memory location on which we store a value which we are going to use in our
program. Variable names should be meaningful.
Placeholder Placeholder
Data Type C Keyword Bytes Range
(printf) (scanf)
Character char 1 (–128 - 127) %c %c
Integer int 4 –2,147,483,648 to %d %d
2,147,483,647
Floating Point float 4 3.4E-38 to %f %f
3.4 E38
Example03: Asking user to type in his/her age, and displaying his/her age with a message.
# include <stdio.h>
void main (void)
{
int age;
printf("Enter your age =");
scanf("%d",&age);
printf("\nYour age = %d",age);
}
Task03:
Ask user to enter the value of a variable (say ‘x’) and display output as
“You have entered x = ____)
Task 04: Read sides length of a rectangle and calculate the area of that rectangle. Write pseudo code
first, then convert it into code.
Example 05: Taking float and character values as input and displaying output using
placeholders. It also prints ASCII value of the character.
#include <stdio.h>
void main (void)
{
float fpNum; /*Declaring floating point variable called fpNum*/
char alpha; /*Declaring character type variable called alpha*/
printf("The ASCII value of %c = %d\n",alpha,alpha); /*print alpha’s character value and ASCII value*/
Task 05: Write a program to ask user for a character input, program will print the ASCII
code of the character.
Some Notes About printf:
• The stuff that you write inside the double quotes will appear on the screen, with some
exceptions: a %d will be replaced by an integer, a %c will be replaced by a character, \n will
print new line, etc.
• Make sure you use the right format specifier or placeholder [%d for int, %c for char, %f for
float] in your printf statements! Weird things will happen if you don't.
#include <stdio.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
void main()
{
float number, sqrtValue; /*Declaring float type variables called number,sqrtValue*/
printf(" Please Enter any Numeric Value : "); /*Reading input in number*/
scanf("%f", &number);
printf("\n The Square Root Value of %f = %f ", number, sqrtValue); /*print number, sqrtValue
value */
}
Example08: Power in C Programming Example.
#include <stdio.h>
#include <math.h>
ASCII TABLE
This table lists the ASCII characters and their decimal, octal and hexadecimal numbers. Characters
which appear as names in parentheses (e.g., (nl)) are non-printing characters. A table of the common
non-printing characters appears after this table.
Lab Assignment 03
Write a C program to print the following lines.
Problem 01
Write a program to read the distance between two cities (in km.) and convert the distance into
meters and feet.
Data Requirements
Input
float kmDistance /* Distance in kilometers*/
Output
float meterDistance /* Distance in meters */
float feetDistance /* Distance in feet */
Relevant Formulae
1 km = 1000 meters
1 km = 3280.84 feet
Sample Output:
Problem 02
Write a program that calculates the acceleration (m/s2) of a jet fighter launched from an aircraft
carrier, given the jet’s takeoff speed in m/s and the distance (meters). The jet starts from rest to
takeoff. Also calculate the time (seconds) for the fighter to be accelerated to takeoff speed.
Data Requirements
Input
float velocity /* velocity of the jet */
float distance /* distance covered by the jet */
Output
float acceleration /* acceleration of the jet fighter*/
float time /* Time for the fighter to accelerate to takeoff speed */
Relevant Formula
𝑆 = 𝑣𝑡
2𝑎𝑆 = 𝑣 2
Sample Output:
Problem 03
Write a program to convert a temperature in degrees Fahrenheit to degrees Celsius.
Data Requirements
Input
float Fahrenheit /* temperature in degrees Fahrenheit */
Output
float Celsius /* temperature in degrees Celsius */
Relevant Formula
Celsius = 5/9 (Fahrenheit − 32)
Sample Output:
Problem 04
Write a program to find out the roots of a Quadratic Equation using sqrt( ) and pow( ) function.
Data Requirements
Input
int a = 1, b = 5, c = 4 /* values of constants of Quadratic Equation */
Output
int x1, int x2 /* values of roots of Quadratic Equation */
Relevant Formula
−𝑏 + √𝑏2 − 4𝑎𝑐
𝑥1 =
2𝑎
−𝑏 − √𝑏2 − 4𝑎𝑐
𝑥2 =
2𝑎
Sample Output: