Coding Club

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Coding club – C language

Topics to be covered: -
1) - variables, data types, input/output
2) - Instructions & Operators
3) - Conditional Statements
4) - Loop Control Statements
5) - Functions & Recursion
6) - Pointers
7) - Arrays
8) - Strings
Chapter - 1 - variables, data types,
input/output.
Variables: -
 In C language, a variable is a named storage location that
holds a value of a particular data type. It is used to store
values that can be accessed and manipulated by the
program during runtime.

 The syntax for declaring a variable in C is as follows:


data_type variable_name;
Here, data_type represents the type of data that the
variable can hold, and variable_name is the name given
to the variable. For example, to declare an integer
variable named my_age, the syntax would be:
Int my_age;
 In addition to declaring a variable, you can also assign an
initial value to it at the time of declaration, like this:
int my_age = 20;
In this case, the variable my_age is assigned an initial
value of 10.
Rules for writing variables: -
1. Variable names in C must begin with a letter or an
underscore (_). They cannot begin with a number.
2. Variable names can only contain letters, numbers, and
underscores.
3. C is a case-sensitive language, so variables named "My
Variable" and "my variable" are considered two different
variables.
4. Variable names in C must be unique within their scope.
That means you cannot have two variables with the same
name in the same block of code.
5. The maximum length of a variable name in C is 31
characters.
6. Values of variables can be changed.
7. Variable names should be meaningful and descriptive, so
they are easy to understand and maintain. For example,
"age" is a more meaningful variable name than "x"
Data types in C language: -
int: used to store integer values.
float: used to store floating-point values with
single precision.
double: used to store floating-point values
with double precision.
char: used to store a single character.
Constants: -
In C, a constant is a value that does not change during
program execution. It is a fixed value that is defined
and initialized at the time of its declaration, and its
value remains the same throughout the program.
1) Integer Constants: These are the constants that
represent whole numbers.
Ex: - 1, 56, -34
2) floating-point Constants: These are the constants
that represent real numbers.
Ex: - 4.08, -2.56, 3.14
3) Character Constants: These are the constants that
represent individual characters. They are enclosed in
single quotes.
Ex: - ‘a’, ‘A’, ‘&’, ‘#’
Keywords: -
Keywords in C are words that have a predefined
meaning in the programming language and cannot be
used for any other purpose, such as variable names or
function names.
This are the reserved words that have special meaning
to the compiler.
There are 32 keywords in c language.
The following is a list of all the keywords in C:
C Keywords

auto double int struct

break else long switch

case enum register typedef

char extern return union

continue for signed void

do if static while

default goto sizeof volatile

const float short unsigned


Simple Program Structure: -
Comments: -
In C programming language, comments are used to
provide additional information about the code that is
not executed by the computer
There are two types of comments in C:
1. Single-line comments: Single-line comments start
with two forward slashes "//" and continue until
the end of the line. For example:
// This is a single-line comment

2. Multi-line comments: Multi-line comments start


with /* and end with */. They can span multiple
lines of code. For example:
/*
This is a multi-line comment
It can span multiple lines of code
*/
Output: -
In C programming language, output is typically printed
to the standard output stream using the printf function.
printing the string "Hello, world!" to the console:

In C, the escape sequence "\n" is used to represent a


newline character. When this sequence is included in a
string or character array, it causes the output to move
to the beginning of the next line. Here's an example:
You can also use printf to output variables, formatted
text, and other types of data. Here are a few examples:

This program will output the values of num, pi, and


letter to the console, using the %d, %f, and %c format
specifiers respectively.
Input: -
In C, "input" typically refers to getting data from the
user or from a file. The standard library function used
for getting input from the user is "scanf", which reads
formatted input from the standard input stream
(usually the keyboard) and stores it in variables.
Here's an example of using "scanf" to get input from the
user for an integer variable:

In this example,
the "printf" function is used to display a message to the
user, prompting them to enter an integer. Then, the
"scanf" function is called to read the integer from the
user and store it in the variable "num". Finally, the
"printf" function is used again to display the entered
integer back to the user.
Q.1) write a program to calculate the area of a square.
Take the sides from the user.
Q.2) write a program to calculate the area of a circle.
Take radius from user.
Homework problem: -
Q.1) write a program to calculate the perimeter of a
rectangle. Take side from user.
Q.2) take a number from user and output its cube.

You might also like