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

Summary, Introduction to C Programming: Variables, Data Types, Operators, and Input/Output, Summaries of C programming

This document offers a foundational overview of the c programming language, covering essential concepts such as variables, basic data types (int, char, double), arithmetic operators, and input/output operations using printf and scanf. it also delves into preprocessor directives, comments, and program structure, providing a solid base for beginners to understand the fundamentals of c programming. Examples to illustrate key concepts and explains important rules for writing c code.

Typology: Summaries

2024/2025

Uploaded on 04/24/2025

abc-51w
abc-51w 🇨🇦

5 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to C Programming
Overview of C
C is an imperative procedural language that supports structured
programming, lexical variable scope, recursion, and uses a static type
system.
Evolution of C
The ancestor of C was called B in 1971. The first version of C was created
by Kernighan and Ritchie in 1972. The C99 version, introduced in 1999,
allows mixed declarations and executables, variable length arrays, and new
types supporting Unicode characters, complex numbers, and boolean values.
Preprocessor Directives
A preprocessor is a system program that modifies the C program before
compilation. A preprocessor directive is an instruction to the preprocessor.
A library is a collection of functions, symbols, and values. Two types of
preprocessor directives exist: #include, used to include libraries of
functions and macro values. #include <stdio.h> includes the standard
input/output header, which contains macro definitions, constants, and
declarations of functions and types for standard input and output
operations.
Comments in C
Comments are lines of code that are ignored by the compiler.
Instructions
Instructions, also known as statements, are terminated by a semicolon (;).
Line changes and tabs are not significant to the C compiler. For example,
a=3; b=4; c=5; is equivalent to a = 3; b = 4; c = 5;.
Skeleton of a Program
A basic C program structure includes #include <stdio.h>, additional
includes, constant macros, the int main (void) function containing
declarative and executable statements, and return (0);.
My First Program
A simple C program includes the stdio.h header, the main function, a
printf statement to display output, and a return (0); statement. The
return (0); statement returns an integer (0) to the operating system,
pf3
pf4

Partial preview of the text

Download Summary, Introduction to C Programming: Variables, Data Types, Operators, and Input/Output and more Summaries C programming in PDF only on Docsity!

Introduction to C Programming

Overview of C

C is an imperative procedural language that supports structured programming, lexical variable scope, recursion, and uses a static type system.

Evolution of C

The ancestor of C was called B in 1971. The first version of C was created by Kernighan and Ritchie in 1972. The C99 version, introduced in 1999, allows mixed declarations and executables, variable length arrays, and new types supporting Unicode characters, complex numbers, and boolean values.

Preprocessor Directives

A preprocessor is a system program that modifies the C program before compilation. A preprocessor directive is an instruction to the preprocessor. A library is a collection of functions, symbols, and values. Two types of preprocessor directives exist: #include, used to include libraries of functions and macro values. #include <stdio.h> includes the standard input/output header, which contains macro definitions, constants, and declarations of functions and types for standard input and output operations.

Comments in C

Comments are lines of code that are ignored by the compiler.

Instructions

Instructions, also known as statements, are terminated by a semicolon (;). Line changes and tabs are not significant to the C compiler. For example, a=3; b=4; c=5; is equivalent to a = 3; b = 4; c = 5;.

Skeleton of a Program

A basic C program structure includes #include <stdio.h>, additional includes, constant macros, the int main (void) function containing declarative and executable statements, and return (0);.

My First Program

A simple C program includes the stdio.h header, the main function, a printf statement to display output, and a return (0); statement. The return (0); statement returns an integer (0) to the operating system,

indicating successful program completion. It is typically the last statement in a program.

Variables

In programming, variables are used to store data. C has three basic types of variables.

Basic Data Types

int represents a whole number.

Identifiers

All variables must be declared and have names. Examples include int lifespan;, double mass;, and char letter;.

Hard Rules for Identifiers

An identifier must not be a reserved word. An identifier must never begin with a digit. An identifier should not be a standard identifier. Examples of valid and invalid identifiers are provided.

Executable Statements

Executable statements should come after the declarations in a function for compatibility with all compilers.

Assignment Statements

An assignment statement copies the value on the right-hand side of the = operator to the variable on the left-hand side. For example: kms = KMS_PER_MILE * miles;. Assignment is not the same as an algebraic equation. For example, sum = sum + item; is a valid assignment. The right side of an assignment can be a constant, a variable, or an expression.

The printf Statement

The printf statement displays a line of program output. It uses a print list, which contains the variables or expressions whose values are displayed. Placeholders, symbols beginning with % in a format string, indicate where to display the output value. For example: printf ('That equals %lf kilometers. \n', kms);.

Placeholders in Format String

%c is used for char variables in printf and scanf.

Arithmetic Operators

The arithmetic operators in C include + (addition), - (subtraction), * (multiplication), / (division), and % (remainder). Integer expressions always return integer results. For example, 5 + 2 is 7 , 5.0 + 2.0 is 7.0, 5 - 2 is 3 , 5.0 - 2.0 is 3.0, 5 * 2 is 10 , 5.0 * 2.0 is 10.0, 5.0 / 2. is 2.5, 5 / 2 is 2 , and 5 % 2 is 1.

Integer Division

An operation with two integer operands always gives an integer answer.

Expressions with Multiple Operators

A unary operator is an operator with one operand. For example: x = y + z;.

Rules for Evaluating Expressions

Rule #1: Parentheses Rule - All sub-expressions are evaluated separately. Rule #2: Operator Precedence Rule - unary + - first, * / % next, binary + - last. Rule #3: Associativity Rule - Right Associativity: Unary operators in the same expression and at the same precedence level are evaluated right to left.

Numerical Inaccuracies

Representational error is an error due to coding a real number as a finite number of binary digits. Arithmetic underflow is an error in which a very small computational result is represented as zero.

Modes of Operation

Interactive mode involves the user responding to prompts by typing in data.

Reading from an Existing File

To read from a file, you need first to declare it (FILE is all uppercase). fclose (in);

Writing/Creating a New File

To create a new file and write into it from a file, you need first to declare it. fclose (out);

Common Programming Errors

Syntax errors are violations of the C grammar rules.