Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CHAPTER 2 (C Programming - Constants and Variables), Study notes of C programming

The basics of C programming language, including C tokens, variables, data types, and constants. It explains the rules for declaring variables and the different data types that can be used in C. The document also covers how to declare variables as constants using the const keyword. It is a useful resource for beginners learning C programming language.

Typology: Study notes

2022/2023

Available from 02/19/2023

kush-panchaaalll
kush-panchaaalll 🇮🇳

2 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHAPTER 2
Constants, Variables and DataTypes
This chapter contains following topics:
C Tokens
Variables
DataTypes
Backslash characters (escape sequences)
Constants How to Declare Variables
Use of scanf function
Introduction:
A program is a set of Instructions. These instructions are made using various words
and symbols according some rules known as grammar (or syntax rules).
Like any other language, C has its own vocabulary and grammar. So, all these
details are discussed in this chapter.
Character Set :
The characters are grouped in to following categories:
1) Letters
a. Upper Case: A to Z
b. Lower Case: a to z
pf3
pf4
pf5
pf8

Partial preview of the text

Download CHAPTER 2 (C Programming - Constants and Variables) and more Study notes C programming in PDF only on Docsity!

CHAPTER 2

Constants, Variables and DataTypes

This chapter contains following topics:

 C Tokens  Variables  DataTypes  Backslash characters (escape sequences)  Constants How to Declare Variables  Use of scanf function

Introduction:

 A program is a set of Instructions. These instructions are made using various words and symbols according some rules known as grammar (or syntax rules).

 Like any other language, C has its own vocabulary and grammar. So, all these details are discussed in this chapter.

Character Set :

 The characters are grouped in to following categories:

  1. Letters

a. Upper Case: A to Z

b. Lower Case: a to z

2 ) Digits

a. All Decimal digits 0 to 9

  1. Special Characters

4 ) White Spaces

a. Blank Space

b. Horizontal tab

c. Carriage Return

d. New Line

Example of valid Identifiers:

  1. int_void
  2. Int
  3. max_no
  4. void_int
  5. no
  6. no_
  7. void

Example of invalid Identifiers:

  1. int

  2. $nos

  3. no 1

  4. no,

  5. 2sum

  6. (sum)

  7. Constants

An integer constant refers to a sequence of digits without fractional part. There are three types of integers: decimal integer, octal integer and hexadecimal integer.

Decimal integers consist of a set of digits, 0 through 9, preceded by optional + or -.

 Backlash Character Constants:

C supports special backslash character constants that are used to display output in particular format. For example ‘\n’ is used to print the text in the next line.

List of backslash character are given as below:

\b For back space \n For back space \r Carriage Return \t Horizontal tab * To print Single quote in output \” To print Double quote in output ? To print Question mark in output. \ To print Back slash in output \0 To represent null character

VARIABLES

A variable is a data name that can be used to store data value. A variable can store different value at different time.

Programmer can decide the variable name according to his/her choice but after satisfying the following rules of variables:

Rules of Variable:

  1. First character must be an alphabet, some compiler allows to use Underscore.
  2. ANSI C identifies first 31 characters.
  3. Upper Case and Lowercase letters are significant.
  4. Cannot use keyword.
  5. Must not contain white spaces.

 INTEGRAL DATATYPES:

Declaring variable as constant:

 We can also declare variable to have constant value during the whole program by using const keyword.

For example: const int PI = 3.14;

const is new data type quantifier available in ANSI standard. It tells the compiler that value of PI remains 3.14 throughout the program. So we can’t change its value at any other place than declaration.

C PROGRAM TO DO SUM OF TWO NUMBERS:

#include <stdio.h> int main() {

int number1, number2, sum;

printf("Enter two integers: "); scanf("%d %d", &number1, &number2);

// calculating sum sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum); return 0; }

OUTPUT WILL BE:

Enter two integers: 12 11 12 + 11 = 23