Escrito por estudiantes que aprobaron Inmediatamente disponible después del pago Leer en línea o como PDF ¿Documento equivocado? Cámbialo gratis 4,6 TrustPilot
logo-home
Document preview thumbnail
Vista previa 3 fuera de 20 páginas
Notas de lectura

string oops c language

Document preview thumbnail
Vista previa 3 fuera de 20 páginas

Lecture notes of 20 pages for the course Problem Solving And Object Oriented Programming at Vellore Institute Of Technology (strings)

Vista previa del contenido

Strings
In C programming, a string is a sequence of characters terminated with a
null character \0 . For example:

char c[] = "c string";




When the compiler encounters a sequence of characters enclosed in the
double quotation marks, it appends a null character \0 at the end by
default.




Memory Diagram


How to declare a string?

Here's how you can declare strings:

char s[5];




String Declaration in C


Here, we have declared a string of 5 characters.

,How to initialize strings?

You can initialize strings in a number of ways.

char c[] = "abcd";

char c[50] = "abcd";

char c[] = {'a', 'b', 'c', 'd', '\0'};

char c[5] = {'a', 'b', 'c', 'd', '\0'};




String Initialization in C


Let's take another example:

char c[5] = "abcde";


Here, we are trying to assign 6 characters (the last character is '\0' ) to
a char array having 5 characters. This is bad and you should never do this.



Assigning Values to Strings

Arrays and strings do not support the assignment operator once it is
declared. For example,

char c[100];
c = "C programming"; // Error! array type is not assignable.



Note: Use the strcpy() function to copy the string instead.

, Read String from the user

You can use the scanf() function to read a string.
The scanf() function reads the sequence of characters until it
encounters whitespace (space, newline, tab, etc.).




Example 1: scanf() to read a string

#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}


Output

Enter name: Dennis Ritchie
Your name is Dennis.




Even though Dennis Ritchie was entered in the above program,
only "Dennis" was stored in the name string. It's because there was a space
after Dennis .

Also notice that we have used the code name instead of &name with scanf() .

scanf("%s", name);


This is because name is a char array, and we know that array names decay
to pointers in C.
Thus, the name in scanf() already points to the address of the first element in
the string, which is why we don't need to use & .

Información del documento

Subido en
17 de junio de 2022
Número de páginas
20
Escrito en
2021/2022
Tipo
Notas de lectura
Profesor(es)
Jaya
Contiene
Todas las clases
$8.49

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
3
Última venta
-



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes